0

I have use this code, products are coming but images are not showing. anyone can correct this code. thanks

  <?php
$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(); ?>

<?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, 'small_image')->resize(300,300);
        $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 } ?>
2
  • did you check the products has images and checked small image prntscr.com/fwkpgs
    – Raghu
    Commented Jul 17, 2017 at 4:51
  • Yes images are added in all products. i think issue in this line. $image_url = $this->helper('catalog/image')->init($_product, 'small_image')->resize(300,300); Commented Jul 17, 2017 at 7:58

1 Answer 1

0

I tried it and it works, don't forget to set a Small image for each product in admin settings : catalog->product manager->{select your product}->images->Small image.

$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">
    <?php if ($_collectionSize) { ?>
        <h3 class="subtitle">Most Viewed Products</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>
1

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.