Tuesday, January 12, 2016

Display 'Best Selling' Products on Homepage in Magento

When you create an e–commerce website with magenta than its easy and most reliable for small or big store. For that you must create the best design for appearing website look more interesting to the customer. For any store home page is more suitable and the base for all stores. In Homepage you can add all content, features and functionality of the website. So it is more important that you must highlight some discount and products on the homepage.
In magento all Homepage content manage from admin panel CMS->Pages->Home page (If your default page is not Homepage than do that page as default page from System->Configuration->General->Web->Default Pages select 'Default Web URL' as cms and 'CMS Home Page' as 'Home Page').Here you can add any content, images and other default widgets in homepage, but when you want to search best selling products or latest products on the homepage than it’s not found any widget in default magenta than you must do simple customization for that.

If you want to add any categories product on homepage than add below code in CMS content

{{block type="catalog/product_list" column_count="3" category_id="2" template="catalog/product/list.phtml"}}

Here enter category id and category column in below code according to use.(If below code doesn’t work than allow permission block for `catalog/product_list` from System->Permissions->Blocks)

Display 'Best Selling' Products


  1. Create new file `Bestseller.php` in `app/code/local/Mage/Catalog/Block/Product/` folder with below code.
    
    class Mage_Catalog_Block_Product_Bestseller extends Mage_Catalog_Block_Product_Abstract{
        public function __construct(){
            parent::__construct();
            $storeId = Mage::app()->getStore()->getId();
            $products = Mage::getResourceModel('reports/product_collection')
                ->addOrderedQty()
                ->addAttributeToSelect('*')
                ->addAttributeToSelect(array('name', 'price', 'small_image'))
                ->setStoreId($storeId)
                ->addStoreFilter($storeId)
                ->setOrder('ordered_qty', 'desc'); // best selling item at first
            Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($products); //check product status
            Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($products); //check product visibility
     
            $products->setPageSize(3)->setCurPage(1);
            $this->setProductCollection($products);
        }
    }

  2. Create bestseller.phtml in file in app/design/frontend/default/THEME/template/catalog/product/ folder with below code.
    
    <?php if (($_products = $this->getProductCollection()) && $_products->getSize()):?>
    <h3>BEST SELLERS</h3>
    <ul>
      <?php foreach ($_products->getItems() as $_product): ?>
      <li> <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName())?>"><img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(275) ?>" width="275" alt="<?php echo $this->htmlEscape($_product->getName())?>" /></a> <a href="<?php echo $_product->getProductUrl();?>" title="<?php echo $this->escapeHtml($_product->getName()) ?>"><?php echo $this->escapeHtml($_product->getName()) ?></a> <?php echo $this->getReviewsSummaryHtml($_product) ?> <?php echo $this->getPriceHtml($_product, true) ?> </li>
      <?php endforeach;?>
    </ul>
    <?php endif; ?>
    
    

  3. Call below created block in cms homepage as below code.
    {{block type="catalog/product_bestseller" template="catalog/product/bestseller.phtml"}}

  4. Go to System->Permissions->Blocks and create new block with name 'catalog/product_bestseller' and 'Is Allowed' set 'Yes'.
Now clear all caches and check results.it's display Best selling products in homepage or any cms pages where you put below code.If there is no item in your order list than no products display in Best Seller.

1 comment :

  1. It features a special formula that contains no harmful peroxides, site

    ReplyDelete