Shortcode: Product Sales Count

Return the total number of sales for a given product.

Example: [wc_product_sales_count product_id=”123″]

Code

Filename: gw-shortcode-product-sales-count.php

<?php
/**
 * Gravity Wiz // WooCommerce // Shortcode: Product Sales Count
 * https://gravitywiz.com/
 *
 * Return the total number of sales for a given product.
 *
 * Example: [wc_product_sales_count product_id="123"]
 */
add_shortcode( 'wc_product_sales_count', function( $atts ) {

	$atts = shortcode_atts(array(
		'product_id' => 0,
	), $atts);

	$product = wc_get_product( $atts['product_id'] );

	if ( $product ) {
		return $product->get_total_sales();
	} else {
		return 0;
	}

	return $order_count;
} );

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.