Saturday, 31 October 2015

How to get Most Viewed Products of Current Product Category in Magento - Magento

Get most viewed products of current product categorywise in magento

$storeId = Mage::app()->getStore()->getId();
$category_ids = $_product->getCategoryIds();

$products = Mage::getResourceModel('reports/product_collection')
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('category_ids',array('finset'=>$category_ids))
    ->setStoreId($storeId)
    ->addStoreFilter($storeId)
    ->addViewsCount()

Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($products);
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($products);

foreach($products as $product){
    echo $product->getName();
    echo $product->getPrice();
}

No comments:

Post a Comment