How to Calculate Sales by State in WooCommerce – 2026

Last updated on September 26th, 2024 at 08:48 am

Understanding where your customers are coming from and which states generate the most revenue can be crucial for optimising your marketing strategy and planning stock. WooCommerce offers various analytics options, but calculating sales by state isn’t immediately available out of the box. Fortunately, you can easily generate these reports with custom code, directly from your WooCommerce data.

In this guide, we’ll show you how to calculate sales by state in WooCommerce using a simple custom function. Adding this to your WooCommerce store will give you key insights into your best-performing regions, allowing you to tailor your sales efforts accordingly.

Why Calculate Sales by State?

Knowing where your sales are coming from can help you in multiple ways:

  • Targeted Marketing: Focus marketing efforts on states where you have the most potential.
  • Stock Distribution: Plan your stock shipments according to demand from different regions.
  • Tax Reporting: Easily calculate taxes and financial information by state, making compliance more efficient.

Adding Custom Code to Calculate Sales by State

You can use the following code snippet to calculate total sales by state in WooCommerce. As always, it’s a good idea to use a child theme when adding custom code, so your changes are preserved after theme updates.

/*
 * Snippet: How to Calculate Sales by State in WooCommerce – 2026
* Author: John Cook
* URL: https://wcsuccessacademy.com/?p=1498
* Tested with WooCommerce 10.7.0
* "This function calculates total sales by state in WooCommerce"
*/ function wcsuccess_calculate_sales_by_state( $state_code ) { global $wpdb; $results = $wpdb->get_results( $wpdb->prepare(" SELECT SUM(meta.meta_value) as total_sales, COUNT(posts.ID) as order_count FROM {$wpdb->prefix}posts as posts JOIN {$wpdb->prefix}postmeta as meta ON posts.ID = meta.post_id JOIN {$wpdb->prefix}woocommerce_order_items as items ON posts.ID = items.order_id JOIN {$wpdb->prefix}woocommerce_order_itemmeta as itemmeta ON items.order_item_id = itemmeta.order_item_id WHERE posts.post_type = 'shop_order' AND posts.post_status IN ('wc-completed') AND itemmeta.meta_key = '_billing_state' AND itemmeta.meta_value = %s AND meta.meta_key = '_order_total' ", $state_code )); if ( !empty( $results ) ) { return array( 'total_sales' => $results[0]->total_sales, 'order_count' => $results[0]->order_count ); } return false; } // Example usage: $sales_data = wcsuccess_calculate_sales_by_state('NSW'); // Replace 'NSW' with the desired state code if ( $sales_data ) { echo 'Total Sales: ' . $sales_data['total_sales'] . '<br>'; echo 'Order Count: ' . $sales_data['order_count']; }

How the Code Works

This function queries the WooCommerce database to retrieve the total sales and the number of completed orders for a specific state. The state is identified using the billing address information stored in the order meta. Replace 'NSW' in the example code with the desired state code (e.g., VIC, QLD, etc.) to calculate sales for that state.

See also  How to Add Multiple Products with Quantities to the Cart Using a URL in WooCommerce - 2026

Adding a Dashboard Widget for Admin

You may want to display sales by state directly in your WooCommerce admin dashboard. Here’s how you can add a widget to show this information:

/*
 * Snippet: How to Calculate Sales by State in WooCommerce – 2026
* Author: John Cook
* URL: https://wcsuccessacademy.com/?p=1498
* Tested with WooCommerce 10.7.0
* "This function adds a dashboard widget to display sales by state"
*/ function wcsuccess_add_sales_by_state_widget() { wp_add_dashboard_widget( 'wcsuccess_sales_by_state', 'Sales by State', 'wcsuccess_sales_by_state_widget_display' ); } add_action( 'wp_dashboard_setup', 'wcsuccess_add_sales_by_state_widget' ); function wcsuccess_sales_by_state_widget_display() { $sales_data = wcsuccess_calculate_sales_by_state('VIC'); // Change to your desired state if ( $sales_data ) { echo 'Total Sales in VIC: $' . $sales_data['total_sales'] . '<br>'; echo 'Total Orders: ' . $sales_data['order_count']; } else { echo 'No sales data available for this state.'; } }

This code adds a dashboard widget in the WordPress admin that displays sales data for a specific state, like Victoria (VIC). You can modify the state code as needed to display data for other regions.

Best Use Cases for Calculating Sales by State

  • Marketing Strategies: Focus your marketing campaigns in states where your sales are strong, or conversely, put more effort into boosting sales in underperforming regions.
  • Shipping Optimisation: Knowing where most of your customers are located can help you refine your shipping policies and ensure you have stock readily available in those regions.
  • State-Based Promotions: Run targeted promotions for customers in specific states. For instance, you could offer discounts or free shipping to boost sales in lower-performing regions.
See also  How to Create Variable Virtual Downloadable Products Automatically When a New Image Is Uploaded - 2026

Conclusion

By calculating sales by state in WooCommerce, you can gain valuable insights into your customers’ geographic distribution and make data-driven decisions to improve your store’s performance. With a bit of custom code, you can easily integrate this feature into your WooCommerce admin, without the need for an additional plugin.

Always make sure to test any custom code before implementing it on a live site, and use a child theme to keep your changes safe. If you’re looking for more WooCommerce customisations, be sure to check out our WooCommerce Visual Hooks Guide and wp-config generator.

0 0 votes
Article Rating

Stay In Touch

Was this post helpful? Why not show your support and buy me a coffee?

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Scroll to Top
0
Would love your thoughts, please comment.x
()
x
×