【问题标题】:Display the product shipping class on Woocommerce single product pages在 Woocommerce 单个产品页面上显示产品运输类别
【发布时间】:2019-03-01 14:01:32
【问题描述】:

我正在尝试在产品页面上显示运输类别,但我必须做错事,因为什么都没有显示..

我在function.php中使用了下面的代码:

add_action('woocommerce_single_product_summary', 'display_specific_shipping_class', 15 );
function display_specific_shipping_class(){
    global $product;

    // HERE define your targeted shipping class name
    $defined_shipping_class = "Estimated Delivery in 7-15 days";

    $product_shipping_class = $product->get_shipping_class();

    // Get the product shipping class term name
    $term_name = get_term_by( 'slug', $product_shipping_class, 'product_shipping_class' );

    if( $term_name == $defined_shipping_class ){
        echo '<p class="product-shipping-class">' . $term_name . '</p>';
    }
}

【问题讨论】:

    标签: php wordpress woocommerce shipping taxonomy-terms


    【解决方案1】:

    您得到的是 WP_term 对象而不是术语名称...所以改为使用以下内容:

    add_action('woocommerce_single_product_summary', 'display_specific_shipping_class', 15 );
    function display_specific_shipping_class(){
        global $product;
    
        // HERE define your targeted shipping class name
        $defined_shipping_class = "Estimated Delivery in 7-15 days";
    
        // Get the product shipping class WP_Term Object
        $term = get_term_by( 'slug', $product->get_shipping_class(), 'product_shipping_class' );
    
        if( is_a($term, 'WP_Term') && $term->name == $defined_shipping_class ){
            echo '<p class="product-shipping-class">' . $term->name . '</p>';
        }
    }
    

    代码进入您的活动子主题(或活动主题)的 function.php 文件中。它现在应该可以工作了。

    【讨论】:

    • 您好,感谢您的帮助。我使用了您的代码并且它正在工作,但我收到此错误PHP Notice: Trying to get property 'name' of non-object in /var/www/vhosts/../../wp-content/themes/..-child/functions.php on line 286。这是 ` if( $term->name == $defined_shipping_class ){`
    • 使用更新的代码我收到此错误[05-Mar-2019 14:37:19 UTC] PHP Warning: is_a() expects parameter 2 to be string, object given in /var/www/vhosts/..../.../wp-content/themes/...-child/functions.php on line 286
    • @Helen_zs 更新:抱歉,我已经反转了 is_a() 条件函数中的参数。再试一次,它现在应该可以工作了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-03
    • 2023-03-24
    • 2018-07-07
    • 2017-06-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多