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 ?

4
  • Have you tried this ? stackoverflow.com/a/27755629/7545026 Commented Oct 3, 2017 at 7:32
  • <?php echo $this->getLayout()->createBlock('reports/product_viewed')->setTemplate('reports/product_viewed.phtml')->toHtml(); ?> Where I have to add this one Commented Oct 3, 2017 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"}} Commented Oct 3, 2017 at 7:58
  • its not working. As I want to check the report.xml there is no report.xml Commented Oct 3, 2017 at 8:06

1 Answer 1

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"}}
    
1
  • thank you for the code. But it is not working Commented Oct 3, 2017 at 9:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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