4

I need to display the most viewed products on home page.without any plugin installation. please help me!

  • What's your logic for Most Popular Products ? – Anil Suthar Aug 6 '16 at 5:42
  • Most Viewed Products – Crock Aug 6 '16 at 6:00
5

Use below code to get Most Viewed products collection :

<?php
$storeId = Mage::app()->getStore()->getId();

$_productCollection = Mage::getResourceModel('reports/product_collection')
    ->addAttributeToSelect('*')
    ->setStoreId($storeId)
    ->addStoreFilter($storeId)
    ->addViewsCount()
    //->setPageSize($totalPerPage)
    ;
?>

For more details, check List Popular Products

UPDATE : In admin, go to CMS > Pages, edit Homepage.

-- In Content area, add this line : {{block type="catalog/product" template="catalog/product/most-viewed.phtml"}} and SAVE.

-- Now create a new template file at /app/design/frontend/PACKAGE/THEME/template/catalog/product/, and name it most-viewed.phtml

-- Add this code in most-viewed.phtml file, (this will show max 10 products, edit $totalPerPage variable as you require)

<?php
$totalPerPage = 10;
$storeId = Mage::app()->getStore()->getId();
$_productCollection = Mage::getResourceModel('reports/product_collection')
    ->addAttributeToSelect('*')
    ->setStoreId($storeId)
    ->addStoreFilter($storeId)
    ->addViewsCount()
    ->setPageSize($totalPerPage)
    ;
?>
<?php $_collectionSize = $_productCollection->count(); ?>

<?php if ($_collectionSize) { ?>
    <h2 class="subtitle">Most Viewed Products</h2>
    <ul class="products-grid">
        <?php
        foreach ($_productCollection as $_product):
        $image_url = $this->helper('catalog/image')->init($_product, 'thumbnail')->setWatermarkImageOpacity(0)->keepFrame(false)->resize(200,200);
        $alt = $this->htmlEscape($this->stripTags($this->getImageLabel($_product, 'small_image'), null, true));
        ?>
        <li class="item">
            <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>">
                <img src="<?php echo $image_url; ?>" alt="<?php echo trim($alt)? $alt: $this->htmlEscape($this->stripTags($_product->getName())); ?>" />
            </a>
            <div class="product-info">
                <h2 class="product-name">
                    <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>">
                        <?php echo trim($alt)? $alt: $this->htmlEscape($this->stripTags($_product->getName())); ?>
                    </a>
                </h2>
            </div>
        </li>
        <?php endforeach; ?>
    </ul>
<?php } ?>
  • In that link and Mentioned the answer above, Both are does not tells which which files has to modify and all – Crock Aug 6 '16 at 6:17
  • @Crock: this is a general code, you can use it in any phtml (already mentioned in that link). Check updated answer – Anil Suthar Aug 6 '16 at 6:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.