Get Product Stock Quantity
In this tutorial I will not only show you how to get stock quantity in WooCommerce (of a product or a product variation by the way) but we will also display it on the shop page. Like this:

How to Get Stock Quantity
Let’s echo WooCommerce product stock quantity of a particular product ID.
$product_id = 55; // ID of a product or a variation
$product = wc_get_product( $product_id );
$stock_quantity = $product->get_stock_quantity();
// now we can print product stock quantity or do whatever
echo "In stock: $stock_quantity";
Worth noting that stock quantity is only available for products with “Manage Stock” turned on.

The same applies for product variations:

Knowing that, let’s make some changes in our code.
$product = wc_get_product( $product_id );
// check if stock is managed on a product level
if( $product->get_manage_stock() ) {
$stock_quantity = $product->get_stock_quantity();
// now we can print it or do whatever
echo "In stock: $stock_quantity";
} else {
$stock_status = $product->get_stock_status();
if( 'instock' === $stock_status ) {
echo 'In stock';
}
if( 'outofstock' === $stock_status ) {
echo 'None of this product left...';
}
// there is also "onbackorder" value can be returned
}
Yes, I forgot to mention, the same code works for product variations as well. So as $product_id
you can pass either ID of a product or ID of a variation.
You can also change product stock quantity with $product->set_stock_quantity()
, $product->set_manage_stock()
methods or use my Simple Inventory Sync plugin if you would like to configure stock synchronization between multiple WooCommerce stores.
Get stock quantity when looping through items in a cart
foreach( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$stock_quantity = $cart_item[ 'data' ]->get_stock_quantity();
}
Get stock quantity of a product with WooCommerce REST API
$request = wp_remote_get(
"https://YOUR-SITE/wp-json/wc/v3/products/$product_id",
array(
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( "$login:$pwd" )
)
)
);
if( 'OK' === wp_remote_retrieve_response_message( $request ) ) {
$product = json_decode( wp_remote_retrieve_body( $request ) );
echo "In stock: {$product->stock_quantity}";
}
- In order to work with WooCommerce REST API you will need credentials to connect to it. You can either use an application password or create a consumer key and consumer secret in WooCommerce settings.
- If you need to get a stock quantity of a product variation you can to use another API endpoint
/wp-json/wc/v3/products/$product_id/variations/$variation_id
. - You can also
print_r( $product )
to check what is available in REST API response.
Display Stock Quantity on Shop Page
You can find tons of tutorials that suggest you to use wc_get_stock_html()
function.
add_action( 'woocommerce_after_shop_loop_item', function() {
global $product;
echo wc_get_stock_html( $product );
} );
This snippet is just displaying the product availability message on the shop page like it is displaying on single product pages.

But let’s do something more interesting using get_stock_quantity()
, get_manage_stock()
and get_stock_status()
methods of WC_Product
object. For example let’s just display a message “Only 1 left in stock!”, I am pretty sure it will help with sales.
add_action( 'woocommerce_after_shop_loop_item', function() {
global $product;
if( $product->get_manage_stock() && 1 === $product->get_stock_quantity() ) {
echo '<p class="stock stock-1-left">Only 1 left in stock – order soon</p>';
}
} );
And some CSS: .stock-1-left{ color: #eaa600 }
, here we are:


Misha Rudrastyh
Hey guys and welcome to my website. For more than 10 years I've been doing my best to share with you some superb WordPress guides and tips for free.
Need some developer help? Contact me
Hi Misha,
I’m having a bit of trouble with the “only-1 left in stock” function. When I use that, instead of the regular function you listed at the top of the page, it only displays for the products with 1 unit in the inventory, and does not display anything at all for items with either plenty in stock, or those marked out of stock. Any idea why that might be?
Thanks.