英文:
Disable add to cart button when product is on backorder on WooCommerce category archive pages
问题
我想问如何在类别和产品页面上禁用“加入购物车”按钮,当产品缺货时。
我尝试过这个:
<!-- language: lang.php -->
add_action('woocommerce_single_product_summary', 'check_if_backordered', 1 );
function check_if_backordered(){
global $product;
if ($product-\>is_on_backorder()){
add_filter( 'woocommerce_is_purchasable', '\__return_false');
}
}
但这只对产品页面有效。有什么建议?
英文:
I want to ask how to disable the “add to cart” button on categories and product pages. when the product is on backorder.
I tried this:
<!-- language: lang.php -->
add_action('woocommerce_single_product_summary', 'check_if_backordered', 1 );
function check_if_backordered(){
global $product;
if ($product-\>is_on_backorder()){
add_filter( 'woocommerce_is_purchasable', '\__return_false');
}
}
But this only works for the product page. Any advice?
答案1
得分: 1
你可以尝试使用以下代码片段:
add_filter('woocommerce_is_purchasable', 'disable_add_to_cart', 10, 2);
function disable_add_to_cart($purchasable, $product) {
if ($product->is_on_backorder(1))
$purchasable = false;
return $purchasable;
}
不过,这还取决于你的主题与 WooCommerce 的集成情况,可能你的主题不支持这种流程。
英文:
you can try with this snippet:
add_filter('woocommerce_is_purchasable', 'disable_add_to_cart', 10, 2);
function disable_add_to_cart($purchasable, $product) {
if ($product->is_on_backorder(1))
$purchasable = false;
return $purchasable;
}
Anyway, it depends on your theme integration with WC, it could be possibile that your theme does not support this flow.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论