删除Woocommerce中针对特定产品类别的“添加购物车"按钮 [英] Remove add cart button in Woocommerce for a specific product category
问题描述
我在如何从类别产品中删除购物车时遇到问题.如果我将其应用于特定的ID或一般而言,它就可以正常工作,但是我无法为某个类别执行此操作.以下是我对此所做的代码.
I have an issue how to remove a cart from a category product. It works just fine if I apply it to a specific id or all in general, but I am unable to do it for a category. Below is my code I have done regarding it.
此外,我正在努力将这种模式应用于相关文章"部分,因此将不胜感激.
Also, I am struggling to apply this same pattern to Related Articles section, so any help would be appreciated.
谢谢.
//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');
}
推荐答案
2020年11月更新
要使其与产品类别一起使用,您可以使用WordPress条件函数 has_term()
这样:
To make it work with a product category you can use the WordPress conditional function has_term()
this way:
add_action('woocommerce_single_product_summary', 'remove_product_description_add_cart_button', 1 );
function remove_product_description_add_cart_button() { // function for deleting ...
// Set HERE your category ID, slug or name (or an array)
$categories = array('your-category-1');
//Remove Add to Cart button from product description of product with id 1234
if ( has_term( $categories, 'product_cat', get_the_id() ) ) {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
}
}
代码进入您的活动子主题(或活动主题)的function.php文件中.或也在任何插件php文件中.经过测试,可以正常工作.
Code goes in function.php file of your active child theme (or active theme). Or also in any plugin php file. Tested and works.
这篇关于删除Woocommerce中针对特定产品类别的“添加购物车"按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!