How to Calculate Sales by Coupon Code in WooCommerce – 2026

Last updated on September 28th, 2024 at 08:58 am

Coupons are a great way to attract customers and boost sales in WooCommerce. Whether you’re running seasonal promotions or rewarding loyal customers, knowing how much revenue each coupon has generated is essential for evaluating the success of your campaigns. WooCommerce doesn’t automatically provide a detailed breakdown of sales by coupon code, but you can calculate this with some simple custom code.

In this guide, we’ll walk you through how to calculate total sales by coupon code in WooCommerce, helping you track the effectiveness of your promotions and refine your marketing strategies.

Why Track Sales by Coupon Code?

Tracking sales by coupon code offers several benefits:

  • Evaluate Promotions: See how much revenue specific coupons have generated to assess the effectiveness of marketing campaigns.
  • Customer Insights: Understand which discounts resonate with your customers.
  • Optimise Future Discounts: Use the data to create better, more targeted offers in the future.

Custom Code to Calculate Sales by Coupon Code

The following code snippet allows you to calculate sales for each coupon code in WooCommerce. To use it, simply add it to your theme’s functions.php file or, preferably, to a child theme to ensure your changes are safe from theme updates.

/*
 * Snippet: How to Calculate Sales by Coupon Code in WooCommerce – 2026
* Author: John Cook
* URL: https://wcsuccessacademy.com/?p=1500
* Tested with WooCommerce 10.7.0
* "This function calculates total sales by coupon code in WooCommerce"
*/ function wcsuccess_calculate_sales_by_coupon( $coupon_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 = '_used_coupons' AND itemmeta.meta_value = %s AND meta.meta_key = '_order_total' ", $coupon_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_coupon('SUMMER2024'); // Replace with your coupon code if ( $sales_data ) { echo 'Total Sales: ' . $sales_data['total_sales'] . '<br>'; echo 'Order Count: ' . $sales_data['order_count']; }

How the Code Works

  • The function wcsuccess_calculate_sales_by_coupon() queries WooCommerce’s database to calculate total sales and order count for a specific coupon code.
  • The code filters for orders marked as completed (wc-completed), ensuring only relevant sales are counted.
  • Replace 'SUMMER2024' with the coupon code you want to check, and the function will return the total sales generated by that coupon, along with the number of orders that used it.
See also  Prevent Coupon Code Sharing for WooCommerce - 2026

Displaying Sales Data in the Admin Dashboard

To make the sales data more accessible, you can add a custom widget to your WordPress admin dashboard that shows sales figures for a specific coupon code.

/*
 * Snippet: How to Calculate Sales by Coupon Code in WooCommerce – 2026
* Author: John Cook
* URL: https://wcsuccessacademy.com/?p=1500
* Tested with WooCommerce 10.7.0
* "This function adds a dashboard widget to display sales by coupon code"
*/ function wcsuccess_add_coupon_sales_widget() { wp_add_dashboard_widget( 'wcsuccess_coupon_sales', 'Sales by Coupon Code', 'wcsuccess_coupon_sales_widget_display' ); } add_action( 'wp_dashboard_setup', 'wcsuccess_add_coupon_sales_widget' ); function wcsuccess_coupon_sales_widget_display() { $sales_data = wcsuccess_calculate_sales_by_coupon('WINTER2024'); // Replace with your coupon code if ( $sales_data ) { echo 'Total Sales for WINTER2024: $' . $sales_data['total_sales'] . '<br>'; echo 'Total Orders: ' . $sales_data['order_count']; } else { echo 'No sales data available for this coupon.'; } }

This code adds a dashboard widget that displays the total sales and number of orders for a specific coupon code directly in the WordPress admin. Update 'WINTER2024' with the coupon code you’d like to track.

Best Use Cases for Calculating Sales by Coupon Code

  • Evaluate Promotion Success: Determine which coupons are driving the most revenue and adjust your future offers accordingly.
  • Track Seasonal Campaigns: Monitor the performance of holiday or seasonal discount codes (e.g., Black Friday, Summer Sale) to see what resonates with your customers.
  • Refine Marketing Strategies: Use the data to improve your marketing efforts, focusing on the most effective coupon codes.
See also  How to Create Variable Virtual Downloadable Products Automatically When a New Image Is Uploaded - 2026

Conclusion

Tracking sales by coupon code in WooCommerce can give you valuable insights into how well your discounts are performing. With the custom code provided, you can easily generate sales reports based on coupon usage, all without installing additional plugins.

Make sure to add the code to a child theme so that your changes are not overwritten by theme updates. For more WooCommerce customisations, you can also explore our WooCommerce Visual Hooks Guide and our 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
×