How to Extract Your Top Customers by Sales from Your WooCommerce Store

How to Extract Your Top Customers by Sales from Your WooCommerce Store

As a WooCommerce store owner, understanding who your top customers are can be crucial for driving business growth. These customers are not only valuable for repeat business, but they can also be excellent advocates for your brand. In this blog post, we’ll guide you through how to extract your top customers by sales from your WooCommerce store using a simple code snippet.


Why Identifying Top Customers is Important

  1. Targeted Marketing: Knowing your top customers allows you to tailor your marketing efforts towards them, increasing the likelihood of repeat purchases.
  2. Customer Loyalty Programs: You can offer exclusive rewards or discounts to your top customers, strengthening their loyalty.
  3. Personalized Experience: Understanding your top customers’ preferences helps you offer a more personalized shopping experience.
  4. Customer Insights: Analyzing the purchasing habits of your top customers can provide valuable insights for product development and inventory management.

How to Extract Top Customers by Sales: A Step-by-Step Guide

To identify your top customers by sales in WooCommerce, you can use a custom SQL query or a PHP code snippet. For this guide, we’ll focus on a PHP solution that you can easily implement.

Step 1: Prepare Your Environment

Before proceeding, make sure you have access to your WooCommerce store’s backend, and you’re comfortable editing theme files or using a custom plugin.

Step 2: Add the PHP Code Snippet

Here’s a PHP code snippet that you can use to retrieve a list of your top customers based on their total spending:

function get_top_customers_by_sales($limit = 10) {
    global $wpdb;

    // Query to get the top customers by total sales
    $query = "
        SELECT p.ID AS customer_id, 
               SUM(pm2.meta_value) AS total_spent,
               CONCAT_WS(' ', um1.meta_value, um2.meta_value) AS customer_name,
               u.user_email
        FROM {$wpdb->prefix}posts AS p
        JOIN {$wpdb->prefix}postmeta AS pm1 ON p.ID = pm1.post_id
        JOIN {$wpdb->prefix}postmeta AS pm2 ON p.ID = pm2.post_id
        JOIN {$wpdb->prefix}users AS u ON u.ID = p.post_author
        LEFT JOIN {$wpdb->prefix}usermeta AS um1 ON um1.user_id = u.ID AND um1.meta_key = 'first_name'
        LEFT JOIN {$wpdb->prefix}usermeta AS um2 ON um2.user_id = u.ID AND um2.meta_key = 'last_name'
        WHERE p.post_type = 'shop_order'
        AND p.post_status IN ('wc-completed')
        AND pm1.meta_key = '_customer_user'
        AND pm2.meta_key = '_order_total'
        GROUP BY p.post_author
        ORDER BY total_spent DESC
        LIMIT %d
    ";

    // Prepare and execute the query
    $results = $wpdb->get_results($wpdb->prepare($query, $limit));

    // Output the results
    if (!empty($results)) {
        echo '<h2>Top ' . $limit . ' Customers by Sales</h2>';
        echo '<table>';
        echo '<tr><th>Customer ID</th><th>Customer Name</th><th>Email</th><th>Total Spent</th></tr>';
        foreach ($results as $result) {
            echo '<tr>';
            echo '<td>' . esc_html($result->customer_id) . '</td>';
            echo '<td>' . esc_html($result->customer_name) . '</td>';
            echo '<td>' . esc_html($result->user_email) . '</td>';
            echo '<td>' . wc_price($result->total_spent) . '</td>';
            echo '</tr>';
        }
        echo '</table>';
    } else {
        echo '<p>No customers found.</p>';
    }
}

// Usage example
add_shortcode('top_customers_by_sales', 'get_top_customers_by_sales');

Step 3: Implement the Code

  1. Using a Custom Plugin: You can create a small custom plugin to hold this code. To do this, create a folder named top-customers-by-sales in the wp-content/plugins/ directory. Inside that folder, create a file named top-customers-by-sales.php and paste the code snippet above into it. Then, activate the plugin from your WordPress dashboard.
  2. Adding to Theme’s functions.php: Alternatively, you can paste the code into your theme’s functions.php file. However, using a custom plugin is preferable as it won’t be affected when you update your theme.

Step 4: Display the Top Customers

You can display the top customers on any page or post using the shortcode [top_customers_by_sales]. This shortcode will render a table listing your top customers by total sales.

Step 5: Customize the Output

You can customize the $limit parameter in the function to control how many top customers you want to display. For example, to show the top 20 customers, change the function call to:

get_top_customers_by_sales(20);

Conclusion

Identifying your top customers by sales is a powerful way to leverage your WooCommerce store’s data for better marketing and customer retention strategies. With the simple code snippet provided, you can quickly extract this valuable information and start building stronger relationships with your best customers.

By displaying this data, either on the backend or directly on a webpage, you can keep track of your most valuable customers and ensure they receive the attention they deserve.

Feel free to share this post with other WooCommerce store owners and leave a comment below if you have any questions or need further customization!


By implementing this solution, you’ll have a clear view of who your top spenders are, allowing you to focus on what matters most—keeping your best customers happy and engaged.

Photo by Blake Wisz on Unsplash

Get A No Obligation Quote

Do You Need Help With Your WooCommerce Site?

Click through to the next page and complete the form to get a free no obligation quote to fix any issue you are having with your WooCommerce site.