To get the best-selling products in WooCommerce, you can use the WooCommerce function wc_get_products
with appropriate arguments to retrieve the products based on high sales, recent views, and high ratings.
Suggested Read: How to check if a product is in a WooCommerce order
WooCommerce: Get “Best Selling” Products
Here, we’ll provide an example code of how to get best-selling or trending products based on total sales.
2 3 4 5 6 7 8 9 10 11 12 13 |
$best_selling_products = wc_get_products(array( 'limit' => 10, // Adjust the limit as needed 'status' => 'publish', 'orderby' => 'meta_value_num', 'meta_key' => 'total_sales', )); foreach ($best_selling_products as $product) { echo '<p>' . $product->get_name() . ' - Total Sales: ' . $product->get_total_sales() . '</p>'; } |
WooCommerce: Get Best Selling Products from the last week
Here’s a step-by-step guide: you need to consider the sales data and order information. WooCommerce itself doesn’t natively track the “best sellers” by default, so you would need to consider other data such as order quantities, order totals, or the number of times a product has been purchased. You can use the WooCommerce wc_get_orders function to retrieve the orders and their details.
Here’s a general approach to fetching the best-selling products from the last week based on the number of orders:
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
// Calculate the date one week ago from the current date. $one_week_ago = date('Y-m-d', strtotime('-1 week')); // Define the number of best-selling products you want to retrieve. $number_of_best_sellers = 10; // Change this number as needed. // Get the order IDs for completed orders in the last week. $order_ids = wc_get_orders(array( 'date_query' => array( array( 'after' => $one_week_ago, ), ), 'limit' => -1, 'status' => array_map( 'wc_get_order_status_name', wc_get_is_paid_statuses() ), 'return' => 'ids', )); // Initialize an array to store product sales data. $product_sales = array(); // Loop through the order IDs and collect product sales data. foreach ($order_ids as $order_id) { $order = wc_get_order($order_id); foreach ($order->get_items() as $item_id => $item) { $product_id = $item->get_product_id(); $product_qty = $item->get_quantity(); if (isset($product_sales[$product_id])) { $product_sales[$product_id] += $product_qty; } else { $product_sales[$product_id] = $product_qty; } } } // Sort the products by sales quantity in descending order. arsort($product_sales); // Get the top-selling products for the last week. $best_selling_products_week = array_keys(array_slice($product_sales, 0, $number_of_best_sellers,true)); // Display the best-selling products. foreach ($best_selling_products_week as $product_id) { $product = wc_get_product($product_id); if($product){ echo '<p>' . $product->get_name() . ' (Quantity Sold: ' . $product_sales[$product_id] . ')</p>'; } } |
Note: You can change ‘-1 week’ to whatever time length you require e.g. ‘-2 weeks’, ‘-3 months’ etc.
Are you want to get implementation help, or modify or extend the functionality of this script?
A Tutorialswebsite Expert can do it for you.
In this above code:
- We calculate the date one week ago from the current date using the strtotime function.
- We define the query arguments for the orders, specifying that we want orders completed in the last week.
- We use wc_get_orders to retrieve the orders.
- We loop through the orders, extract product information, and calculate the total quantity of each product sold.
- We sort the products by the sales quantity in descending order.
- We select the best-selling products by slicing the sorted array.
- Finally, we display the best-selling products and their quantities.
You can adjust the number of top-selling products to retrieve by changing the array_slice parameters.
Wrapping Words
Thanks for reading 🙏, I hope you found the How to Get Best Selling Products in Woocommerce tutorial helpful for your project. Keep learning! If you face any problems – I am here to solve your problems.
Read More Related Articles:
- Custom Shipping Rates by billing country in WooCommerce Programmatically
- Create WooCommerce custom product type programmatically
Pradeep Maurya is the Professional Web Developer & Designer and the Founder of “Tutorials website”. He lives in Delhi and loves to be a self-dependent person. As an owner, he is trying his best to improve this platform day by day. His passion, dedication and quick decision making ability to stand apart from others. He’s an avid blogger and writes on the publications like Dzone, e27.co