Display a BestSellers block on your Magento homepage

One of the frequent questions you ask when installing Magento is how to add Bestsellers block to your homepage

Indeed, Magento - in its standard version at least - does not provide this feature.

Nevertheless, even if the lack of this feature might be quite weird for an ecommerce platform, it is not a blocking issue. Quite the contrary, it allows you to realise how easy it is to create new blocks on Magento, or, at least, the flexibility of the architecture of Magento.

  1. Get the data

    This could be the most difficult step since you must know perfectly the Magento core object model.
    As soon as you know where to collect data (in this case, sold products) and how to request them (in this case, by descending order of sales), getting required data is pretty easy, it just take one line (actually several lines here, but just to allow a better readability).

    $_productCollection = Mage::getResourceModel('reports/product_collection')
        ->setOrder('ordered_qty', 'desc');

  2. Create your BestSellers block

    Remember to use MVC pattern: Zend framework, and therefore Magento, ensure that each object has its specific role.
    Creat your business object:
    In a file that could be app/code/local/ma_librairie/Bestseller/Model/Bestseller.php write your business rules (here: getting sold products by descending order of sales).
    Then in a file app/code/local/ma_librairie/Bestseller/Block/Bestseller.php extend class Mage_Core_Block_Template that will pass data to display block.
    At last, in file app/design/frontend/mon_theme/default/template/catalog/product/view/bestseller.phtml display block content.
    Of course these files have to be declared in the appropriate XML configuration files.

  3. Display your block

    Set up your block on yout active layout.
    Either change the XML file of your layout, or add it on your homepage from Magento backoffice.

For more information about Magento Templates, you may read the article Creating templates for your Magento website.

2 comments on "Display a BestSellers block on your Magento homepage"

Affordable SEO's picture
Affordable SEO (visitor) - Sun, 24/01/2010 - 01:03:

I followed your instructions to the key, but still can't get my block to display

Laurent Clouet's picture
Laurent Clouet - Mon, 25/01/2010 - 11:53:

Did you declared correctly your block in config.xml file ?

Is it the good type of block that you call in your layout file ?

You can also take a look at widget bestseller: http://www.magentocommerce.com/extension/2118/widget-bestsellers if you use Magento CE1.4 or EE1.6.