Disable add to cart button when product is on backorder on WooCommerce category archive pages

huangapple go评论58阅读模式
英文:

Disable add to cart button when product is on backorder on WooCommerce category archive pages

问题

我想问如何在类别和产品页面上禁用“加入购物车”按钮,当产品缺货时。

我尝试过这个:

<!-- language: lang.php -->

add_action(&#39;woocommerce_single_product_summary&#39;, &#39;check_if_backordered&#39;, 1 );

function check_if_backordered(){ 
    global $product; 
    if ($product-\&gt;is_on_backorder()){ 
        add_filter( &#39;woocommerce_is_purchasable&#39;, &#39;\__return_false&#39;); 
    } 
}

但这只对产品页面有效。有什么建议?

英文:

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(&#39;woocommerce_single_product_summary&#39;, &#39;check_if_backordered&#39;, 1 );

function check_if_backordered(){ 
    global $product; 
    if ($product-\&gt;is_on_backorder()){ 
        add_filter( &#39;woocommerce_is_purchasable&#39;, &#39;\__return_false&#39;); 
    } 
}

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(&#39;woocommerce_is_purchasable&#39;, &#39;disable_add_to_cart&#39;, 10, 2);

function disable_add_to_cart($purchasable, $product) {
    if ($product-&gt;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.

huangapple
  • 本文由 发表于 2023年3月20日 22:39:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75791680.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定