cancel
Showing results for 
Search instead for 
Did you mean: 

Products image not showing when add ->addViewsCount() in most-viewed products

Products image not showing when add ->addViewsCount() in most-viewed products

I have added  a new module “Most Viewed Products” Block in product view page 

public function getViewedProducts()
{

$storeId = $this->_storeManager->getStore()->getId();
$getViewedProducts = $this->_reportsCollectionFactory->create()
->addAttributeToSelect('*')
->addViewsCount()
->setStoreId($storeId)
->addStoreFilter($storeId);

return $getViewedProducts;
}

and success.phtml

 

<?php
$products = $this->getViewedProducts();

if ($products->count()) { ?>
<h2 class="subtitle">Most Viewed Products</h2>
<ul class="product-items widget-product-grid">
<?php foreach($products as $product) :
$productUrl = $product->getUrlModel()->getUrl($product);


$_imagehelper = $this->helper('Magento\Catalog\Helper\Image');
$productImage = $_imagehelper->init($product, 'category_page_list')->constrainOnly(FALSE)->keepAspectRatio(TRUE)->keepFrame(FALSE)->resize(400)->getUrl();
?>
<li class="product-item" style="float:left;">
<a href="<?php echo $productUrl; ?>" class="product-item-link"><img src="<?php echo $productImage; ?>"/></a>
<div class="product-info">
<h2 class="product-name">
<a href="<?php echo $productUrl; ?>">
<?php echo $product->getName(); ?>
</a>
</h2>
</div>
</li>
<?php endforeach;?>
</ul>
<?php } ?>

 

Is working but  Products image not showing when add ->addViewsCount() in most-viewed products

I hope your help, Thanks