2016-08-25 37 views
1

所以我使用條件壽代碼片斷從這個example後的成功合作,與this thread codeWooCommerce - 條件has_term已經不更新

但它更新我的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文件中有正確的代碼。

我該如何解決這個問題?

感謝

回答

2

嘗試這種方式,可以使用$後全局對象和嵌入條件的函數中:

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中找不到當前帖子所以在這種情況下,最好將它嵌入到$ post或$ product全局對象之後的函數中。