0

The problem in showing the recently viewed product.

How can I show it in homepage using the block? Like , I show new products through this code

{{block type="catalog/product_new" products_count="2" template="catalog/product/new.phtml"}}

Is there any way to show the recently viewed product ?

  • Have you tried this ? stackoverflow.com/a/27755629/7545026 – Dinesh Yadav Oct 3 '17 at 7:32
  • <?php echo $this->getLayout()->createBlock('reports/product_viewed')->setTemplate('reports/product_viewed.phtml')->toHtml(); ?> Where I have to add this one – Swati Tyagi Oct 3 '17 at 7:34
  • If you want to show this on home page then you don't need to use above code. You can use this code in your cms page {{block type="reports/product_viewed" template="reports/product_viewed.phtml"}} – Dinesh Yadav Oct 3 '17 at 7:58
  • its not working. As I want to check the report.xml there is no report.xml – Swati Tyagi Oct 3 '17 at 8:06
0
  1. Create most-viewed.phtml file in : app/design/frontend/{packagename}/{themename}/template/catalog/product/most-viewed.phtml then add this:

    $totalPerPage =4;
    $storeId = Mage::app()->getStore()->getId();
    $_productCollection = Mage::getResourceModel('reports/product_collection')
                          ->addAttributeToSelect('*')
                          ->setStoreId($storeId)
                          ->addStoreFilter($storeId)
                          ->addViewsCount()
                          ->setPageSize($totalPerPage);
    ?>
    <?php $_collectionSize = $_productCollection->count(); ?>
    <div class="row" style="border: 1px solid #eaeaea; padding: 23px; margin-bottom: 10px;">
    <?php if ($_collectionSize) { ?>
        <h3 class="subtitle" style="background-color: #eaeaea; padding: 7px; color: #929292; text-transform: uppercase; font-size: 15px;">Les plus vus block</h3>
        <ul class="products-grid">
            <?php
            foreach ($_productCollection as $_product): ?>
            <div class="col-md-3">
            <?php
                $image_url = $this->helper('catalog/image')->init($_product, 'small_image')->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">
                        <h3 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>
                        </h3>
                    </div>
                </li>
            </div>
            <?php endforeach; ?>
        </ul>
    <?php } ?>
    </div>
    
  2. add the piece of code bellow in your cms block:

    {{block type="core/template" name="most.viewed.block" template="catalog/product/most-viewed.phtml"}}
    
| improve this answer | |
  • thank you for the code. But it is not working – Swati Tyagi Oct 3 '17 at 9:17

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.