2017-01-15 59 views
3

我遇到了如何從類別產品中刪除購物車的問題。如果我將它應用於特定的ID或一般情況下,它就可以工作,但我無法爲某個類別做到這一點。以下是我已經完成的代碼。刪除Woocommerce中針對特定產品類別的添加購物車按鈕

此外,我正在努力將相同的模式應用到相關文章部分,所以任何幫助,將不勝感激。

謝謝。

//function for deleting .... 

function remove_product_description_add_cart_button(){ 

    global $product; 

    //Remove Add to Cart button from product description of product with id 1234  

    if ($product->id == 188){ 

    remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30); 

    } 

add_action('wp','remove_product_description_add_cart_button'); 

} 

回答

4

,使其與產品類別的工作,你可以使用WordPress的條件函數has_term()這樣:

//function for deleting .... 
function remove_product_description_add_cart_button(){ 
    global $product; 

    // Set HERE your category ID, slug or name (or an array) 
    $category = 'categoryslug'; 

    //Remove Add to Cart button from product description of product with id 1234  
    if (has_term($category, 'product_cat', $product->id)) 
     remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30); 

} 
add_action('wp','remove_product_description_add_cart_button'); 

代碼放在您的活動子主題的function.php文件(或主題)。或者在任何插件的PHP文件。

1

你必須嘗試這樣的事:

function remove_product_description_add_cart_button(){ 

global $product; 

    $termsOfProduct = wp_get_post_terms($product->id, 'product_cat'); 

    if (in_array("CatToFind", $termsOfProduct)) { 

    remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30); 

    } 

add_action('wp','remove_product_description_add_cart_button'); 

} 

類別產品Woocommerce僅僅條款。 Wp_get_post_terms允許您查找與帖子(產品ID)關聯的任何類別。

參考:https://codex.wordpress.org/Function_Reference/wp_get_post_terms

0

你可以嘗試這樣的事情

function western_custom_buy_buttons(){ 

    $product = get_product(); 

    if (has_term('category1', 'product_cat')){ 

    // removing the purchase buttons 

          remove_action('woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart'); 



    remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30); 

    remove_action('woocommerce_simple_add_to_cart', 'woocommerce_simple_add_to_cart', 30); 

    remove_action('woocommerce_grouped_add_to_cart', 'woocommerce_grouped_add_to_cart', 30); 

    remove_action('woocommerce_variable_add_to_cart', 'woocommerce_variable_add_to_cart', 30); 

    remove_action('woocommerce_external_add_to_cart', 'woocommerce_external_add_to_cart', 30); 

    } 

} 

add_action('wp', 'western_custom_buy_buttons'); 

參考:https://www.themelocation.com/how-to-remove-add-to-cart-button-from-a-certain-category-woocommerce/

相關問題