How to Add a Custom Stock Progress Bar to Your WooCommerce Products?

Have you ever wanted to show your customers exactly how many products you have left in stock, but more visually and engagingly? Today, I’ll show you how to add a sleek progress bar to your WooCommerce product pages that displays stock levels. Don’t worry if you’re new to coding – I’ll break everything down into simple terms and show you the easiest way to implement this feature!

What Does This Code Do?

This code adds a neat visual progress bar to your WooCommerce product pages that shows how many items are left in stock. Instead of just showing a boring number, your customers will see:

  • A progress bar indicating stock levels
  • A friendly message showing the exact number of items remaining
  • Different messages for out-of-stock or backordered items

The best part? It automatically replaces WooCommerce’s default stock message with this more appealing visual indicator.

Here’s the result:

How the Code Works

Let’s break down what this code does in simple terms:

  1. First, it checks if you’re managing stock for the product
  2. If the product is out of stock or on backorder, it shows appropriate messages
  3. For in-stock products, it:
  • Shows how many items are available
  • Displays a progress bar (maxing out at 100 units for visual appeal)
  • Removes the default WooCommerce stock message to avoid duplication

How to Implement the Code

You have two ways to add this code to your site. Let’s look at both methods:

Method 1: Using functions.php

⚠️ Warning: This method requires more technical knowledge and can break your site if not done carefully!

  1. Connect to your site via FTP or file manager
  2. Navigate to your theme folder (wp-content/themes/your-theme)
  3. Open functions.php
  4. Add the code at the bottom of the file
  5. Save and upload the file

Method 2: Using the Code Snippets Plugin (Recommended!)

👍 This is the safer and easier method!

  1. Install and activate the “Code Snippets” plugin from WordPress.org
  2. Go to Snippets → Add New in your WordPress dashboard
  3. Give your snippet a title (e.g., “WooCommerce Stock Progress Bar”)
  4. Copy and paste the code into the code editor
  5. Select “Only run in administration area” as “No”
  6. Click “Save Changes and Activate”

Why Use Code Snippets Instead of functions.php?

The Code Snippets plugin is far superior to editing functions.php directly for several reasons:

Safety First:

  • There is no risk of breaking your site with syntax errors
  • Easy to deactivate if something goes wrong
  • You won’t lose your changes during theme updates

Better Organization:

  • Keep all your custom code in one place
  • Easy to manage multiple snippets
  • Add descriptions and tags for better organization

User-Friendly:

  • No FTP or file editing required
  • Simple interface for managing code
  • Built-in PHP validation

Video: How to Add a Custom Stock Progress Bar to Your WooCommerce Products?

Add a Custom Stock Progress Bar to Your WooCommerce Products: Use this code

This code implements a stock progress bar.

// Add custom stock progress bar and handle stock messages
add_action( 'woocommerce_before_add_to_cart_form', 'wpsh_stock_bar', 10, 0 );

function wpsh_stock_bar() {
	global $product;

	// Remove the default WooCommerce stock notice
	remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_meta', 40 );

	// Handle product stock status
	if ( ! $product->managing_stock() ) {
		return; // Don't show the progress bar if stock isn't being managed
	}

	// If the product is out of stock, display default "Out of stock" message
	if ( ! $product->is_in_stock() ) {
		echo '<p class="stock out-of-stock">' . esc_html__( 'Out of stock', 'woocommerce' ) . '</p>';
		return; // Stop further execution if the product is out of stock
	}

	// If the product is on backorder, display default "Available on backorder" message
	if ( $product->is_on_backorder() ) {
		echo '<p class="stock backorder">' . esc_html__( 'Available on backorder', 'woocommerce' ) . '</p>';
		return; // Stop further execution if the product is on backorder
	}

	// Get the current stock quantity and ensure the progress bar reflects it
	$stock_quantity = $product->get_stock_quantity();
	$max_stock = 100; // Define a maximum stock limit for the progress bar

	// Ensure $stock_quantity doesn't exceed the defined maximum stock
	$progress_value = min( $stock_quantity, $max_stock );

	// Output the custom stock progress bar with appropriate styling and alignment
	echo '<div class="wpsh-stock-bar">';
	echo esc_html( 'Only ' . $stock_quantity . ' in stock!' ) . '<br>';
	echo '<progress max="' . esc_attr( $max_stock ) . '" value="' . esc_attr( $progress_value ) . '" style="width: 100%;"></progress>';
	echo '</div>';
}

// Hide the default stock notice completely if the progress bar is displayed
add_filter( 'woocommerce_get_availability', 'wpsh_hide_default_stock_notice', 10, 2 );

function wpsh_hide_default_stock_notice( $availability, $product ) {
	// If stock is being managed and product is in stock, hide the default stock message
	if ( $product->managing_stock() && $product->is_in_stock() && ! $product->is_on_backorder() ) {
		$availability['availability'] = ''; // Empty the availability message
	}

	return $availability;
}

To style your stock progress bar, you’ll need to add some CSS code. Navigate to Appearance >> Customize >> Additional CSS and insert the CSS code provided below. If you’d like to use your own colors, simply adjust the “background-color” section as needed.

.wpsh-stock-bar {
			text-align: left;
			display: block;
			margin-bottom: 10px;
			margin-top: 10px;
		}

		/* Customize the progress bar's filled portion for WebKit browsers (Chrome, Safari, Edge) */
		.wpsh-stock-bar progress::-webkit-progress-value {
			background-color: #ffcc00; /* Yellow for the filled part */
		}

		/* Customize the background color for WebKit browsers */
		.wpsh-stock-bar progress::-webkit-progress-bar {
			background-color: #ccc; /* Light grey for the unfilled part */
		}

		/* Customize the progress bar's filled portion for Firefox */
		.wpsh-stock-bar progress::-moz-progress-bar {
			background-color: #ffcc00; /* Yellow for the filled part */
		}

		/* For browsers that don't support progress bar styling, apply default background */
		.wpsh-stock-bar progress {
			background-color: #ccc;
			border: none;
			width: 100%;
			max-width: 300px;
			height: 20px;
			overflow: hidden;
		}

Give It a Try!

Adding a stock progress bar to your WooCommerce products is a great way to enhance your store’s user experience. By using the Code Snippets plugin, you can implement this feature safely and easily, even if you’re not a coding expert.

Remember to test the feature on a development site first or during low-traffic hours. Once implemented, you’ll give your customers a much better visual indication of product availability!


Pro Tip: After adding the code, visit one of your products that has stock management enabled to see the progress bar in action. You can adjust the maximum stock display (currently set to 100) by changing the $max_stock variable in the code to better suit your needs.

Do you want to thank me and buy me a beer?

Every donation is entirely welcome but NEVER required. Enjoy my work for free but if you would like to thank me and buy me a beer or two then you can use this form here below.

Donation Form (#2)

Here are some of my favorite WordPress tools

Thanks for reading this article! I hope it's been useful as you work on your own websites and e-commerce sites. I wanted to share some tools I use as a WordPress developer, and I think you'll find them helpful too.

Just so you know, these are affiliate links. If you decide to use any of them, I'll earn a commission. This helps me create tutorials and YouTube videos. But honestly, I genuinely use and recommend these tools to my friends and family as well. Your support keeps me creating content that benefits everyone.

Themes: Over the past few years, I've consistently relied on two primary themes for all sorts of projects: the Blocksy theme and the Kadence Theme. If you explore this website and my YouTube channel, you'll come across numerous tutorials that delve into these themes. If you're interested in obtaining a 10% discount for both of these themes, then:

Code Snippets Manager: WPCodeBox allows you to add code snippets to your site. Not only that, but it also provides you with the capability to construct and oversee your WordPress Code Snippets library right in the cloud. You can grab it with the 20% discount here (SAVE 20% Coupon: WPSH20).

Contact forms: There are hundreds of contact forms out there but Fluent Forms is the one I like the most. If you need a 20% discount then use this link (save 20% coupon is WPSH20).

Gutenberg add-ons: If I need a good Gutenberg blocks add-on then Kadence Blocks is the one I have used the most. You’ll get a 10% discount with the coupon SIMPLEHACKS here.

Website migration: While building a website you probably need a good plugin that can help you with the migration, backups, restoration, and staging sites. Well, WpVivid is the one I have used for the last couple of years. If you use this link along with the WPSH20 coupon you’ll get a 20% discount.

Woocommerce extensions: There are a bunch of Woocommerce extensions that I like but the one that stands out is Advanced Dynamic Pricing. Once again, you’ll get a 20% discount if you use this link here (save 20% coupon is WPSH20)

Web Hosting: If you would like to have a really fast and easy-to-use managed cloud hosting, then I recommend Verpex Hosting (see my review here). By the way, this site is hosted in Verpex.)

To see all my most up-to-date recommendations, check out this resource that I made for you!

Janek T.
Janek T.

Improve this text: {CLIPBOARD}

- I have been passionate about Wordpress since 2011, creating websites and sharing valuable tips on using Wordpress and Woocommerce on my site.
- Be the first to receive notifications about new tutorials by subscribing to my Youtube channel .
- Follow me on Twitter here

Articles: 134