简体   繁体   English

从可变产品中获取所有变体的总库存 In Woocommerce

[英]Get the total stock of all variations from a variable product In Woocommerce

IN woocommerce I would like to display the total stock of all variations in a variable product. IN woocommerce 我想显示可变产品中所有变体的总库存。 The stock of each product variation is managed at the product variation level.每个产品变型的库存在产品变型级别进行管理。

If I use for a variable product:如果我用于可变产品:

global $product;

echo $product->get_stock_quantity();

I don't get the total stock of all variations of this product.我不知道该产品所有变体的总库存。

Is it possible to get the total stock of all variations from a variable product In Woocommerce?是否有可能从 Woocommerce 中的可变产品中获取所有变体的总库存?

The following custom function will displays the sum of all product variations stock quantity for a variable product (only).以下自定义函数将显示可变产品(仅限)的所有产品变体库存数量的总和。

It use a very light SQL query that make the sum of all children variations from a variable product.它使用一个非常轻量级的 SQL 查询,从一个变量产品中得出所有子变量的总和。

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

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


USAGE (3 cases):用法(3例):

1) Anywhere in php code, get the stock quantity ( from a dynamic $product_id ): 1) 在 php 代码的任何地方,获取库存数量(来自动态$product_id ):

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

2) Display in shop and archives pages (Under the price range example): 2)显示在商店和档案页面(以价格范围为例):

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

Code goes in function.php file of your active child theme (or active theme).代码位于活动子主题(或活动主题)的 function.php 文件中。

在此处输入图片说明


3) Display in single product pages (Under the price range example): 3) 单品页面展示(以价格区间为例):

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

Code goes in function.php file of your active child theme (or active theme).代码位于活动子主题(或活动主题)的 function.php 文件中。

在此处输入图片说明

This function returns the total stock quantity if the product is not a variable product or the stock management is enabled at product level.如果产品不是可变产品或在产品级别启用库存管理,则此 function 返回总库存数量。

Else it calculates the total stock for each variation.否则它会计算每个变体的总库存。

You can place this function in for example functions.php.您可以将此 function 放入例如 functions.php 中。 And call it like this:并这样称呼它:

<?php

global $product;
$stockQuantity = get_total_combined_stock_quantity($product);

?>
function get_total_combined_stock_quantity($product)
{
    if (!$product->is_type('variable')) {
        return $product->get_stock_quantity();
    }

    //Stock management is enabled at product level
    if ($product->managing_stock()) {
        return $product->get_stock_quantity();
    }

    $total = 0;

    if ($product->is_type('variable')) {
        foreach ($product->get_visible_children() as $variationId) {
            $variation = wc_get_product($variationId);
            $total += $variation->get_stock_quantity();
        }
    }

    return $total;
}
add_action( 'woocommerce_before_add_to_cart_button', 'get_selected_variation_stock' );
function get_selected_variation_stock() {
    global $product;

    if ($product->is_type( 'variable' ))
    {
        $avail_vari = $product->get_available_variations();
        foreach ($avail_vari as $key => $value)
        {
            $vari_id = $value['variation_id'];
            $vari_colr = $value['attributes']['attribute_pa_colour'];
            $vari_obj = new WC_Product_variation($vari_id);
            $vari_stock = $vari_obj->get_stock_quantity();

            echo $vari_colr . ": " . $vari_stock . " ";

        }
    }

Paste this code into function.php of your theme and hurray your work will be done.将此代码粘贴到您的主题的 function.php 中,并欢呼您的工作将完成。

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

相关问题 Woocommerce 中可变产品的所有变体的总库存 - Total stock of all variations from a variable product In Woocommerce 如果所有变体都缺货,则从目录中隐藏 WooCommerce 可变产品 - Hide WooCommerce variable product from catalog if all variations are out of stock 获取Woocommerce中可变产品的“库存”产品变量 - Get the count of “in stock” product variations for a variable product in Woocommerce 从Woocommerce 3中的可变产品获取版本ID - Get variations IDs from a variable product in Woocommerce 3 从 WooCommerce 中的可变产品中获取产品变体 skus - Get the product variations skus from a variable product in WooCommerce 从Woocommerce中的可变产品变体获取产品属性标签 - Get product attribute labels from variations of a variable product in Woocommerce 获取WooCommerce中变量产品价格的所有变体属性 - Get all variations attributes with prices for a variable product in WooCommerce 显示在 WooCommerce 可变产品上售罄,当所有变体都缺货时 - Display sold out on WooCommerce variable product when all variations are out of stock 从可变产品的 WooCommerce 变体中获取重量值 - Get weight value from WooCommerce variations of a variable product 更新 Woocommerce 中可变产品的所有变体价格 - Update all variations prices of a variable product in Woocommerce
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM