简体   繁体   English

显示在 WooCommerce 可变产品上售罄,当所有变体都缺货时

[英]Display sold out on WooCommerce variable product when all variations are out of stock

In WooCommerce I am using the function below where it adds a sold out text on the product thumbnail if the product is out of stock:在 WooCommerce 我使用下面的 function 如果产品缺货,它会在产品缩略图上添加售罄文本:

add_action( 'woocommerce_before_shop_loop_item_title', 'bbloomer_display_sold_out_loop_woocommerce' );
 
function bbloomer_display_sold_out_loop_woocommerce() {
    global $product;
    if ( ! $product->is_in_stock() ) {
        echo '<span class="soldout">Sold Out</span>';
    }
} 

It works for a simple product, but not for variable products.它适用于简单的产品,但不适用于可变产品。

For variable products with variations, if I set all variations to 0 stock quantity except for 1 variation, I notice that the sold out message still appears on the thumbnail.对于具有变体的可变产品,如果我将除 1 变体之外的所有变体设置为 0 库存数量,我注意到已售罄消息仍然出现在缩略图上。 Technically this is not correct as we do have some in stock.从技术上讲,这是不正确的,因为我们确实有一些库存。

Does anybody know how to change the code below in order to handle this?有谁知道如何更改下面的代码来处理这个问题?

You can create a custom conditional function to check if all variations of a variable product are "out of stock" as follows:您可以创建自定义条件 function 来检查变量产品的所有变体是否“缺货”,如下所示:

function is_variable_product_out_of_stock( $product ) {
    $children_count = 0; // initializing
    $variation_ids  = $product->get_visible_children();
        
    // Loop through children variations of the parent variable product
    foreach( $variation_ids as $variation_id ) {{
        $variation = wc_get_product($_variation_id); // Get the product variation Object
            
        if( ! $variation->is_in_stock() ) {
            $children_count++; // count out of stock children
        }
    }
    // Compare counts and return a boolean
    return count($variation_ids) == $children_count ? true : false;
}

Then you will use it in your revisited hooked function below:然后您将在下面重新访问的钩子 function 中使用它:

add_action( 'woocommerce_before_shop_loop_item_title', 'display_products_loop_out_of_stock' );
 
function display_products_loop_out_of_stock() {
    global $product;

    if ( ( ! $product->is_type('variable') && ! $product->is_in_stock()  ) 
    || ( $product->is_type('variable') && is_variable_product_out_of_stock( $product ) ) ) {
        echo '<span class="soldout">Sold Out</span>';
    }
} 

Code goes in functions.php file of the active child theme (or active theme).代码进入活动子主题(或活动主题)的functions.php文件。 It should works.它应该有效。

I made a lighter version of @LoicTheAztec 's function that will stop looping as soon as it finds a variable that is in stock:我制作了 @LoicTheAztec 的 function 的更轻版本,一旦找到库存变量,它将停止循环:

function is_variable_product_out_of_stock($product) {
    $variation_ids = $product->get_visible_children();
    foreach($variation_ids as $variation_id) {
        $variation = wc_get_product($variation_id);
        if ($variation->is_in_stock())
            return false;
    }
    return true;
}

Also with no fatal error because he made two critical typos in his function.也没有致命错误,因为他在 function 中犯了两个严重的错别字。

You can the do something custom like he did:你可以像他一样做一些自定义的事情:

add_action('woocommerce_before_shop_loop_item_title', 'display_products_loop_out_of_stock');
function display_products_loop_out_of_stock() {
    global $product;
    if ((!$product->is_type('variable') and !$product->is_in_stock()) or ($product->is_type('variable') and is_variable_product_out_of_stock($product)))
        echo '<span class="soldout">Sold Out</span>';
}

暂无
暂无

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

相关问题 如果 WooCommerce 中的所有变体都缺货,则显示已售罄的灰色按钮 - Display a Sold out greyed button if all variations are out of stock in WooCommerce 如果所有变体都缺货,则从目录中隐藏 WooCommerce 可变产品 - Hide WooCommerce variable product from catalog if all variations are out of stock 显示用于 woocommerce 缺货产品变体的自定义 div 块 - Display a custom div block for woocommerce out of stock product variations 产品售罄时,我可以隐藏 WooCommerce 产品变体菜单吗? - Can I hide the WooCommerce product variations menu when the product is sold out? 如果 WooCommerce 中的一个变体缺货,则将所有变体设置为缺货 - Set all variations out of stock if one variation is out of stock in WooCommerce 隐藏在Woocommerce中变量产品缺货时添加到购物车块 - Hide Add to cart block when a variable product is out of stock in Woocommerce Woocommerce 中可变产品的所有变体的总库存 - Total stock of all variations from a variable product In Woocommerce 从可变产品中获取所有变体的总库存 In Woocommerce - Get the total stock of all variations from a variable product In Woocommerce 通过 WooCommerce 中的自定义库存数量减少来灰显“缺货”产品变化 - Greying out “out-of-stock” product variations with custom stock quantity reduction in WooCommerce 在“缺货”版本之间切换时,可变产品“添加到购物车”按钮会短暂显示 - Variable product Add to Cart button displays briefly when switching between 'out of stock' variations
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM