从Woocommerce中的可变产品中获取所有变体的总库存 [英] Get the total stock of all variations from a variable product In Woocommerce

查看:155
本文介绍了从Woocommerce中的可变产品中获取所有变体的总库存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在woocommerce中,我想显示一个可变产品中所有变体的总数.每个产品变体的库存都在产品变体级别进行管理.

IN woocommerce I would like to display the total stock of all variations in a variable product. The stock of each product variation is managed at the product variation level.

如果我用于可变产品:

global $product;

echo $product->get_stock_quantity();

我没有获得该产品所有版本的总库存.

I don't get the total stock of all variations of this product.

在Woocommerce中是否可以从可变产品中获取所有变体的总库存?

Is it possible to get the total stock of all variations from a variable product In Woocommerce?

推荐答案

以下自定义函数将显示可变产品的所有产品变化库存数量的总和(仅).

The following custom function will displays the sum of all product variations stock quantity for a variable product (only).

它使用非常简单的SQL查询,使变量产品中所有子代的总和变化.

It use a very light SQL query that make the sum of all children variations from a variable product.

注意:库存需要在产品变化级别上进行管理.

Note: The stock need to be managed at product variation level.

function wc_get_variable_product_stock_quantity( $output = 'raw', $product_id = 0 ){
    global $wpdb, $product;

    // Get the product ID (can be defined)
    $product_id = $product_id > 0 ? $product_id : get_the_id();

    // Check and get the instance of the WC_Product Object
    $product = is_a( $product, 'WC_Product' ) ? $product : wc_get_product($product_id);

    // Only for variable product type
    if( $product->is_type('variable') ){

        // Get the stock quantity sum of all product variations (children)
        $stock_quantity = $wpdb->get_var("
            SELECT SUM(pm.meta_value)
            FROM {$wpdb->prefix}posts as p
            JOIN {$wpdb->prefix}postmeta as pm ON p.ID = pm.post_id
            WHERE p.post_type = 'product_variation'
            AND p.post_status = 'publish'
            AND p.post_parent = '$product_id'
            AND pm.meta_key = '_stock'
            AND pm.meta_value IS NOT NULL
        ");

        // Preparing formatted output
        if ( $stock_quantity > 0 ) {
            $html = '<p class="stock in-stock">'. sprintf( __("%s in stock", "woocommerce"), $stock_quantity ).'</p>';
        } else {
            if ( is_numeric($stock_quantity) )
                $html = '<p class="stock out-of-stock">' . __("Out of stock", "woocommerce") . '</p>';
            else
                $html = '';
        }

        // Different output options
        if( $output == 'echo_html' )
            echo $html;
        elseif( $output == 'return_html' )
            return $html;
        else
            return $stock_quantity;
    }
}

代码进入您的活动子主题(或活动主题)的function.php文件中.经过测试,可以正常工作.

Code goes in function.php file of your active child theme (or active theme). Tested and works.

1)在php代码中的任何地方,获取库存数量(从动态 $product_id获取):

1) Anywhere in php code, get the stock quantity (from a dynamic $product_id):

$stock_quantity = wc_get_variable_product_stock_quantity( 'raw', $product_id );


2)在商店和档案页面中显示(在价格范围示例下):


2) Display in shop and archives pages (Under the price range example):

add_action( 'woocommerce_after_shop_loop_item_title', 'display_variable_product_stock_quantity', 20 );
function display_variable_product_stock_quantity(){
    wc_get_variable_product_stock_quantity( 'echo_html' );
}

代码进入您的活动子主题(或活动主题)的function.php文件中.

3)在单个产品页面中显示(在价格范围示例下):

3) Display in single product pages (Under the price range example):

add_action( 'woocommerce_single_product_summary', 'display_variable_product_stock_quantity', 15 );
function display_variable_product_stock_quantity(){
    wc_get_variable_product_stock_quantity( 'echo_html' );
}

代码进入您的活动子主题(或活动主题)的function.php文件中.

这篇关于从Woocommerce中的可变产品中获取所有变体的总库存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆