For our first post we thought that we would show you how to display the total number of products sold on a per product basis. This can be a handy little trick to show social proof for a popular selling product. If potential buyers can see that an item in your store is selling well it will help to encourage them to buy that item.
This function makes use of the built-in units sold custom field within Woocommerce. As each unit is sold it increases the count by one, and will be reflected on the front end of the product.
* snippet Show the number of products sold
* how-to Read at http://wcsuccessacademy.com/?p=11
* source code http://wcsuccessacademy.com/?p=11
* author John Cook
* tested with WooCommerce 8.8.2
*/
add_action( 'woocommerce_single_product_summary', 'wcsuccess_units_sold_count', 18 );
function wcsuccess_units_sold_count() {
global $product;
// get the total number of sales
$count = $product->get_total_sales();
// Define the message to display (Modify as needed)
$text = sprintf( _n( '%s Sale', '%s Total sales', $count, 'woocommerce' ), number_format_i18n($count));
// Only output the message if the total number of sales are over 100 (you can modify as needed)
if($count > 100) {
echo '<p class="total-sales"><span class="dashicons dashicons-chart-line"></span> ' . $text . '</p>';
}
}
You’ll see in this code snippet that I have included a Font Awesome icon at the beginning of the units sold count. This is optional, but I believe that it gives a more appealing and professional look. Feel free to change the icon to any other icon, or none at all.
This is what it will look like on the product page
Where does this code go?
Place this code at the bottom of your child theme functions.php file (before “?>” if you have it), or into a functions plugin. Please use caution when editing these delicate files, and if necessary make a copy of the file before making any changes if you are unfamiliar with these edits. Even copying and pasting into a text document will give you a safe copy as a backup. We recommend using a functions plugin or a child theme for any theme customizations.
Is it working for you?
Let me know if this code snippet for how to show the number of products sold in Woocommerce on the product page is working for you in the comments below. You can customise this code further by showing a different message depending on the total number sold.
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.
Google Scholar ID: https://scholar.google.com.au/citations?user=0KvpwNEAAAAJ&hl=en
ORCID: https://orcid.org/0000-0001-8113-1296
Stay In Touch
Hi, can you add the code to exclude the products if no sales appear from the front end? This means it will only appear if the product has sale/sold. And is this code can be used for variable products?
You sure can. You can also set timeframes and create different messages depending on how many have been sold based on the time you set. For example, you may set a rule showing 15 sold this hour, or 45 in the last 24 hours.