Bestseller products in Magento
35 Comments 6th DEC 2008 | Posted by Branko Ajzele in Magento

Bestseller or best selling product is one of the features people tend to ask for when it comes to Magento™.
There are multiple ways to implement this feature.
In this example, I’m not using controller or model directories at all; I’m going to show you how to implement this feature using only one file: the View.
Basically, what you need to do is to create the directory inside your template directory and place the bestseller.phtml file in it. In my example, I’m using the custom-created directory /inchoo. All of the screenshots provided here are based on that directory structure.
Adding this feature to your store is a matter of two simple steps:
- copy bestseller-phtml file to your directory
- display the block on home page
To add a block to a home page, you simply log into the Magento, CMS > Manage Pages > Home. Then add the following to the content area:
{{block type=”core/template” template=”inchoo/bestseller.phtml”}}
Notice the type attribute. I used core/template which means you can place this code anywhere in your site and it will work. Code does not inherit any collection objects from controllers since it has none. All that is necessary for the bestseller.phtml to work is defined in that single file.
One more thing: If you study the code in bestseller.phtml file you will see, at the very top of the file, the part that says: $this->show_total.
If I were to write
{{block type=”core/template” show_total=”12″ template=”inchoo/bestseller.phtml”}}
in my home page, then $this would be assigned property show_total with a value of 12.
Therefore, the provided bestseller.phtml file provides the option of setting the number of products you wish to see listed.
Here is the bestseller.phtml packed in bestseller.zip.
Hope you find this useful.
To post code in comments, place your code inside [code] and [/code] tags.






















December 11th, 2008 at 5:33
Thanks, this post is really useful!
Do you think that on the basis of this “bestseller” phtml file it would be possible to edit it so that it only displays the items with a stock lower than 10 instead of displaying the bestsellers?
December 11th, 2008 at 6:22
I actually tried using your technique to display the bestellers but nothing shows up on the page … And i’m pretty sure i followed your instructions exactly and i deactivated the cache in Magento. I’m using Magento 1.8. It’s really strange…
December 11th, 2008 at 21:21
@Jack: if you copied the code from here and pasted it in the home page admin’s CMS management screen, you have to change the “, in: {{block type=”core/template” template=”inchoo/bestseller.phtml”}}
@Branko: thanx for this tutorial, very helpfull, as always.
I imagine you’re aware that configurable products don’t show up with this method. I’m going to look into it to see if I can manage them to appear, but if you have a hint for me I’d be please to read it
December 16th, 2008 at 8:26
Hey Branko, thanks once more for another good Magento tip. I’ve been learning a few things from your sites.
I have a question about your show_total property. You set it in the block description that goes in the content of the page. I was trying to do a similar thing in the layout xml files for some of my own blocks but it doesn’t seem to work in that case. I.e
Do you know why this is?
Also, if you’re preparing another tutorial, could you possibly do one about the various filter option when retrieving a collection (the add*Filter() methods)
Thanks a lot.
December 16th, 2008 at 8:28
Of course my XML tag has bee eaten by WordPress
. But I’m sure you know what I mean?
December 18th, 2008 at 7:39
@osdave: thanks, I had the same problem … but I did’nt have to think it ” ….
{{block type=”core/template” template=”inchoo/bestseller.phtml”}}
January 7th, 2009 at 2:11
Maybe something happened to the Magento filesystem with the latest update, but I can’t find “bestseller.phtml anywhere. Given the depth of Magento’s file system, when referring to files it would hep if a full path was provided. Thanks.
January 15th, 2009 at 23:12
Hi,
Is it possible to get this block to work with configurable products also. I’m affraid that those lines:
Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH,
Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG
are pretty good at not taking single products which are a part of configurables into account. :/
January 26th, 2009 at 11:50
Thanks for that
It works perfectly.
February 2nd, 2009 at 17:12
Is it possible for me to implement getPriceHtml($_product, true, ‘-best’) ?> in this piece of code? getPrice works, but I need getPriceHtml.
Thanks in advance
March 8th, 2009 at 16:16
How are you able to display the path to the template file ? Is it a module ? Or i’m missing an important feature of Magento… ?!
Thanks
March 13th, 2009 at 16:00
Yes, is it possible to implement getPriceHtml?
Cannot make it work
Please help,
thanks!
March 24th, 2009 at 16:28
1) It seems that it does not work for current store view. It displays products from another store.
2) I want to use this module to display newest products. Do you think that changing ->setOrder(‘ordered_qty’, ‘desc’); to ->setOrder(‘created_at’, ‘desc’); will work?
April 17th, 2009 at 11:08
Speed – currend module loads all products and than only displays specified number of products. That is very very slow on a website with thousands of products.
Just add ->setPageSize($totalPerPage) to the Mage::getResourceModel and it will be much faster.
April 21st, 2009 at 7:26
@Jakub
This is an old article an a old approach. Here is a new module (the real module) that is way more faster http://activecodeline.com/magento-bestseller-module-extensions/
April 25th, 2009 at 6:31
thank u branko
May 29th, 2009 at 9:13
i followed your code as it is.but still i couldn’t add bestseller to the homepage.can any one help me as soon as possible…
Thank you…
June 4th, 2009 at 8:05
[...] are many “Bestseller“, “Most Viewed”, and other front-page modules available for Magento. Those that [...]
July 27th, 2009 at 17:29
i prefer using this module than the new one as my products are not that much. i have it set on my front page and it is showing the best selling products in vertical form, how can i make it to show them like the best selling products html form table magento has by default?
August 7th, 2009 at 13:30
Where do I place the bestseller.phtml file? I mean to say which folder?
October 26th, 2009 at 9:09
Excellent thanks for the tutorial that’s what i was looking for how i can integrate custom .phtml files to magento could’t think that would be that easy may i as how you get to know this
so in future i will try to think your way
this is the only place except magento forum or wiki i can read about magento.
Thanks again.
January 22nd, 2010 at 23:13
Hi,
I wonder if it would, with the help of your code, be possible to add the product->ordered_qty in same way in the backend, to be able to see
how many of each products that has been ordered..
Thanks
Johan
February 9th, 2010 at 1:52
Hi, Sorry but this code doesn’t work for me, Might be I m doing something wrong.
I hv created one directory call bestseller under the template directory in my theme.
so path for the bestseller.phtml is “C:\wamp\www\magento\app\design\frontend\default\mycustom\template\bestseller\bestseller.phtml” , is that right ???
and I hv put that block part in my homepage content section.. and i hv changed to {{block type=”core/template” show_total=”12? template=”bestseller/bestseller.phtml”}}
So what is the wrong here?
I couldn’t see anything on my homepage..
Plz help.
April 7th, 2010 at 9:22
THX, i’m a primary learner of magento…But i’m not familiar with Zend framework method…
April 8th, 2010 at 12:10
Hi,
First of all thanks for this wonderful module. It works for me perfectly under Magento 1.4. I need help regarding price, how can I display price along each product?
April 8th, 2010 at 12:14
oh I got it………?php echo $product->price ?
April 9th, 2010 at 19:04
This doesn’t fully work. Currency sign not displayed and .0000 displayed. How to display it correctly.
May 7th, 2010 at 15:53
Hello
I want to give a title separably for a promotional product block and a bestseller block. How can I differences the two block?
Thanks
May 18th, 2010 at 12:17
i’m using magento 1.4 but i couldnt success put variable (show total) to phtml file ,
in bestseller.phtml
$totalPerPage = ($this->show_total) ? $this->show_total : 6;
this code return always value 6 ,
whats wrong?
May 18th, 2010 at 12:18
here is my catalog.xml code;
May 18th, 2010 at 14:02
when i research this situation , i saw that it works when i put blocks code in homepage cms , but when i try to use catalog xml , it cant get variable.
May 23rd, 2010 at 22:24
[...] Bestseller Products in Magento [...]
May 27th, 2010 at 20:35
Very nice !
How can we add a button add to cart?
Thk’s
June 1st, 2010 at 5:06
why configurable products does not display?How to filter?thanks ~
August 18th, 2010 at 12:54
hi,
.
good work, i am facing a problem with this extension they fetch all prduct from all store/website, i want to limitize this extension to one store, how i can do that ..?
second thing is it possible from one or more categories.
Thanks
Muzafar Ali