WooCommerce - 条件has_term不会再更新后工作(WooCommerce - Co

2019-09-28 04:03发布

所以我使用条件寿代码片断从这个例子 ,与此线程代码成功:

但它的更新我的WordPress核心和WooCommerce插件后不再工作。

if ( is_product() && has_term( 'sample-category', 'product_cat' ) ){

    add_action( 'woocommerce_after_add_to_cart_button', 'add_custom_button', 10, 0 );
    function add_custom_button() {
        global $products;

        $product_link = get_permalink( $products->id );
        $sample_link = substr($product_link, 0, -1) . '-swatch-card/';
        echo '<a class="button alt btn-sample" href="' . esc_url( $sample_link ) .'">' . __( "Order a Sample", "my_theme_slug" )  . '</a>';

    }

}

儿童插件仍然在function.php文件正确的代码。

我如何能解决这个问题吗?

谢谢

Answer 1:

试试这个方法可能是,使用$后全局对象和嵌入条件的函数中:

add_action( 'woocommerce_after_add_to_cart_button', 'add_custom_button', 10, 0 );
function add_custom_button() {
    global $post;
    if ( has_term( 'collection', 'product_cat', $post->ID ) ) {
        $product_link = get_permalink( $post->ID );
        $sample_link = substr($product_link, 0, -1) . '-swatch-card/';
        echo '<a class="button alt btn-sample" href="' . esc_url( $sample_link ) .'">' . __( "Order a Sample", "my_theme_slug" )  . '</a>';
    }
};

有条件has_term()的需求有时是第三个参数的工作......在function.php无法找到当前的职位因此,在这种情况下,最好是把它嵌入后$邮寄或$产品全局对象的函数内部。



文章来源: WooCommerce - Conditional has_term doesn't work anymore after updating