Friday, 8 July 2011

how to get mostviewed products in magento?

[Step 1] Create a Mostviewed.php file and put it here :
app/code/local/Mage/Catalog/Block/Product/Mostviewed.php

class Mage_Catalog_Block_Product_Mostviewed 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)
            ->addViewsCount();
        Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($products);
        Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($products);
 
        $products->setPageSize(5)->setCurPage(1);
        $this->setProductCollection($products);
    }
}

[Step 2] Create a mostviewed.phtml file and put it here :
app/design/frontend/*/*/template/catalog/product/mostviewed.phtml

getProductCollection()) && $_products->getSize()): ?>
__('These Products Are Popular Right Now!') ?>
getItems()); echo $_collectionSize; ?>

[Step 3] now put this line where you want to view mostviewed products..

{{block type="catalog/product_mostviewed" template="catalog/product/mostviewed.phtml"}}

Source : http://harisur.net/how-to-get-mostviewed-products-in-magento

No comments:

Post a Comment