Ecommerce Shopify WordPress Discussion

Custom menu to display customer who have made maximum purchased

i want display customer who have made the most purchased on woocommerce on a custom menu. So, to achieve this I have created a custom function that fetches the customers with the maximum purchases from WooCommerce orders and displays their details in a table. Below is a my PHP code to accomplish this but is not working correctly. Please check my code and give me some advices. // Add a custom menu item to the WordPress admin menu function add_custom_menu_item() { add_menu_page( 'Top Customers', 'Top Customers', 'manage_options', 'top_customers_page', 'display_top_customers' ); } add_action('admin_menu', 'add_custom_menu_item'); // Function to display top customers function display_top_customers() { global $wpdb; // Query to get top customers $top_customers_query = " SELECT p.post_author as customer_id, COUNT(p.ID) as total_orders, GROUP_CONCAT(p.ID) as order_ids, SUM(pm.meta_value) as total_order_amount FROM {$wpdb->prefix}posts p INNER JOIN {$wpdb->prefix}postmeta pm ON p.ID = pm.post_id WHERE p.post_type = 'shop_order' AND p.post_status IN ('wc-completed', 'wc-processing') AND pm.meta_key = '_order_total' GROUP BY p.post_author ORDER BY total_orders DESC LIMIT 10"; // Change the limit as per your requirement $top_customers = $wpdb->get_results($top_customers_query); // Output the table ?> <div class="wrap"> <h2>Top Customers</h2> <table class="wp-list-table widefat fixed striped"> <thead> <tr> <th>Customer ID</th> <th>Total Orders</th> <th>Total Order Amount</th> <th>Order Numbers</th> </tr> </thead> <tbody> <?php foreach ($top_customers as $customer) : ?> <tr> <td><?php echo $customer->customer_id; ?></td> <td><?php echo $customer->total_orders; ?></td> <td><?php echo wc_price($customer->total_order_amount); ?></td> <td> <?php $order_ids = explode(',', $customer->order_ids); foreach ($order_ids as $order_id) { $order = wc_get_order($order_id); if ($order) { $order_link = admin_url('post.php?post=' . $order_id . '&action=edit'); echo '<a href="' . $order_link . '" target="_blank">' . $order_id . '</a>, '; } } ?> </td> </tr> <?php endforeach; ?> </tbody> </table> </div> <?php }
TurboCommerce make the better internet purchasing globaly

Turbo Multi-language Translator

Make the better internet purchasing globaly

Turbosify SEO Speed Booster

5.0 (7) Free plan available
Get better conversions by improving store loading speed Installed

Turbo Multi-language Chat - AI Customer service in one hand

TurboCommerce make the better internet purchasing globaly
Our products

The help you need, when you need it

App by Turbo Engine

3 apps • 5.0 average rating

Turbosify Speed Booster

5.0 (7)
Get better conversions by optimizing shopify store Google page speed Installed

Turbosify Translator for Wordpress Woocommerce

5.0 (74) Free Wordpress Woocommerce Plugin
Translate your wordpress website to multiple language within 1 click, no configuration needed, no No technical required

Grow your business here

Whether you want to sell products down the street or around the world, we have all the tools you need.