根据WooCommerce产品自定义库存状态禁用添加到购物车按钮 [英] Disable add to cart button based on WooCommerce product custom stock status

查看:76
本文介绍了根据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产品自定义库存状态禁用添加到购物车按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆