简体   繁体   English

如何获取变体的库存数量 woocommerce

[英]How get variation's stock quantity woocommerce

I'm developing a theme for wordpress and woocommerce and need to show a variation's stock.我正在为 wordpress 和 woocommerce 开发一个主题,需要显示变体的库存。

<p class="stock-m13"><?php echo $product->get_stock_quantity(); ?></p>

I read How to get the stock quantity of an article from woocommerce?我阅读了如何从 woocommerce 获取文章的库存数量? , but this code only show me the global stock quantity, not each variation quantity. ,但这段代码只显示全球库存数量,而不是每个变化数量。

Do I need another function to get variation quantity?我需要另一个函数来获取变化量吗? Or it is possible that my code is not completed (I think it because I have used Twenty Fifteen theme and variation quantity is showed)?或者有可能我的代码没有完成(我认为是因为我使用了二十十五个主题并且显示了变化数量)?

I tried to get the max quantity with this:我试图用这个获得最大数量:

<?php
    foreach ($product->get_available_variations() as $key) {
        echo $key['max_qty'] .'<br/>';
    }
?>

And that works, but I don't know if this is useful when the stock goes down.这行得通,但我不知道这在股票下跌时是否有用。

Given a variable product with multiple variations in order to get each variation stock quantity:给定具有多个变体的可变产品,以获得每个变体的库存数量:

function get_stock_variations_from_product(){
    global $product;
    $variations = $product->get_available_variations();
    foreach($variations as $variation){
         $variation_id = $variation['variation_id'];
         $variation_obj = new WC_Product_variation($variation_id);
         $stock = $variation_obj->get_stock_quantity();
    }
}

This is the cleanest way I figured out for WC 2.5, I think there might be better ways in terms of performance but not as clean.这是我为 WC 2.5 找到的最干净的方法,我认为在性能方面可能有更好的方法,但没有那么干净。

The below code will print all attributes with ' is_in_stock ' variable.下面的代码将使用“ is_in_stock ”变量打印所有属性。

<?php
    global $product;
    $product_variations = $product->get_available_variations();

    foreach ($product_variations as $variation)  {
        $var_data = $variation['attributes'];
        $var_data['in_stock'] = $variation['is_in_stock'];
    }

    //List all attributes with stock available or not array..
    echo '<pre>';
    print_r($var_data);
    echo '</pre>';
    die;
?>

Yes, your way is the correct way to grab a variation's stock.是的,您的方法是获取变体库存的正确方法。

As long as you are getting the variations fresh from the database every time you need to use its stock quantity then you should be okay with using this method.只要您每次需要使用其库存数量时都从数据库中获取最新的变化,那么您应该可以使用此方法。

*******UPDATEED *******更新

I'm not sure I understand what you're trying to do.我不确定我是否理解你想要做什么。 You should just be able to grab both variation stock and send them over to jquery to play with.您应该能够获取两种变体库存并将它们发送到 jquery 来使用。

$product_variable = new WC_Product_Variable($product->id);
$product_variations = $product_variable->get_available_variations();

foreach ($product_variations as $variation)  {
  $stock[$variation['attribute_pa_variation-name']] = $variation['max_qty'];
}

Here I'm assigning stock level to an associative array with attribute_pa_"your-attribute-name" as the key在这里,我将库存水平分配给一个以 attribute_pa_"your-attribute-name" 作为键的关联数组

Now I can send my array over to jQuery.现在我可以将我的数组发送到 jQuery。

Please clarify if I'm mis-understanding the question.请澄清我是否误解了这个问题。

If anyone else is wondering how to do this when looping through items in a cart, it's just $cart_item['data']->get_stock_quantity() under the following example:如果其他人想知道如何在循环浏览购物车中的商品时执行此操作,只需在以下示例中使用$cart_item['data']->get_stock_quantity()

foreach(WC()->cart->get_cart() as $cart_item_key => $cart_item) {
  $stock = $cart_item['data']->get_stock_quantity();
}

Ok, with this I can take the quantity.好的,有了这个,我可以接受数量了。 But is not useful.但是没有用。

With this I can prepare a code that make differents sentences, one for each product, and show it when a variation would be selected using jquery.有了这个,我可以准备一个代码来制作不同的句子,每个产品一个,并在使用 jquery 选择变体时显示它。

But this is not good idea.但这不是个好主意。 Is not elegant.不优雅。 I though existed something like我虽然存在类似的东西

$product->get_variation_price()

This code return the variation price when is selected.此代码在选择时返回变化价格。 But

$product->get_stock_quantity();

not change when variation is selected because show me the global stock.选择变体时不会改变,因为向我展示全球库存。

Can somebody give me an elegant solution?有人可以给我一个优雅的解决方案吗? Or not exists?还是不存在?

UPDATE更新

Finally I used最后我用

$product->get_available_variations()

To get the data.来获取数据。

With this I've generated html code, hidden with css, and I show it when I click on variation's color.有了这个,我生成了 html 代码,用 css 隐藏,当我点击变体的颜色时我会显示它。

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

    if ($product->is_type( 'variable' ))
    {
        $available_variations = $product->get_available_variations();
        foreach ($available_variations as $key => $value)
        {
            $variation_id = $value['variation_id'];
            $attribute_pa_colour = $value['attributes']['attribute_pa_colour'];
            $variation_obj = new WC_Product_variation($variation_id);
            $stock = $variation_obj->get_stock_quantity();

            echo $attribute_pa_colour . ": " . $stock . " ";

        }
    }

By using this hook, you have to find out all variation's stock quantity as well as other variation's product data woocommerce.通过使用这个钩子,你必须找出所有变体的库存数量以及其他变体的产品数据woocommerce。

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

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