How to Get Best Selling Products in Woocommerce Programmatically

Best Selling Products in Woocommerce Programmatically

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.

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:

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:

  1. Custom Shipping Rates by billing country in WooCommerce Programmatically
  2. Create WooCommerce custom product type programmatically

Related posts