so In my scenario I need Recently Viewed Products in new custom page... n I also want to show the recently viewed products while the user is not logged In.
Vender/Module/view/frontend/layout/recently_index_index
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<referenceContainer name="content">
<block class="Vender\Module\Block\Viewedd" name="recently" template="Vender_Module::recently.phtml" cacheable="false" />
</referenceContainer>
</page>
Ok so this layout is really important in this layout as you can see that cacheable = "false"
this is the thing where I was stuck around 3 days. If you want to show Recently Viewed Products If the user is not logged In then you have to false the cacheable. now
Vender\Module\Block\Viewedd.php
<?php
namespace Vender\Module\Block;
use Magento\Framework\View\Element\Template\Context;
use Magento\Reports\Block\Product\Viewed;
use Magento\Catalog\Model\ProductFactory;
class Viewedd extends \Magento\Framework\View\Element\Template
{
/**
* Limit of orders
*/
const ORDER_LIMIT = 4; //Image Limit this can be changed from here
/**
* @param \Magento\Framework\View\Element\Template\Context $context
* @param array $data
*/
public function __construct(
Context $context,
Viewed $recentlyViewed,
ProductFactory $productFactory,
array $data = []
) {
$this->_recentlyViewed = $recentlyViewed;
$this->_productFactory = $productFactory;
parent::__construct($context, $data);
}
/**
* @return void
*/
protected function _construct()
{
parent::_construct();
$this->getRecentlyviewed();
}
/**
* Get recently placed orders. By default they will be limited by 5.
*/
public function getRecentlyviewed()
{
$recentViewedCollection = $this->_recentlyViewed->getItemsCollection()->setPageSize(
self::ORDER_LIMIT
)->load();
;
$this->setRecentlyviewedcol($recentViewedCollection);
return $this->getRecentlyviewedcol();
}
/**
* @return string
*/
protected function _toHtml()
{
if ($this->getRecentlyviewedcol()->getSize() > 0) {
return parent::_toHtml();
}
return parent::_toHtml();
}
public function getProductLoader()
{
return $this->_productFactory;
}
}
now in this class as you can see first we get recently viewed Products in getRecentlyviewed()
function. now after that we need to create our phtml
file
Vender/Module/view/frontend/templates/recently.phtml
<h1>Recently Viewed Products</h1>
<?php
$col = $this->getRecentlyviewedcol();
$count = count($col);
$_productloader = $this->getProductLoader();
?>
<?php if($count): ?>
<div class="wishtlist-recently-viewed">
<div class="d-recently-title">
<h3>Recently Viewed</h3>
</div>
<div class="d-view-blocks">
<ul>
<?php foreach ($col as $item) : ?>
<?php
$loadedProduct = $_productloader->create()->load($item->getEntityId());
$i = $this->helper('Magento\Catalog\Helper\Image')->init($loadedProduct, 'product_page_image_large');
?>
<li class="col-12 col-lg-3 col-md-3 col-sm-12">
<div class="d-slide-box">
<div class="d-block-div ">
<div class="d-wishlist-img">
<a href="<?=$loadedProduct->getProductUrl();?>"><img style="width:300px;height:350px;" class="img-fluid" src="<?=$i->getUrl();?>" alt="First slide"></a>
</div>
<div class="d-view-title-inner d-view-title-inner-r-v">
<div class="d-wishlist-title">
<a href="<?=$loadedProduct->getProductUrl();?>"><h3><?=$loadedProduct->getName()?></h3></a>
</div>
</div>
</div>
</div>
</li>
<?php endforeach; ?>
</ul>
</div>
</div>
<?php endif;?>
In this you'll get all the Recently Viewed Products with images and with Product Name.
Note: firstly I was trying this on the HomePage using the above other solutions but it was not working you need to create new layout or maybe put the xml
block in your Layout hope it will work for you.