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

53
Top

Enjoyed this post?

Subscribe to our RSS Feed, Follow us on Twitter and spread it to your friends!

Author

Branko is Inchoo's CTO with over 3 years of active / everyday full time Magento development.

Other posts from this author

Discussion 53 Comments

1 2
Add Comment
  1. Tejasvini

    get price html using following code on bestesller.phtml:

    $storeId = Mage::app()->getStore()->getId(); $_product= Mage::getModel(‘catalog/product’)->setStoreId($storeId)->load($product->entity_id);
    $theProductBlock = new Mage_Catalog_Block_Product; echo $theProductBlock->getPriceHtml($_product, true);

  2. Dragan Simonovic

    Is it possible to include toolbar with pagination and everything?

  3. Mark

    It drops an exception error in 1.5.1.0 on the line:

    $_productCollection = Mage::getResourceModel('reports/product_collection')
                                  ->addAttributeToSelect('*')
                                  ->addOrderedQty()
                                  ->addAttributeToFilter('visibility', $visibility)
                                  ->setOrder('ordered_qty', 'desc');
    

    any ideas?

1 2

Add Your Comment

Please wrap all source codes with [code][/code] tags.
Top