简体   繁体   English

如果 Woocommerce 商店和档案缺货,请删除产品按钮

[英]Remove product button if out of stock from Woocommerce shop and archives

I am using Add font awesome icon to custom add to cart button in Woocommerce 3 answer code to remove the "add to cart" button from my products pages, but now whenever there is an article out of stock I get a "Not Available" label there which messes with the layout of my page.在 Woocommerce 3答案代码中使用添加字体真棒图标自定义添加到购物车按钮以从我的产品页面中删除“添加到购物车”按钮,但现在每当有文章缺货时,我都会收到“不可用”标签那里弄乱了我的页面布局。

I've tried disabling it, but with no luck, anyone knows how to hide the label?我试过禁用它,但没有运气,有人知道如何隐藏标签吗?

To remove loop add to cart button when a product is out of stock use this instead:要在产品缺货时删除循环添加到购物车按钮,请改用:

add_action( 'woocommerce_after_shop_loop_item', 'out_of_stock_remove_loop_button', 2 );
function out_of_stock_remove_loop_button() {
    global $product;

    if( ! $product->is_in_stock() )
        remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
}

Code goes in function.php file of your active child theme (or active theme).代码位于活动子主题(或活动主题)的 function.php 文件中。 Tested and works.测试和工作。

Use this snippet from the official woocommerce documentation website .使用来自官方 woocommerce 文档网站的这个片段。

if (!function_exists('woocommerce_template_loop_add_to_cart')) {
    function woocommerce_template_loop_add_to_cart() {
        global $product;
        if ( ! $product->is_in_stock() || ! $product->is_purchasable() ) return;
        wc_get_template('loop/add-to-cart.php');
    }
}

Place this code into your functions.php file from your active theme or child-theme.将此代码从您的活动主题或子主题放入您的functions.php文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM