[英]Hide the add to cart button for most products - Woocommerce
I'm trying to hide the "add to cart" button for all products except for one or two items on my woocommerce website. 我想对所有产品隐藏“添加到购物车”按钮,但我的woocommerce网站上只有一两个产品除外。
Here is a simple solution I found that sets the products to "purchasable = false". 我发现这是一个简单的解决方案,可将产品设置为“ purchasable = false”。 After placing in the function.php file all the "add to cart" buttons disappear.
放入function.php文件后,所有“添加到购物车”按钮均消失。
/** hides add to cart button**/
add_filter( 'woocommerce_is_purchasable', false );
How do I add exception to this? 如何为此添加例外?
I would like to show the add to cart button for product ids 22 & 23 我想显示产品ID为22和23的添加到购物车按钮
I have spent a few hours researching any help would be greatly appreciated. 我花了几个小时研究任何帮助,将不胜感激。
Remove the first filter: 删除第一个过滤器:
/** REMOVE **/
// add_filter( 'woocommerce_is_purchasable', false );
The woocommerce_is_purchasable
filter takes in the default value of $is_purchasable
(boolean) and the WC_Product $object
it is parsing. woocommerce_is_purchasable
过滤器采用默认值$is_purchasable
(布尔值)和要解析的WC_Product $object
。 So check each object and return true
to show the button and false
to hide it. 因此,检查每个对象并返回
true
以显示按钮,返回false
以隐藏按钮。
add_filter('woocommerce_is_purchasable', 'myplugin_is_purchasable', 10, 2);
function myplugin_is_purchasable( $is_purchasable, $object ) {
// Checks to see if the product id is 22 or 23,
// returns true if is, false otherwise.
return ( 22 === $object->id || 23 === $object->id );
}
NOTE: This is probably not a very good way to accomplish this. 注意:这可能不是一个很好的方法来完成此操作。 It shouldn't be hardcoded.
它不应该被硬编码。 If the id's of the products you want to show change, it will be transparent to the user later on.
如果您要显示的产品的ID发生变化,则以后对用户透明。 You will have to dive back into code to change it.
您将不得不重新研究代码以对其进行更改。 It's just generally not a good idea to hard code id's like this.
像这样的硬编码id通常不是一个好主意。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.