Question

I am trying to implement Best seller sort by in category list page.

Followed below link, but that is not working in Magento 2.3.4.

Magento 2 add Sort By Best Sellers Option on category products litsing page

Any suggestions how this can be achieved? the above solution having problem with page load. That is taking more than 20mins to load.

Also it has other problem, collection is not filtering and showing the Best seller items in list page.

Please someone suggest me how can we get Best seller option in sort By in list and search result page. Thanks in Advance!!

Was it helpful?

Solution

I have written a solution that involves an additional product attribute called 'Best Sellers'. This attribute is auto-created by the module.

Each time an order is made for a product (If a configurable product is added, it is the parent product that is accounted with the sale), the product attribute 'best sellers' value is increased. (the quantity is not taken into account in my implementation) Then, this solution reads the number of sales on the fly rather than using a cron.

it is a solution I have used already in production in the past but I have rewritten it today to use the Magento 2 best principles I know of.

https://bitbucket.org/magstaging/bestseller/src/master/

OTHER TIPS

I suggest try another approach that I did in a Magento 1 store: instead of try rewrite how the resource/collection is assembled, create a new attribute in native way for best seller (save the qty of purchases) and create an extension with a cron to populate the data in this field.

Using this way the core feature of sort of Magento will works as expected in any scenario. Of course it won't be in real time. In my case with Magento 1 I did a cron that update best seller attribute in the products every day at midnight.

So a quick list with the steps for you:

  1. Create the extension with a crontab job.
  2. Create the attribute to storage the quantity of sales for products (you can created it inside the admin panel if you want). Make sure to make it usable to sort and set this attribute to default sort attribute in Magento admin.
  3. Create a model with a function to get the sales order items created since last execution (last 24h) and update the product's bestseller attribute increasing the number.
  4. Do a customization to change the default direction of sort because you want the bigger number first (DESC direction). This link can help you: https://www.customerparadigm.com/changing-default-product-sorting-in-magento-2/

Also in github there are some extensions for get it, but I never tested in Magento 2:

In the end there is a third way to try it: Instead of create a cron to fill the attribute for qty of purchases, you can create an event to increment the attribute after place each order. But I don't like this way because can impact the time to place an order.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top