WooCommerce: Calculate Sales by State

You’re filling out your tax reports… and then find out WooCommerce doesn’t give you this calculation by default!

Don’t cry ๐Ÿ™‚ Today I’ll show you a quick snippet to calculate that in a second. Feel free to change the year, the country and the states in the snippet.

WooCommerce: get sales by state

PHP Snippet: Get Sales by State @ WooCommerce Admin (Reports)


/**
 * @snippet       Get Sales by State @ WooCommerce Admin
 * @how-to        Watch tutorial @ https://businessbloomer.com/?p=19055
 * @sourcecode    https://businessbloomer.com/?p=72853
 * @author        Rodolfo Melogli
 * @testedwith    WooCommerce 3.1.2
 */

// -----------------------
// 1. Create extra tab under Reports / Orders

add_filter( 'woocommerce_admin_reports', 'bbloomer_admin_add_report_orders_tab' );

function bbloomer_admin_add_report_orders_tab( $reports ) { 

$array = array(
    'sales_by_state' => array(
        'title' => 'Sales by state',
        'description' => '',
        'hide_title' => 1,
        'callback' => 'bbloomer_yearly_sales_by_state'
    )
);

$reports['orders']['reports'] = array_merge($reports['orders']['reports'],$array);

return $reports; 
}

// -----------------------
// 2. Calculate sales by state

function bbloomer_yearly_sales_by_state() {

	$year = 2017; // change this if needed
	$total_ca = $total_wa = 0; // add states if needed

    $args = [
        'post_type' => 'shop_order',
        'posts_per_page' => '-1',
		'year' => $year,
        'post_status' => ['wc-completed']
    ];
    $my_query = new WP_Query($args);
    $orders = $my_query->posts;

    foreach ($orders as $order => $value) {
   
	$order_id = $value->ID;
	$order = wc_get_order($order_id);
	$order_data = $order->get_data();

	if ( $order_data['billing']['country'] === 'US' ) {   
		if ( $order_data['billing']['state'] === 'CA' ) $total_ca += $order->get_total();
		if ( $order_data['billing']['state'] === 'WA' ) $total_wa += $order->get_total();
	}

    }

	echo "<h3>Sales by State for Year " . $year . "</h3>";
	echo "CA: " . wc_price($total_ca) . "</br>";
	echo "WA: " . wc_price($total_wa) . "</br>";

}

Where to add this snippet?

You can place PHP snippets at the bottom of your child theme functions.php file (before "?>" if you have it). CSS, on the other hand, goes in your child theme 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 free video tutorial "Where to Place WooCommerce Customization?"

Does this snippet (still) work?

Please let me know in the comments if everything worked 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 on PHP 7+.

If you think this code saved you time & money, please join other Business Bloomer supporters and avail of 365 days of WooCommerce benefits. Thank you in advance :)

Need Help with WooCommerce Customization?

Check out these free video tutorials!

  • how-to-edit-woocommerce-with-php-snippets
  • woocommerce-hooks-add_action-list-visual
  • woocommerce-customize-single-product-page-PHP

Rodolfo Melogli

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as a 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.

15 thoughts on “WooCommerce: Calculate Sales by State

  1. Awesome. This helped beyond measure!

    Took some time to add all 50 states to the array but otherwise, was excellent. If you do ever build a plugin for this setup so that users could just install and enter the date range, I would pay for that! ๐Ÿ™‚

    1. Thank you!

    2. Agreed Dave, I would absolutely pay for a plugin that did this.

  2. This is so helpful. Thank you! I have four states that I needed to pull information for, and this did the trick!

    How difficult would it be to display by month within the year totals? For instance, 2017 is already showing for the year – then filter down by month within the state totals like so:

    WA > Jan – $xxx; Feb – $xxx; Mar – $xxx | CA > Jan – $xxx; Feb – $xxx; Mar – $xxx and so on?

    1. Karen, thanks so much for your comment! Yes, this is totally possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R

  3. Hello, I am using Woocommerce Brand Plugin.

    Plugin Link: https://woocommerce.com/products/brands/

    I want to Get Sales by Brand Report. How can I do this ?

    Regards

    1. James, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R

  4. Cool Idea!

    What if i need to make this for Romania? Should i put RO to states?

    Because now it gives me an error.

    We put Romanian states using a plugin, but your snippet doesnt seem to work with that states http://prntscr.com/gnejer

    Can you direct me how to change? thanks

    1. Cristian, thanks for your comment! I believe I already helped you via Bloomer Armada support. Hope you managed to fix it ๐Ÿ™‚

  5. Works great! Thanks. How about a list of 10 states with most sales?

    1. Thank you ๐Ÿ™‚ Sure, that can be done, you will need to do some PHP edits ๐Ÿ™‚

  6. Every morning I wake-up I look forward to seeing your e-mail. Another awesome PHP snippet I’ve already added all the states and they work fabulously. Great work.

  7. Really nice excellent tutorial

Questions? Feedback? Support? Leave your Comment Now!
_____

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

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.