Bestseller products in Magento

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

Bestseller products 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.

If you like what you read, please share it.

  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Yahoo! Bookmarks
  • Reddit
  • Technorati
  • Twitter
  • StumbleUpon
  • LinkedIn
  • Netvibes
  • NewsVine
  • Sphinn
  • Tumblr
  • Posterous

To post code in comments, place your code inside [code] and [/code] tags.

There are 35 comments (Add Yours +)

  • 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?

  • 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…

  • @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 :)

  • 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.

  • Of course my XML tag has bee eaten by WordPress :) . But I’m sure you know what I mean?

  • @osdave: thanks, I had the same problem … but I did’nt have to think it ” ….
    {{block type=”core/template” template=”inchoo/bestseller.phtml”}}

  • 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.

  • 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. :/

  • Thanks for that :-)
    It works perfectly.

  • 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 :)

  • 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

  • Yes, is it possible to implement getPriceHtml?
    Cannot make it work :(

    Please help,
    thanks!

  • 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?

  • 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.

  • @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/

  • thank u branko

  • 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…

  • [...] are many “Bestseller“, “Most Viewed”, and other front-page modules available for Magento. Those that [...]

  • 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?

  • Where do I place the bestseller.phtml file? I mean to say which folder?

  • 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.

  • 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

  • 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.

  • Joshua Lee Says

    THX, i’m a primary learner of magento…But i’m not familiar with Zend framework method…

  • 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?

  • oh I got it………?php echo $product->price ?

  • This doesn’t fully work. Currency sign not displayed and .0000 displayed. How to display it correctly.

  • Quentin Peyramale Says

    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

  • 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?

  • here is my catalog.xml code;

  • 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.

  • Very nice !
    How can we add a button add to cart?
    Thk’s

  • why configurable products does not display?How to filter?thanks ~

  • 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

Leave a Comment

Please wrap all source codes with [code][/code] tags.
Magento Design and Development | Magento SEO | iPhone Application Development Web Application Development with ZEND | WordPress Ecommerce | WordPress development
Sitemap

Inchoo - webappsolutions | 2009