IN woocommerce I would like to display the total stock of all variations in a variable product. The stock of each product variation is managed at the product variation level.
If I use for a variable product:
global $product;
echo $product->get_stock_quantity();
I don't get the total stock of all variations of this product.
Is it possible to get the total stock of all variations from a variable product In Woocommerce?
Any help is appreciated.
The following custom function will displays the sum of all product variations stock quantity for a variable product (only).
It use a very light SQL query that make the sum of all children variations from a variable product.
Code goes in function.php file of your active child theme (or active theme). Tested and works.
USAGE (3 cases):
1) Anywhere in php code, get the stock quantity (from a dynamic
$product_id
):2) Display in shop and archives pages (Under the price range example):
Code goes in function.php file of your active child theme (or active theme).
3) Display in single product pages (Under the price range example):
Code goes in function.php file of your active child theme (or active theme).