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.

 

[cc lang=”php”]/**
* snippet Show the number of products sold
* how-to Read at https://wcsuccessacademy.com/?p=11
* source code https://wcsuccessacademy.com/?p=11
* author John Cook
* tested with WooCommerce 3.3.3
*/
add_action( ‘woocommerce_single_product_summary’, ‘my_units_sold_count’, 18 );

function my_units_sold_count() {
global $product;
$units_sold = get_post_meta( $product->id, ‘total_sales’, true );
echo ‘

‘ . ‘‘ . sprintf( __( ‘Units Sold: %s’, ‘woocommerce’ ), $units_sold ) . ‘

‘;
}[/cc]

 

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.

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