How to Display Percentage Sold of a Product in WooCommerce – 2026

Last updated on November 20th, 2024 at 09:32 am

Showing the percentage of stock already sold is a great way to create urgency and encourage customers to buy before the stock runs out. This feature can be particularly effective for products with limited stock, using a message like “96.4% already sold” or “Only 3.6% of stock remaining.”

In this guide, we’ll set up a display that shows the percentage of stock sold once a defined threshold is met. We’ll use a custom field to store the original stock quantity, allowing WooCommerce to calculate and display the percentage sold.


Step 1: Add a Custom Field for Total Stock Quantity

The first step is to add a custom field where you can set the original total quantity of stock for each product. This field will be used as a reference to calculate the percentage sold.

Code to Add Custom Stock Field

Add the following code to your theme’s functions.php file or a child theme:

/*
 * Snippet: How to Display Percentage Sold of a Product in WooCommerce – 2026
* Author: John Cook
* URL: https://wcsuccessacademy.com/?p=1647
* Tested with WooCommerce 10.7.0
* "This function adds a custom field for original stock quantity to the product page"
*/ function wcsuccess_add_total_stock_field() { woocommerce_wp_text_input( array( 'id' => '_total_stock_quantity', 'label' => __( 'Total Stock Quantity', 'woocommerce' ), 'type' => 'number', 'description' => __( 'Enter the original total quantity of stock for this product.', 'woocommerce' ), 'desc_tip' => true, 'custom_attributes' => array( 'min' => '1', 'step' => '1' ) )); } add_action( 'woocommerce_product_options_inventory_product_data', 'wcsuccess_add_total_stock_field' ); /* * Snippet: How to Display Percentage Sold of a Product in WooCommerce – 2026
* Author: John Cook
* URL: https://wcsuccessacademy.com/?p=1647
* Tested with WooCommerce 10.7.0
* "This function saves the total stock quantity field"
*/ function wcsuccess_save_total_stock_field( $post_id ) { $total_stock = isset( $_POST['_total_stock_quantity'] ) ? intval( $_POST['_total_stock_quantity'] ) : ''; if ( $total_stock ) { update_post_meta( $post_id, '_total_stock_quantity', $total_stock ); } } add_action( 'woocommerce_process_product_meta', 'wcsuccess_save_total_stock_field' );

Explanation:

  • Custom Field: Adds an input field on the product edit page where you can set the initial stock quantity.
  • Data Storage: The value is saved as custom meta data for the product, allowing it to be used for calculations.
See also  Programmatically add the year to your WordPress post and product titles 2026

Step 2: Display the Percentage Sold or Remaining Stock

Now we’ll calculate and display the percentage sold or remaining based on the initial stock quantity and the current stock status. We’ll display this information on the product page only if the percentage sold meets a specified threshold.

Code to Display Percentage Sold

Add this code to functions.php:

/*
 * Snippet: How to Display Percentage Sold of a Product in WooCommerce – 2026
* Author: John Cook
* URL: https://wcsuccessacademy.com/?p=1647
* Tested with WooCommerce 10.7.0
* "This function displays percentage sold or remaining if threshold is met"
*/ function wcsuccess_display_percentage_sold() { global $product; $total_stock = get_post_meta( $product->get_id(), '_total_stock_quantity', true ); $current_stock = $product->get_stock_quantity(); // Define the threshold percentage (e.g., 50% sold) $threshold_percentage = 50; if ( $total_stock && $current_stock !== null ) { $sold_quantity = $total_stock - $current_stock; $percentage_sold = ($sold_quantity / $total_stock) * 100; $percentage_remaining = 100 - $percentage_sold; // Only display if the percentage sold meets or exceeds the threshold if ( $percentage_sold >= $threshold_percentage ) { echo '<div class="percentage-sold" style="font-size: 18px; font-weight: bold; color: #e74c3c;">'; echo sprintf( __( '%.1f%% already sold', 'woocommerce' ), $percentage_sold ); echo '</div>'; } elseif ( $percentage_remaining <= 100 - $threshold_percentage ) { echo '<div class="percentage-remaining" style="font-size: 18px; font-weight: bold; color: #27ae60;">'; echo sprintf( __( 'Only %.1f%% of stock remaining!', 'woocommerce' ), $percentage_remaining ); echo '</div>'; } } } add_action( 'woocommerce_single_product_summary', 'wcsuccess_display_percentage_sold', 25 );

Explanation:

  • Calculation: The function calculates the quantity sold and converts it into a percentage of the initial stock.
  • Threshold Display: Only displays the percentage sold or remaining if it meets the specified threshold.
  • Custom Messages: Shows either the percentage sold or the remaining stock based on which is more effective for creating urgency.

Notes:

  • Custom Threshold: Change $threshold_percentage to adjust when the message appears (e.g., 50% or 75% sold).
  • Custom Styling: You can adjust colours, font size, and styles in the CSS section for each message.

Step 3: Styling the Display

For a visually appealing display, add the following CSS to your theme’s style.css file or custom CSS section.

.percentage-sold {
    background-color: #fde3e3;
    padding: 10px;
    border-radius: 5px;
    color: #e74c3c;
}

.percentage-remaining {
    background-color: #e3fbe3;
    padding: 10px;
    border-radius: 5px;
    color: #27ae60;
}

Explanation:

  • Distinct Colours: A red background for “sold” and green for “remaining” to visually communicate urgency.
  • Padding and Borders: Provides a polished look to make the message stand out.
See also  How to Use JavaScript for Live Price Updates in WooCommerce Variations - 2026

Example Workflow

  1. Product Setup: On the product edit page, enter the original stock quantity in the Total Stock Quantity field.
  2. Display Threshold: If the product reaches or exceeds the defined threshold (e.g., 50% sold), the percentage sold or remaining will be displayed on the product page.
  3. Real-Time Update: WooCommerce will automatically calculate and display the correct percentage as stock levels change.

Conclusion

Adding a percentage sold display to WooCommerce without a plugin creates a sense of urgency and can increase conversions. By using a custom field and calculated display, you can effectively inform customers about limited stock availability and motivate them to make a purchase.

Test the code on a staging environment before deploying it to your live store. For more customisation options, check out our WooCommerce Visual Hooks Guide or our wp-config generator to streamline configurations.

0 0 votes
Article Rating

Stay In Touch

Was this post helpful? Why not show your support and buy me a coffee?

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Scroll to Top
0
Would love your thoughts, please comment.x
()
x
×