WooCommerce: Get Total Sales By Product Category

It’s easy enough in WooCommerce to get/calculate product sales. What’s difficult, on the other hand, is calculating the total amount of sales for a specific category, because there is no core function that already does that.

Why sales by category – you may ask? Well, to me, that’s a very important metric. For example, I sell both consulting and non-consulting products on this same website, so it’s important for me to keep track of category sales year-on-year, especially when my goal is reducing 1-to-1 client work while increasing scalable product sales such as courses, plugins and memberships.

In this quick tutorial, we will first get the “WooCommerce orders that contain a target product category”, and after that we will loop through the array to calculate the total sales for that specific category. Sounds difficult? No worries – just copy and paste the snippets below.

Pity that the product categories dashboard page doesn’t give you an idea of the total spent by category… let’s find a way to calculate that! You can then print the total where you wish: on the frontend single product category page in case that can help customers, or in the backend, for example in the single product category edit page!

PHP Snippet: Get Total Purchase Amount For a Specific WooCommerce Product Category

 * @snippet       Calculate Product Category Sales Amount
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 7
 * @donate $9     https://businessbloomer.com/bloomer-armada/


function bbloomer_get_orders_by_product_cat( $cat_slug, $order_status = array( 'wc-completed' ) ) {
    global $wpdb;
	 $args = array(
		 'limit' => -1,
	 	 'status' => 'publish',
		 'return' => 'ids',
		 'category' => array( $cat_slug ),
	 $product_ids = wc_get_products( $args );
    $results = $wpdb->get_col( "
        SELECT order_items.order_id
        FROM {$wpdb->prefix}woocommerce_order_items as order_items
        LEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta as order_item_meta ON order_items.order_item_id = order_item_meta.order_item_id
        LEFT JOIN {$wpdb->posts} AS posts ON order_items.order_id = posts.ID
        WHERE posts.post_type = 'shop_order'
        AND posts.post_status IN ( '" . implode( "','", $order_status ) . "' )
        AND order_items.order_item_type = 'line_item'
        AND order_item_meta.meta_key = '_product_id'
        AND order_item_meta.meta_value IN ( '" . implode( "','", $product_ids ) . "' )
    " );
    return $results;


function bbloomer_cat_sales( $cat_slug ) {
	$orders = bbloomer_get_orders_by_product_cat( $cat_slug );
	$total = 0;
	foreach ( $orders as $order_id ) {
		foreach ( wc_get_order( $order_id )->get_items() as $key => $item ) {
			$product_id = $item->get_product_id();
			if ( ! $product_id ) continue;
			if ( has_term( $cat_slug, 'product_cat', $product_id ) ) $total += $item->get_total();
   return wc_price( $total );

// 3. USAGE

echo bbloomer_cat_sales( 'tables' );

Where to add custom code?

You should place PHP snippets at the bottom of your child theme functions.php file and CSS at the bottom of its style.css file. Make sure you know what you are doing when editing such files - if you need more guidance, please take a look at my guide "Should I Add Custom Code Via WP Editor, FTP or Code Snippets?" and my video tutorial "Where to Place WooCommerce Customization?"

Does this snippet (still) work?

Please let me know in the comments if everything went as expected. I would be happy to revise the snippet if you report otherwise (please provide screenshots). I have tested this code with Storefront theme, the WooCommerce version listed above and a WordPress-friendly hosting.

If you think this code saved you time & money, feel free to join 17,000+ WooCommerce Weekly subscribers for blog post updates and 250+ Business Bloomer supporters for 365 days of WooCommerce benefits. Thank you in advance!

Need Help with WooCommerce?

Check out these free video tutorials. You can learn how to customize WooCommerce without unnecessary plugins, how to properly configure the WooCommerce plugin settings and even how to master WooCommerce troubleshooting in case of a bug!

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza.

4 thoughts on “WooCommerce: Get Total Sales By Product Category

  1. This code not providing correct total sales for one particular category

    1. It only counts “completed” orders, maybe that’s the issue?

      1. No, I have tested woo-commerce analytics reports only for completed orders, the above code given amount and analytics amount have big mis match

        1. It could be a WooCommerce Analytics sync problem, too! Lol. Anyway, it’s difficult to say without taking a look at your website backend. My code works on my test website so not sure I’m afraid

Questions? Feedback? Support? Leave your Comment Now!

If you are writing code, please wrap it between shortcodes: [php]code_here[/php]. Failure to complying with this (as well as going off topic, not writing in English, etc.) will result in comment deletion. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining BloomerArmada to get blog comment reply priority, ask me 1-to-1 WooCommerce questions and enjoy many more perks. Thank you :)

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