Skip to content
Advertisement

Add variation stock quantity and status to Woocommerce product variation dropdown

Based on Show stock status next to each attribute value in WooCommerce variable products, I have the following code that shows stock quantity + stock status in product variation dropdown and also as displayed product availability text:

JavaScript

But I am having an issue with this code:
for ex. I have a product with size (stock): S (instock qty 2), L(0), XL(0). When I select variation S – it shows Quantity 2 – that is correct, but this same quantity is shown even when I select variation L or XL. – that is wrong because they are on ZERO. Can anybody help me with this? Thank you!

You can see it here: https://dogworld.cz/produkt/pelisek-pro-psa-reedog-beige-paw/

Advertisement

Answer

There are some mistakes in your code and a better way to shows stock quantity + stock status in product variation dropdown.

The first function is a custom function where you will define the stock text addition to be displayed in the product variation dropdown, which is handled by the second function.

In your last function, since Woocommerce 3, get_total_stock() is deprecated and replaced by the method get_stock_quantity(). Also you need to use the variation $product object that is included as an argument in the hooked function.

Note: This will only work for variable products with one dropdown (one defined product attribute for variations)

Here is the revisited code:

JavaScript

Code goes in function.php file of your active child theme (or active theme). Tested and works.

Based on How to add variation stock status to Woocommerce product variation dropdown

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement