>How to Display Best Seller Products in Magento 1

How to Display Best Seller Products in Magento 1

How do store owners display best seller products in thousands of items at their online store?

This is a popular question, we have had over recent days from our customers when they need to show the most highlighted products on their Homepage, Category View page, Product page… Unfortunately, there’s no such default feature in Magento 1.

Today we will introduce how to create best seller products by ownself at the article.

Create module

First of all, we need to create module to list best seller products.

Create block file with path app/code/local/Bss/Bestseller/Block/Bestsellers.php:

<?php
class Bss_Bestseller_Block_Bestsellers extends Mage_Core_Block_Template
{
    public function getBestsellerProducts($limit = 10)
    {
        if($limit == "") $limit = 10;
        $storeId = (int) Mage::app()->getStore()->getId();
 
        $collection = Mage::getResourceModel('catalog/product_collection')
            ->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
            ->addStoreFilter()
            ->addPriceData()
            ->addTaxPercents()
            ->addUrlRewrite()
            ->setPageSize($limit);
 
        $collection->getSelect()
            ->joinLeft(
                array('aggregation' => $collection->getResource()->getTable('sales/bestsellers_aggregated_monthly')),
                "e.entity_id = aggregation.product_id AND aggregation.store_id={$storeId}",
                array('SUM(aggregation.qty_ordered) AS sold_quantity')
            )
            ->group('e.entity_id')
            ->order('sold_quantity DESC');
        
        Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
        Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);
 
        return $collection;
    }
}

Create config file with path app/code/local/Bss/Bestseller/etc/config.xml:

<?xml version="1.0"?>
<config> 
  <modules> 
    <Bss_Bestseller> 
      <version> 0.1.0</version> 
    </Bss_Bestseller> 
  </modules> 
  <global> 
    <blocks> 
        <bestseller> 
            <class> Bss_Bestseller_Block</class> 
        </bestseller> 
    </blocks> 
  </global> 
</config> 

Create define file with path app/etc/ modules/Bss_Bestseller.xml:

<?xml version="1.0"?>
<config>
  <modules>
    <Bss_Bestseller>
      <active>true</active>
      <codePool>local</codePool>
      <version>0.1.0</version>
    </Bss_Bestseller>
  </modules>
</config>

Frontend

After completing module, we need to display the best seller products on the frontend. We can set them at everywhere we want by creating cms page. Go to CMS -> select Pages -> Add New Page.

In the content section of the cms page, we fill in:

{{block type="bestseller/bestsellers" template="catalog/product/bestseller.phtml"}}

For the new Magento version, you need to add the catalog/product_list to the System section. Select Permissions -> Blocks -> Add New Block.

Go to app/code/design/frontend/vendor_name/theme_name/template/catalog/product/ and create bestseller.phtml:

<?php
$_productCollection=$this->getBestsellerProducts(10);  
foreach ($_productCollection as $product):            
…
endforeach;
?>

In which:

  • The function getBestsellerProducts(10) – 10 is limited product.
  • Add template which you want to display (you can use the list product template from catalog/product/list.phtml).

The result

Let’s see the result we just created:

< Previous Post
Next Post >