If you’re running a WooCommerce store, providing customers with real-time stock information on the cart page can enhance their shopping experience and help manage expectations. In this guide, we’ll show you how to display product stock at the WooCommerce cart page using custom code added to your theme’s functions.php file. This approach ensures that your store remains lightweight without the need for additional plugins.
If you’re unsure about editing your theme’s files, consider using our free WordPress child theme generator to create a child theme. This will protect your customisations from being overwritten during theme updates.
Step 1: Displaying Stock Quantity on the Cart Page
The first step is to modify the cart page so that it displays the available stock quantity for each product. Add the following code snippet to your theme’s functions.php file:
/**
* Snippet: How to Show Product Stock at WooCommerce Cart Page – 2026
* Author: John Cook
* URL: https://wcsuccessacademy.com/?p=1426
* Tested with WooCommerce 10.7.0
* "Display product stock quantity on the cart page"
*/
function wc_success_show_stock_cart($product_name, $cart_item, $cart_item_key) {
$product = $cart_item['data'];
if ($product->managing_stock() && !$product->is_on_backorder(1)) {
$stock_quantity = $product->get_stock_quantity();
$stock_text = sprintf(__('In stock: %s', 'woocommerce'), $stock_quantity);
$product_name .= sprintf('<p class="stock">%s</p>', $stock_text);
}
return $product_name;
}
add_filter('woocommerce_cart_item_name', 'wc_success_show_stock_cart', 10, 3);
This function hooks into the cart item name display and appends the current stock quantity below each product name in the cart.
Step 2: Customising Stock Messages for Low Stock Items
You might also want to inform your customers if the stock levels are running low. This can create a sense of urgency and potentially drive quicker purchase decisions. To do this, add the following code:
/**
* Snippet: How to Show Product Stock at WooCommerce Cart Page – 2026
* Author: John Cook
* URL: https://wcsuccessacademy.com/?p=1426
* Tested with WooCommerce 10.7.0
* "Display custom message for low stock items in the cart"
*/
function wc_success_low_stock_alert($product_name, $cart_item, $cart_item_key) {
$product = $cart_item['data'];
if ($product->managing_stock() && !$product->is_on_backorder(1)) {
$stock_quantity = $product->get_stock_quantity();
if ($stock_quantity <= 5) { // Adjust the threshold as needed
$stock_text = sprintf(__('Hurry! Only %s left in stock.', 'woocommerce'), $stock_quantity);
$product_name .= sprintf('<p class="low-stock">%s</p>', $stock_text);
}
}
return $product_name;
}
add_filter('woocommerce_cart_item_name', 'wc_success_low_stock_alert', 10, 3);
This snippet builds on the previous function by checking if the stock level is low (in this case, 5 or fewer items). If so, it displays a custom alert message encouraging customers to complete their purchase soon.
Step 3: Ensuring Stock Information Updates with Cart Changes
For an optimal user experience, ensure that the stock information updates dynamically if the customer adjusts the quantity of items in their cart. To achieve this, we’ll add an additional function that recalculates and updates the displayed stock when changes occur:
/**
* Snippet: How to Show Product Stock at WooCommerce Cart Page – 2026
* Author: John Cook
* URL: https://wcsuccessacademy.com/?p=1426
* Tested with WooCommerce 10.7.0
* "Update stock quantity display when cart is updated"
*/
function wc_success_refresh_cart_stock_info() {
if (is_cart()) {
wc_enqueue_js("
jQuery('body').on('updated_cart_totals', function() {
jQuery('.stock, .low-stock').each(function(){
var qty = jQuery(this).closest('.cart_item').find('.qty').val();
var stock_qty = parseInt(jQuery(this).text().match(/\d+/)[0]);
if (stock_qty <= qty) {
jQuery(this).closest('.cart_item').find('.stock, .low-stock').text('Out of stock');
}
});
});
");
}
}
add_action('wp_footer', 'wc_success_refresh_cart_stock_info');
This script ensures that when a customer changes the quantity of a product in their cart, the stock information is updated immediately. It checks if the selected quantity exceeds available stock and displays an “Out of stock” message if necessary.
Conclusion
By following these steps, you can easily show product stock at the WooCommerce cart page, helping your customers make informed purchasing decisions. Displaying real-time stock information not only enhances the customer experience but can also reduce the likelihood of cart abandonment.
To preserve your customisations and keep your site updated without hassle, we recommend using our free WordPress child theme generator. This will allow you to create a child theme and keep your changes intact during theme updates.
For additional customisations, you may find our other tools useful, such as the wp-config file generator, .htaccess file generator, robots.txt generator, and the bad bot blocker generator.
By displaying product stock at the WooCommerce cart page, you give your customers the transparency they need, which can improve trust and potentially increase sales. Happy coding!
I have been working with WordPress and WooCommerce since 2012 and have developed a deep knowledge of the content management system. Since 2012, I have developed several plugins and designed dozens of websites utilising different frameworks, CMS’s and programming languages. I am proficient in PHP, Python, Java, C, C++, R and JavaScript with limited experience in Go, Kotlin and Swift.
Educationally, I have a Master’s degree in cyber security a Bachelor’s (Hons, First Class) in Applied Research and a Graduate Certificate in Data Science. I’m currently undertaking PhD studies investigating IoT cybersecurity. I recently graduated with First Class Honours and Masters of Information Technology, receiving the Executive Dean’s Award for studies undertaken in the 2021 and 2022 academic years. I have worked in the information technology industry for the past 11 years primarily as a software/web developer specific to design, optimisation, network management and security. My research interests are in the areas of Internet of Things (IoT), 5G and Beyond Networks, information security for wireless networks and software development.
Stay In Touch

