简体   繁体   English

隐藏大多数产品的添加到购物车按钮-Woocommerce

[英]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.

相关问题 Woocommerce 隐藏添加到购物车按钮 可变产品除外 - Woocommerce hide add to cart button EXCEPT for variable products 禁用特定 WooCommerce 产品的添加到购物车按钮 - Disabling Add to Cart Button for Specific WooCommerce Products 隐藏WooCommerce变量产品上的“添加到购物车”按钮 - Hiding 'Add to Cart' button on WooCommerce variable products Woocommerce使用单个“添加到购物车”按钮将产品分组 - Woocommerce Grouped Products with Individual “Add To Cart” Button 为 Woocommerce 中的登录用户隐藏“添加到购物车”按钮 - Hide "Add to Cart" button for logged in users in Woocommerce 在 WordPress Woocommerce 中隐藏添加到购物车按钮 - Hide Add To cart Button in WordPress Woocommerce 在WooCommerce中为特定产品添加额外添加到购物车按钮 - Add extra add to cart button to specific products in WooCommerce 如果在ajax上购物车不为空,则隐藏自定义按钮在Woocommerce中添加到购物车 - Hide a custom button if cart is not empty on ajax add to cart in Woocommerce 在Woocommerce商店页面中更改用于简单产品的购物车按钮 - Change add-to-cart button for simple products in Woocommerce shop page WooCommerce - 更改 40 英镑或更多产品的“添加到购物车”按钮文本 - WooCommerce - Change Add to Cart button text for products £40 or more
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM