A Better Way for Obtaining Best Sellers in Magento

Best Sellers, New Arrivals, and Promotions are three cornerstones of any ecommerce solution; they add great value for both customers and store owners.

Best sellers offer customers quick view over the products that other customers buy the most, which adds to the customer’s confidence when deciding to make a purchase and makes it easier for the customer to choose between alternatives.

The method of calculating best sellers is pretty straightforward; you count how many times has each product been sold and order the result by summed-up quantity ordered; then you’ve got yourself a best-sellers widget or products slider that should most probably be placed on the homepage.

In development mode, and on staging server as well, everything is working as you expected, until you move to production and the store owners decide to launch huge campaign that will drive thousands of visitors to the homepage; everything falls apart then, as each request makes Magento recalculate the best sellers and then load every product to show the product’s card.

Caching to the rescue, you tell yourself, and it helps relieve the stress off your database and your store comes back online. You could then make a change and try to refresh the cache while the visitors are hammering the homepage with requests looking for your store’s best selling products and that is exactly when you realize that even those split seconds of recreating the cache can become minutes of downtime as the requests keep hitting Magento while it is recreating the cache are all cache-miss that hammer the database directly until Magento gets it act together and starts serving content from the cache again.

Now let’s back off a moment, Magento has some powerful reports, one of which is, guess what, “Best Selling Products”! So why don’t we check how does Magento generate such report without bringing online stores to their knees, meanwhile, applying one of the most known software engineering principles in the process, which is DRY (Don’t Repeat Yourself) while we’re at it. Okay?!

Magento doesn’t generate this report on the fly or, to be specific, it doesn’t calculate best sellers on the fly; however, it makes use of it’s own cron jobs to aggregate best selling products every day and save them in a database table for later use. This allows Magento to directly hit this pre-calculated table of best sellers and just do the work of getting every product’s attributes, while sacrificing the ability to get fresh and up-to-date list of best sellers, which is not that crucial in most cases.

So let’s stand on the shoulders of giants and make use of that pre-calculated, automatically-aggregated table of best sellers we say, and instead of calculating the best selling products for every request, just query this table for the period you wish and then fetch the attributes you want to view for each product.

As a rule of thumb in Magento development, make sure to invest some time checking if the feature you want to develop/implement is already implemented in a way or another in Magento; this will save you so much time and effort on the long run and will also, most probably, give you a better idea for doing things. Have you encountered a similar problem while using Magento? Tell us what you did in the comments.

Leave a Reply

Your email address will not be published. Required fields are marked *