How to show the number of products sold in Woocommerce on the product page

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.

See also  How to add Google fonts to WordPress

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.

5 1 vote
Article Rating

Stay In Touch

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

Subscribe
Notify of
guest
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Wani
Wani
August 13, 2021 1:16 pm

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?

2
0
Would love your thoughts, please comment.x
()
x
Scroll to Top
×