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

查看:22
本文介绍了从 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天全站免登陆