Bestseller products in Magento

Featured Image

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.

74
Top

Care to rate this post?

Author

Branko Ajzele

Ex Inchooer

Worked at Inchoo as a Backend Developer/CTO from 2008 to 2013

Other posts from this author

Discussion 74 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?

  4. On 1.6.1 i get nothing … no errors and no bestseller … also after cache clearing.

    {{block type=”core/template” template=”myTemplate/bestseller.phtml”}}

  5. Mike

    Works flawlessly on 1.6.2.

    Cheers Branko!

  6. Pravin

    On upgrade from 1.4 to 1.6 , it is not working.
    It do not shows the addAttributeToSelect attributes in phtml as well as it do not even filter store wise.

    Here is the query it is shoing

    SELECT SUM(order_items.qty_ordered) AS `ordered_qty`, `order_items`.`name` AS `order_items_name`, `order_items`.`product_id` AS `entity_id`, `e`.`entity_type_id`, `e`.`attribute_set_id`, `e`.`type_id`, `e`.`sku`, `e`.`has_options`, `e`.`required_options`, `e`.`created_at`, `e`.`updated_at` FROM `tp_sales_flat_order_item` AS `order_items` INNER JOIN `tp_sales_flat_order` AS `order` ON `order`.entity_id = order_items.order_id AND `order`.state <> 'cancelled' LEFT JOIN `tp_catalog_product_entity` AS `e` ON (e.type_id NOT IN ('grouped', 'configurable', 'bundle')) AND e.entity_id = order_items.product_id AND e.entity_type_id = 4 WHERE (parent_item_id IS NULL) GROUP BY `order_items`.`product_id` HAVING (SUM(order_items.qty_ordered) > 0) ORDER BY `ordered_qty` desc LIMIT 4
  7. Jones

    On Magento 1.6.2 It is also not working.

    I’ve inserted the file bestseller.phtml to mytemlate/inchoo/

    is there anything else to do?

    PLEASE HELP

  8. Teju

    I have use magneto 1.6.2 it not working.
    i have put the bestseller.phtml \www\e4hats\magento\app\design\frontend\default\e4hats\template\catalog\product

    i have create block cms->static block

    {{block type=”catalog/product_list”template=”catalog/product/bestseller.phtml”}}

  9. A category id based solution will be good. On Magento 1.6.2.0 stable it works not. I developed now my own bestseller extension.

  10. Pravin

    @Alexander
    Can you please provide a hint about how you use categopry based solution.
    In my case i have the same root category for both store

    THanks

  11. Amelie

    Can you tell us where to create exactly the folder “inchoo” ?

  12. mahesh kalantre

    hey this is very usefull block, let me know how to add “Add TO Cart ” button in this code

  13. if we disable the line :
    ->addAttributeToFilter(‘visibility’, $visibility)

    then it works.

  14. Lenny

    Great post though I can’t get it to work on grouped products, I can display the invisible items of course but it won’t link to grouped item or the item as it’s invisible (being part of the grouped!)!

    Can you help?

  15. mycon

    When I Go to your Demo Admin interface
    - System > Configuration > Catalog
    - set Use Flat Catalog Category Yes
    - set Use Flat Catalog Product Yes
    - Reindex the tables
    - Go to frontend end see the bugs…thats it!

    SELECT SUM(order_items.qty_ordered) AS `ordered_qty`, `order_items`.`name` AS `order_items_name`, `order_items`.`product_id` AS `entity_id`, `e`.`entity_type_id`, `e`.`attribute_set_id`, `e`.`type_id`, `e`.`sku`, `e`.`has_options`, `e`.`required_options`, `e`.`created_at`, `e`.`updated_at`, `e`.`entity_id`, `e`.`attribute_set_id`, `e`.`type_id`, `e`.`cost`, `e`.`created_at`, `e`.`enable_googlecheckout`, `e`.`gift_message_available`, `e`.`has_options`, `e`.`image_label`, `e`.`is_imported`, `e`.`is_recurring`, `e`.`links_exist`, `e`.`links_purchased_separately`, `e`.`links_title`, `e`.`msrp`, `e`.`msrp_display_actual_price_type`, `e`.`msrp_enabled`, `e`.`name`, `e`.`news_from_date`, `e`.`news_to_date`, `e`.`price`, `e`.`price_type`, `e`.`price_view`, `e`.`recurring_profile`, `e`.`required_options`, `e`.`shipment_type`, `e`.`short_description`, `e`.`sku`, `e`.`sku_type`, `e`.`small_image`, `e`.`small_image_label`, `e`.`special_from_date`, `e`.`special_price`, `e`.`special_to_date`, `e`.`tax_class_id`, `e`.`thumbnail`, `e`.`thumbnail_label`, `e`.`updated_at`, `e`.`url_key`, `e`.`url_path`, `e`.`visibility`, `e`.`weight`, `e`.`weight_type`, `price_index`.`price`, `price_index`.`tax_class_id`, `price_index`.`final_price`, IF(price_index.tier_price IS NOT NULL, LEAST(price_index.min_price, price_index.tier_price), price_index.min_price) AS `minimal_price`, `price_index`.`min_price`, `price_index`.`max_price`, `price_index`.`tier_price`, `e`.`name`, `e`.`price`, `e`.`small_image`, `cat_index`.`position` AS `cat_index_position` FROM `sales_flat_order_item` AS `order_items`
    INNER JOIN `sales_flat_order` AS `order` ON `order`.entity_id = order_items.order_id AND `order`.state <> ‘canceled’
    LEFT JOIN `catalog_product_entity` AS `e` ON (e.type_id NOT IN (‘grouped’, ‘configurable’, ‘bundle’)) AND e.entity_id = order_items.product_id AND e.entity_type_id = 4
    INNER JOIN `catalog_product_index_price` AS `price_index` ON price_index.entity_id = e.entity_id AND price_index.website_id = ’1′ AND price_index.customer_group_id = 0
    INNER JOIN `catalog_category_product_index` AS `cat_index` ON cat_index.product_id=e.entity_id AND cat_index.store_id=’1′ AND cat_index.visibility IN(2, 4) AND cat_index.category_id=’2′ WHERE (parent_item_id IS NULL) GROUP BY `order_items`.`product_id` HAVING (SUM(order_items.qty_ordered) > 0) ORDER BY `ordered_qty` desc LIMIT 8

    Trace:
    #0 D:\xampp\htdocs\magentoma\ma_furniturestore\lib\Varien\Db\Statement\Pdo\Mysql.php(110): Zend_Db_Statement_Pdo->_execute(Array)
    #1 D:\xampp\htdocs\magentoma\ma_furniturestore\lib\Zend\Db\Statement.php(300): Varien_Db_Statement_Pdo_Mysql->_execute(Array)
    #2 D:\xampp\htdocs\magentoma\ma_furniturestore\lib\Zend\Db\Adapter\Abstract.php(479): Zend_Db_Statement->execute(Array)
    #3 D:\xampp\htdocs\magentoma\ma_furniturestore\lib\Zend\Db\Adapter\Pdo\Abstract.php(238): Zend_Db_Adapter_Abstract->query(‘SELECT SUM(orde…’, Array)
    #4 D:\xampp\htdocs\magentoma\ma_furniturestore\lib\Varien\Db\Adapter\Pdo\Mysql.php(389): Zend_Db_Adapter_Pdo_Abstract->query(‘SELECT SUM(orde…’, Array)
    #5 D:\xampp\htdocs\magentoma\ma_furniturestore\lib\Zend\Db\Adapter\Abstract.php(734): Varien_Db_Adapter_Pdo_Mysql->query(‘SELECT SUM(orde…’, Array)
    #6 D:\xampp\htdocs\magentoma\ma_furniturestore\lib\Varien\Data\Collection\Db.php(687): Zend_Db_Adapter_Abstract->fetchAll(‘SELECT SUM(orde…’, Array)
    #7 D:\xampp\htdocs\magentoma\ma_furniturestore\app\code\core\Mage\Eav\Model\Entity\Collection\Abstract.php(1007): Varien_Data_Collection_Db->_fetchAll(‘SELECT SUM(orde…’)
    #8 D:\xampp\htdocs\magentoma\ma_furniturestore\app\code\core\Mage\Eav\Model\Entity\Collection\Abstract.php(831): Mage_Eav_Model_Entity_Collection_Abstract->_loadEntities(false, false)
    #9 D:\xampp\htdocs\magentoma\ma_furniturestore\lib\Varien\Data\Collection.php(740): Mage_Eav_Model_Entity_Collection_Abstract->load()
    #10 D:\xampp\htdocs\magentoma\ma_furniturestore\app\design\frontend\default\ma_furniturestore_orange\template\magentothem\bestsellerproductvertscroller\bestsellerleft.phtml(42): Varien_Data_Collection->count()
    #11 D:\xampp\htdocs\magentoma\ma_furniturestore\app\code\core\Mage\Core\Block\Template.php(241): include(‘D:\xampp\htdocs…’)
    #12 D:\xampp\htdocs\magentoma\ma_furniturestore\app\code\core\Mage\Core\Block\Template.php(272): Mage_Core_Block_Template->fetchView(‘frontend\defaul…’)
    #13 D:\xampp\htdocs\magentoma\ma_furniturestore\app\code\core\Mage\Core\Block\Template.php(286): Mage_Core_Block_Template->renderView()
    #14 D:\xampp\htdocs\magentoma\ma_furniturestore\app\code\core\Mage\Core\Block\Abstract.php(863): Mage_Core_Block_Template->_toHtml()
    #15 D:\xampp\htdocs\magentoma\ma_furniturestore\app\code\core\Mage\Core\Block\Text\List.php(43): Mage_Core_Block_Abstract->toHtml()
    #16 D:\xampp\htdocs\magentoma\ma_furniturestore\app\code\core\Mage\Core\Block\Abstract.php(863): Mage_Core_Block_Text_List->_toHtml()
    #17 D:\xampp\htdocs\magentoma\ma_furniturestore\app\code\core\Mage\Core\Block\Abstract.php(582): Mage_Core_Block_Abstract->toHtml()
    #18 D:\xampp\htdocs\magentoma\ma_furniturestore\app\code\core\Mage\Core\Block\Abstract.php(526): Mage_Core_Block_Abstract->_getChildHtml(‘left’, true)
    #19 D:\xampp\htdocs\magentoma\ma_furniturestore\app\design\frontend\default\ma_furniturestore_orange\template\page\3columns.phtml(52): Mage_Core_Block_Abstract->getChildHtml(‘left’)
    #20 D:\xampp\htdocs\magentoma\ma_furniturestore\app\code\core\Mage\Core\Block\Template.php(241): include(‘D:\xampp\htdocs…’)
    #21 D:\xampp\htdocs\magentoma\ma_furniturestore\app\code\core\Mage\Core\Block\Template.php(272): Mage_Core_Block_Template->fetchView(‘frontend\defaul…’)
    #22 D:\xampp\htdocs\magentoma\ma_furniturestore\app\code\core\Mage\Core\Block\Template.php(286): Mage_Core_Block_Template->renderView()
    #23 D:\xampp\htdocs\magentoma\ma_furniturestore\app\code\core\Mage\Core\Block\Abstract.php(863): Mage_Core_Block_Template->_toHtml()
    #24 D:\xampp\htdocs\magentoma\ma_furniturestore\app\code\core\Mage\Core\Model\Layout.php(529): Mage_Core_Block_Abstract->toHtml()
    #25 D:\xampp\htdocs\magentoma\ma_furniturestore\app\code\core\Mage\Core\Controller\Varien\Action.php(391): Mage_Core_Model_Layout->getOutput()
    #26 D:\xampp\htdocs\magentoma\ma_furniturestore\app\code\core\Mage\Cms\Helper\Page.php(132): Mage_Core_Controller_Varien_Action->renderLayout()
    #27 D:\xampp\htdocs\magentoma\ma_furniturestore\app\code\core\Mage\Cms\Helper\Page.php(52): Mage_Cms_Helper_Page->_renderPage(Object(Mage_Cms_IndexController), ‘home’)
    #28 D:\xampp\htdocs\magentoma\ma_furniturestore\app\code\core\Mage\Cms\controllers\IndexController.php(45): Mage_Cms_Helper_Page->renderPage(Object(Mage_Cms_IndexController), ‘home’)
    #29 D:\xampp\htdocs\magentoma\ma_furniturestore\app\code\core\Mage\Core\Controller\Varien\Action.php(420): Mage_Cms_IndexController->indexAction()
    #30 D:\xampp\htdocs\magentoma\ma_furniturestore\app\code\core\Mage\Core\Controller\Varien\Router\Standard.php(250): Mage_Core_Controller_Varien_Action->dispatch(‘index’)
    #31 D:\xampp\htdocs\magentoma\ma_furniturestore\app\code\core\Mage\Core\Controller\Varien\Front.php(176): Mage_Core_Controller_Varien_Router_Standard->match(Object(Mage_Core_Controller_Request_Http))
    #32 D:\xampp\htdocs\magentoma\ma_furniturestore\app\code\core\Mage\Core\Model\App.php(347): Mage_Core_Controller_Varien_Front->dispatch()
    #33 D:\xampp\htdocs\magentoma\ma_furniturestore\app\Mage.php(640): Mage_Core_Model_App->run(Array)
    #34 D:\xampp\htdocs\magentoma\ma_furniturestore\index.php(80): Mage::run(”, ‘store’)
    #35 {main}

  16. Hi
    I have magento 1.6.2.0
    where exatle a have to put the inchoo dir???
    i put in
    \app\design\frontend\base\default\template\inchoo…

    in this case, the block is same????
    {{block type=”core/template” template=”inchoo/bestseller.phtml”}}

  17. zoran

    Is anyone manage to get this to work on magento 1.7 ?

  18. Srdjan

    Functionality is pretty much trivial, you can start from here:
    http://www.netismine.com/magento/get-bestsellers-collection

  19. kea

    it works in magento 1.7.2, can you filter weekly seller?

  20. daniel

    Hi, I tried your code, but nothing was showed on my home screen. No data was displayed. I work with magento 1.7 and I have a lot of problems with it. Maybe you know something what went wrong. I used your latest bestseller.zip
    Greetings
    Daniel

  21. Works well, but I found that enabling flat products breaks this code, untill you add an innerjoin and move the visibility filter down…

    e.g

            /* @var $products Mage_Reports_Model_Resource_Product_Collection */
            $products = Mage::getResourceModel('reports/product_collection')
                //-&gt;addAttributeToSelect('*')
                -&gt;addAttributeToSelect(array('name','price','small_image'))
                -&gt;addOrderedQty()
                -&gt;setStoreId($storeId)
                -&gt;addStoreFilter($storeId)
                -&gt;setOrder('ordered_qty','desc'); //best sellers on top
                if ($this-&gt;isEnabledFlat()) {
                    $products-&gt;getSelect()-&gt;joinInner(array('e2' =&gt; 'catalog_product_flat_'.$storeId), 'e2.entity_id = e.entity_id');
                }
                $products-&gt;addAttributeToFilter('visibility', $visibility);
    
  22. On my last comment

    if ($this-&gt;isEnabledFlat()) { 

    should have been

    if (Mage::helper('catalog/product_flat')-&gt;isEnabled()) {
  23. Nhuchhe

    Bestseller is not working for Bundled product. Can you please explain it.
    Thanks.

  24. walker

    hello
    thank you for useful post, magneto gives us great base potential for ecommerce. But there are some cases when base magento functionality is not enough and i think that for bestsellers would be suitable use an extension with big potential. i’m speaking about http://amasty.com/improved-sorting.html

    i think that it would be interesting to read some information.

    thank you

1 2

Add Your Comment

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