php woocommerce 如何获取变异的库存量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30514596/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 21:50:02  来源:igfitidea点击:

How get variation's stock quantity woocommerce

phpwoocommercestockvariations

提问by SeiyaJapon

I'm developing a theme for wordpress and woocommerce.

我正在为 wordpress 和 woocommerce 开发一个主题。

I need show variation's stock.

我需要显示变体的库存。

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

(here: How to get the stock quantity of an article from woocommerce?) but this code only show me the global stock quantity, not each variation quantity.

(此处:如何从 woocommerce 获取文章的库存数量?)但此代码仅显示全局库存数量,而不是每个变化数量。

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 try to get max quantity with this:

我尝试通过以下方式获得最大数量:

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

And I get it, but I don't know if this is useful when the stock goes down.

我明白了,但我不知道这在股票下跌时是否有用。

采纳答案by SeiyaJapon

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 隐藏,当我点击变体的颜色时我会显示它。

回答by db306

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 找到的最干净的方法,我认为在性能方面可能有更好的方法,但没有那么干净。

回答by Faisal Alvi

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

回答by Tuesdave

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.

请澄清我是否误解了这个问题。