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.
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_percentageto 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.
Example Workflow
- Product Setup: On the product edit page, enter the original stock quantity in the Total Stock Quantity field.
- 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.
- 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.
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

