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 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 '
<p style="font-size: 1.1em; font-weight: 900; color: #908c8c;">' . '<i class="fa fa-line-chart" style="margin-right: 10px;"></i>' . sprintf( __( 'Units Sold: %s', 'woocommerce' ), $units_sold ) . '</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.