2
votes

in woocommerce I changed the "In Stock" text to show a different text for different stock numbers. So for stock numbers above 3 it sais: " 5 in stock" (colored green with css) and for stock numbers 3 or below it says "only 2 left" (colored red with css). Just like amazon does it.

This works perfectly with simple products as you can see here: https://edelmix.de/superdry-urban-blau-limette-quarzuhr-syg164un/

But on variable products my code doesn't have the desired result. Which is of course to show the same text as described above with the proper stock-quantity of the variation.

What I get is a text with the total quantity of all variations. I turned on manage product quantity at product level in the backend for all the variations and have different stock quantities on the variations.

The result on the variable product as of now can be seen here: https://stage.edelmix.de/buddha-to-buddha-blue-lace-agate-ring-603ba/

The quantity shown is 5 (after you select the variation from the dropdown). Which is the total quantity for all variations all together. The different ring-sizes have a stock quantity of 1/3/1 (as you can see from my test echoing the stock-variable next to the price), and this is what I want to be shown when each variation is selected (with the custom text as it is with the simple products).

My Code so far is:

add_filter( 'woocommerce_get_availability', 'custom_get_availability', 1, 2);

function custom_get_availability( $availability, $_product ) {
    global $woocommerce, $product;
    if ($_product->is_type( 'variable' ))  {
        $available_variations = $product->get_available_variations();
        foreach ($available_variations as $variation) 
            {
                $variation_id = $variation['variation_id'];
                $variation_obj = new WC_Product_Variation($variation_id);
                $stock = $variation_obj->get_stock_quantity();
                echo $stock;  // <-- for testing purpose to see if I do get the individual variation-quantities = works!!
            }
        } else {
            $stock = $product->get_stock_quantity();
        }

    // change text "In Stock' to 'Auf Lager' when quantity more than 3
    if ($_product->is_in_stock() && $stock > 3 ) $availability['availability'] = $stock . ' ' .__('auf Lager', 'woocommerce');

    // change text to n Left, where n is the quantity
    if ($_product->is_in_stock() && ($stock <= 3 )) $availability['availability'] = '<p class="stock ed_low_stock ' . esc_attr( $availability['class'] ) . '">' .  __('Nur noch ' . $stock . ' auf Lager!', 'woocommerce') . '</p>';  

    // change text "Out of Stock' to 'SOLD OUT'
    if (!$_product->is_in_stock()) $availability['availability'] = __('zzt. nicht verfügbar!', 'woocommerce');

    // change text "In Stock' to 'Special Order' for products with unmanaged stock
    if ( !$_product->managing_stock() && $_product->is_in_stock() ) $availability['availability'] = __('Auf Lager', 'woocommerce');

  return $availability;
}

Why doesn't this work?

I have tried everything during the last hours and do not know why this does not work as it should.

Any help is much appreciated.

1
Well, that's partly correct, but does not answer my question.Visualpro
Sorry… I have check and tested your code. I have found what was wrong… See the answer below.LoicTheAztec

1 Answers

2
votes

It was not working because you have made complications for nothing. In the hooked function the $product argument is the current selected Product variation (in a variable product), so you don't need any foreach loop to get the variations from the Variable product.

So your code is going to be more compact now. Try this instead:

add_filter( 'woocommerce_get_availability', 'custom_get_availability', 1, 2);
function custom_get_availability( $availability, $_product ) {
    $stock = $_product->get_stock_quantity();

    // change text "In Stock' to 'Auf Lager' when quantity more than 3
    if ( $_product->is_in_stock() && $stock > 3 ) $availability['availability'] = $stock . ' ' .__('auf Lager', 'woocommerce');

    // change text to n Left, where n is the quantity
    if ( $_product->is_in_stock() && $stock <= 3 ) $availability['availability'] = '<p class="stock ed_low_stock ' . esc_attr( $availability['class'] ) . '">' .  __('Nur noch ' . $stock . ' auf Lager!', 'woocommerce') . '</p>';

    // change text "Out of Stock' to 'SOLD OUT'
    if (! $_product->is_in_stock() ) $availability['availability'] = __('zzt. nicht verfügbar!', 'woocommerce');

    // change text "In Stock' to 'Special Order' for products with unmanaged stock
    if ( ! $_product->managing_stock() && $_product->is_in_stock() ) $availability['availability'] = __('Auf Lager', 'woocommerce');

    return $availability;
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

Tested and working