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:

displaying only one left in stock on WooCommerce shop page

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.

WooCommerce stock quantity setting when manage stock is on
So, when “Manage stock level” checkbox is checked, “Stock quantity” is displayed.

The same applies for product variations:

WooCommerce variation stock quantity when stock management is enabled

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}";
}

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.

Display product stock quantity on WooCommerce shop page
If you are wondering why the product prices are in ETH, you might be interested in my another tutorial about creating MetaMask payment gateway for WooCommerce.

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:

displaying only one left in stock on WooCommerce shop page
Misha Rudrastyh

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

Follow me on X