Display Total Product Sold Quantity to WooCommerce Page

Display Total Product Sold Quantity to WooCommerce Page

Currently your customers have no idea how many quantity or downloads have been ordered from your WooCommerce site. So in order to apply this change, you can add below code into your child theme’s functions.php file.

This snippet code show you how to display total product sold quantity WooCommerce page.

<?php 
/* Display product sold count at product page */ 

add_action( 'woocommerce_single_product_summary', 'get_product_sold_count_at_product', 11 ); 

function get_product_sold_count_at_product() { 
   global $product;
   $units_sold = $product->get_total_sales();
   if ( $units_sold ) echo '<p>' . sprintf( __( 'Units Sold: %s', 'woocommerce' ), $units_sold ) . '</p>';
}

/* Display product sold count at shop page */ 

add_action( 'woocommerce_after_shop_loop_item', 'get_product_sold_count_at_shop', 9 );

function get_product_sold_count_at_shop() {
    global $product;
    $sales = $product->get_total_sales();
    ?>
    <p><?php echo "($sales items sold)"; ?></p>
   <?php
}

/*Display product sold count at cart and checkout page */

add_filter( 'woocommerce_cart_item_name', 'get_product_sold_count', 20, 3 );

function get_product_sold_count( $cart_item_name, $cart_item, $cart_item_key ){
	$product = $cart_item['data'];
	$sales = $product->get_total_sales();
	$text = __( "Total Sold:", "woocommerce" ) . ' ';

	return $cart_item_name . ' - ' . $text . $sales;
}

/*Display product sold count at order page and email notifications */

add_filter( 'woocommerce_order_item_name', 'add_single_excerpt_to_order_item', 10, 3 );

function add_single_excerpt_to_order_item( $item_name, $item, $is_visible ){
	$product = $item->get_product();
	$sales = $product->get_total_sales();
	$text = __( "Total Sold:", "woocommerce" ) . ' ';

	return $item_name . ' - ' . $text . $sales;
}

/*Display product sold count at admin order edit page */

add_action( 'woocommerce_before_order_itemmeta', 'total_sales_before_order_itemmeta', 10, 3 );

function total_sales_before_order_itemmeta( $item_id, $item, $product ){
    if( ! ( is_admin() && $item->is_type('line_item') ) ) return;

    echo '<p><em>' .__( "Total sales" ) . ': ' . $product->get_total_sales() . '</em></p>';
}
?>
Was this article helpful?
YesNo

Leave a comment

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