Disable add to cart button based on WooCommerce product custom stock status(根据WooCommerce产品自定义库存状态禁用添加到购物车按钮)
问题描述
当前在WooCommerce中,如果库存状态为out of stock
,则添加到购物车按钮被禁用。我使用woocommerce_product_stock_status_options
添加了标签为Discontinued product
的新库存状态,现在我正在寻找一种将此产品视为缺货的方法。
因为我认为最好将不再生产的产品和在另一家商店生产并有售但没有库存的产品分开。
推荐答案
您可以根据自定义库存状态(其中您将用您的自定义状态插件替换custom_status_slug
):
add_filter('woocommerce_is_purchasable', 'filter_is_purchasable_callback', 10, 2 );
add_filter('woocommerce_variation_is_purchasable', 'filter_is_purchasable_callback', 10, 2 );
function filter_is_purchasable_callback( $purchasable, $product ) {
if ( $product->get_stock_status() === 'custom_status_slug' ) {
return false;
}
return $purchasable;
}
代码放在活动子主题(或活动主题)的函数.php文件中。已测试并正常工作。
这篇关于根据WooCommerce产品自定义库存状态禁用添加到购物车按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!