【问题标题】:Display sold out on WooCommerce variable product when all variations are out of stock当所有变体都缺货时,显示在 WooCommerce 可变产品上售罄
【发布时间】:2021-01-10 14:01:33
【问题描述】:

在 WooCommerce 中,我使用以下功能,如果产品缺货,它会在产品缩略图上添加售罄文本:

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>';
    }
} 

它适用于简单的产品,但不适用于可变产品。

对于具有变体的可变产品,如果我将除 1 变体之外的所有变体设置为 0 库存数量,我注意到已售罄消息仍显示在缩略图上。从技术上讲,这是不正确的,因为我们确实有一些库存。

有人知道如何更改下面的代码来处理这个问题吗?

【问题讨论】:

    标签: php wordpress woocommerce stock product-variations


    【解决方案1】:

    您可以创建自定义条件函数来检查变量产品的所有变体是否“缺货”,如下所示:

    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;
    }
    

    然后你将在下面重新访问的钩子函数中使用它:

    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>';
        }
    } 
    

    代码位于活动子主题(或活动主题)的functions.php 文件中。它应该可以工作。

    【讨论】:

    • 由于某种原因无法编辑帖子,但代码中有两个致命错误:一个应该是单个的双“{{”和一个用于代替的“$_variation_id”变量"$variation_id"
    【解决方案2】:

    我为@LoicTheAztec 的函数制作了一个更轻量级的版本,一旦发现有库存的变量就会停止循环:

    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;
    }
    

    也没有致命错误,因为他在函数中犯了两个严重的拼写错误。

    你可以像他一样做一些自定义的事情:

    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>';
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-12
      • 1970-01-01
      • 2020-11-18
      • 1970-01-01
      • 1970-01-01
      • 2021-07-24
      • 2021-04-26
      相关资源
      最近更新 更多