Home » WooCommerce: Calculate Sales by Product ID (Shortcode)

WooCommerce: Calculate Sales by Product ID (Shortcode)

by Tutor Aspire

Let’s say you’re developing a custom sales page or a landing page. A great way to increase your conversion rate is by showing the number of purchases close to the “add to cart” button.

We’ve already seen how to do this on the single product page, but what if you need to show this on a custom page, and therefore you need a shortcode?

Well, this is super easy and I’m currently using the snippet below on my own website, and specifically in the pricing table of my #CustomizeWoo online course sales page. So, here you go – enjoy!

A shortcode to display the number of product sales based on the WooCommerce product ID

PHP Snippet: Display Product ID Total Sales Anywhere (Shortcode)

Simply use [sales id=”123″] where “123” is your product ID to show the number of product sales

/**
 * @snippet       Product Sales by ID - WooCommerce Shortcode
 * @how-to        Get tutoraspire.com FREE
 * @author        Tutor Aspire
 * @compatible    WooCommerce 3.6.4
 * @donate $9     https://tutoraspire.com
 */

add_shortcode( 'sales', 'tutoraspire_sales_by_product_id' );
  
function tutoraspire_sales_by_product_id( $atts ) {
    
$atts = shortcode_atts( array(
'id' => ''
), $atts );

$units_sold = get_post_meta( $atts['id'], 'total_sales', true );
    
return $units_sold;
    
}

You may also like