WooCommerce stores in the database the quantity of products sold. Displaying the number of products sold on the product page itself can increase the conversion rate of sales on your site. Let’s see how to display the quantity of products sold next to the “Add to Cart” button in WooCommerce stores.
Note that displaying this number can be detrimental if there are not many purchases for a specific product. Therefore, we will provide a way for you to display this data only if the number of sales is higher than a certain number.
This is a short and focused post, so let’s get straight to the point. Add the following code to the functions.php
file of your child theme:
function sv_product_sold_count() {
global $product;
$products_sold = get_post_meta( $product->get_id(), 'total_sales', true );
if ( $products_sold > 50 ) {
echo '<p class="units-sold">The product has been purchased ' . $products_sold . ' times</p>';
}
}
add_action( 'woocommerce_single_product_summary', 'sv_product_sold_count', 11 );
Pay attention to line number 7. In this line, we have decided that if the sales quantity is more than 50 products, the quantity of products sold should be displayed – otherwise, this data will not be shown. Of course, you can change this number.
It is of course possible to change the location of the appearance of this data by using other hooks available to us in WooCommerce.