4
votes

I want to get count of total number of items purchased on my wordpress site and display this number somewhere. I am using woocomerce 1.6.6 plugin and i want some shortcode or function or db query to do this. Please help.

1

1 Answers

4
votes

I realise this question is well over a year old, but for anyone who stumbles upon it, like I just did, here's how to get the total number of sales of all products in Woocommerce. I needed this for a project since for each product purchased, one is to be donated to people in need. Hence, I wanted to show the total number of donations (= sales).

Each product has a total_sales meta field, so all we need to do is query the database for the sum of all products' total_sales:

function get_number_of_sales() {
  global $wpdb;

  $result = $wpdb->get_row("
      SELECT SUM(pm.meta_value) AS total_sales
      FROM $wpdb->posts AS p
      LEFT JOIN $wpdb->postmeta AS pm ON (p.ID = pm.post_id AND pm.meta_key = 'total_sales') 
      WHERE p.post_type = 'product'
  ");

  return $result->total_sales;

}

Based on this answer.