繁体   English   中英

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

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

IN woocommerce 我想显示可变产品中所有变体的总库存。 每个产品变型的库存在产品变型级别进行管理。

如果我用于可变产品:

global $product;

echo $product->get_stock_quantity();

我不知道该产品所有变体的总库存。

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

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

它使用一个非常轻量级的 SQL 查询,从一个变量产品中得出所有子变量的总和。

注意:库存需要在产品变异级别进行管理。

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 文件中。 测试和工作。


用法(3例):

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

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

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

代码位于活动子主题(或活动主题)的 function.php 文件中。

在此处输入图片说明


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

代码位于活动子主题(或活动主题)的 function.php 文件中。

在此处输入图片说明

如果产品不是可变产品或在产品级别启用库存管理,则此 function 返回总库存数量。

否则它会计算每个变体的总库存。

您可以将此 function 放入例如 functions.php 中。 并这样称呼它:

<?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 . " ";

        }
    }

将此代码粘贴到您的主题的 function.php 中,并欢呼您的工作将完成。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM