How to Show Current Month Bestselling Products in WooCommerce

How to get last month’s bestsellers in WooCommerce.

While WooCommerce does have a way to get all-time bestseller products, it doesn’t have a way to do it with a time constraint. Or none that I know of (and I’ve searched the internet for a few hours for that).

In case this is what you need, here are the WP_Query arguments that will get you the first 10 bestselling products of all time.

  $args = array(
    'post_type' => 'product',
    'meta_key' => 'total_sales',
    'orderby' => 'meta_value_num',
    'posts_per_page' => 1,
  );

But in case you want to get the best selling products from a specific time period, like the last 30 days, March or April 2021, or whatever date, we have to go a different route.

Here’s a quick run-through of what we need to do:

  • Get all orders in a speciffic time period
  • Run trough each order and push to an array the items ordered
  • Count how many times each item is in this array (using PHP function array_count_values() )
  • Sort them from the most frequest to the least frequent
  • Take the first 10 items (or however many we need)
  • Return the items IDs

Let’s go through each one now.

The first thing we have to do is to get all the orders within a certain time period. For this example, we will use the last 30 days (or the last month). We will also need to include parameters so that we get all of the completed orders. Here we go:

$args = array(
  'date_created' => '>' . ( time() - MONTH_IN_SECONDS  ),
  'limit' => -1,
  'status' => 'wc-completed'
);


$orders = wc_get_orders( $args );

And after that, we use wc_get_orders() with the args to get them and store them in a variable.

The next step is to loop through the orders and then loop through each item of the order.

$arr = [];

foreach ( $orders as $order ) {
  foreach ( $order->get_items() as $item_id => $item ) {
    array_push( $arr, $item->get_product_id() );
  }
}

Before the loop, we need to create an array to push our items into. We are pushing only the ID, as it’s the only thing we will need at the moment.

Now comes the counting part. No fancy algorithm is needed, only a simple PHP function:

$vals = array_count_values($arr);
arsort($vals);

$relevant = array_slice($vals, 0, 10, true);

So, what we did there. First, we counted the items. Basically, this function returns an associative array that looks like this: [“ID”] => [3]. How many times each item was in the array.

Obviously, after that, we need to sort the array from the highest to the lowest. And then we will take only as many items as we need. In our case, it was 10. You can put whatever you want there.

Now, there are just two more lines of code needed:

$prod_ids = array_keys($relevant);
    
return $prod_ids;

You can combine those two lines, but I keep them separated in order to be easier understood. First, we need to take the keys (or the IDs) of the items in the $relevant array and put them into another one. And then, we will return the $prod_ids array.

This will return an array with the top 10 bestselling products in the last month. Now you can use this array to call different queries anywhere you need.

Obviously, this code would go into your theme functions.php file. I have encapsulated it as a function so I could reuse it with ease anywhere on the website.

Here’s how the final code would look like:

  function get_bestsellers_last_month(){
    
      $args = array(
          'date_created' => '>' . ( time() - MONTH_IN_SECONDS  ),
          'limit' => -1,
          'status' => 'wc-completed'
      );
      $orders = wc_get_orders( $args );
      $arr = [];
    
      foreach ( $orders as $order ) {
          foreach ( $order->get_items() as $item_id => $item ) {
              array_push( $arr, $item->get_product_id() );
          }
      }
     
      $vals = array_count_values($arr);
      arsort($vals);
      $relevant = array_slice($vals, 0, 10, true);
    
      $prod_ids = array_keys($relevant);
    
      return $prod_ids;  
  }

That’s all folks. Hope this snippet will help you. If you have any questions or you need any help, don’t hesitate to contact me.

What is the Best Platform for eCommerce Websites?

Are you looking for the best platform for eCommerce websites?

If you want to start an online store, you have to keep in mind choosing the best platform for you.

And that’s not as easy as you would think.

First, you have to give yourself an answer to a few questions like, “How much do I want to invest in a platform? Do I want to do it myself, or do I want to hire someone to do it? What features do I want?”

Depending on your answers to these questions, you can choose an eCommerce platform that would fit you.

Here are a few of the most popular eCommerce platforms at the moment.

I’ll start off with my favorite and the one we love working with.

Woocommerce

Woocommerce is one of the most popular eCommerce platforms, powering over 28% of all online stores. Not only that, but it is incredibly easy to use and one of the most affordable eCommerce platforms.

This platform is developed as a plugin for WordPress, and that means it will require a bit of previous experience (or a really easy learning curve) to get acquainted with the CMS (content management system). The good part is that the platform is very user-friendly.

For every client that we work with, we provide them with video training in order do make it even easier to start using the platform.

Now you may think if you Woocommerce supports your kind of product. Most certainly YES. You can sell physical products, digital products that can be downloaded, and they even added a feature for a subscription membership site. Not only that, but you can make bookings with Woocommerce. They really wanted to cover it all.

There are also a lot of tools that will help your store get discovered. Starting from Yoast SEO integration for Woocommerce to Mailchimp (email marketing tool) integration, to countless payment integrations (like Stripe, PayPal, and many others).

But what should you do if you want to customize your website and don’t have any coding experience?

Nothing to worry about. There are countless WordPress page builders that allow you to customize the Woocommerce shop, category, and product pages too. Elementor, Divi, Beaver Builder, just to name a few.

Woocommerce is a good fit for all sizes because it can be highly customizable. Most of the time, Woocommerce will fit your need for an eCommerce platform.

I almost forgot to mention that this platform is free to use. You can buy premium extensions, but the base plugin with the free extensions is plenty for what you need on an eCommerce platform.

Shopify

This is another popular, easy-to-use, not-so-customizable, and more expensive eCommerce platform. Shopify is supporting over 600,000 online stores with more than 1 million active users.

Shopify is the platform most loved by drop shippers because of all the tracking and analytics tools they offer, for a price, of course. Just the basic package of Shopify is starting at 29$ / month and going up to 299$ / month with the advanced package.

With Shopify, you can use one of 100 free themes, or buy a premium design theme for your shop. Even if you buy a premium theme, you can’t customize much: colors, imagery, and fonts.

If you were to compare it to Woocommerce, it is easier to use, but at a much higher cost of operation.

Magento

If you are looking for a highly customizable (like almost unlimited customizability) eCommerce platform, Magento is the one. But all of this comes at a cost: a steep learning curve.

The biggest users of Magento tell us what kind of companies need this platform: Burger King, Huawei, Pepe Jeans. Yes, the kind of companies with huge software development staff who don’t have to worry about setting up the store.

So, for what businesses is this platform best? For big enterprises with a huge amount of products or services. They will get the most efficient results for using Magento.

Those three eCommerce platforms are the most popular and the big players in this industry.

Our Recommendation

The one we recommend to all our clients is Woocommerce because it is the one that makes the most sense for Small and Medium Businesses that are willing to take their businesses to the online market. Woocommerce may be the best eCommerce website platform for you.