15

I'm developing a theme for wordpress and 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.

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.

Dharman
  • 21,838
  • 18
  • 57
  • 107
SeiyaJapon
  • 464
  • 1
  • 6
  • 19

5 Answers5

11

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.

Artjom B.
  • 58,311
  • 24
  • 111
  • 196
db306
  • 536
  • 6
  • 15
2

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.

$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

Now I can send my array over to jQuery.

Please clarify if I'm mis-understanding the question.

Tuesdave
  • 621
  • 2
  • 8
  • 25
2

The below code will print all attributes with 'is_in_stock' variable.

<?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;
?>
Faisal Alvi
  • 472
  • 4
  • 20
1

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.

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.

SeiyaJapon
  • 464
  • 1
  • 6
  • 19
0
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.