In Magento 2 display recently viewed product on product detail page

7.3k Views Asked by At

I want to display recently viewed products on product detail page in Magento 2. I am using Magento ver. 2.1.2. Please assist me how to display it. Thanks

3

There are 3 best solutions below

0
On BEST ANSWER

in your catalog_product_view.xml file add below block in

        <block class="Magento\Reports\Block\Product\Widget\Viewed" after="-" name="recently_viewed" cacheable="false" template="Magento_Reports::widget/viewed/content/viewed_grid.phtml">
            <action method="setPageSize">
                <argument name="page_size" xsi:type="number">4</argument>
            </action>
        </block>
1
On
\Magento\Reports\Model\ResourceModel\Product\CollectionFactory 
$_reportCollectionFactory

$this->_reportCollectionFactory = $_reportCollectionFactory;

$collection = $this->_reportCollectionFactory->create()
    ->addAttributeToSelect('*')
    ->addViewsCount()
    ->setStoreId($storeId)
    ->addStoreFilter($storeId)
    ->setPageSize(10); 
1
On

We should not set cacheable="false". This will greatly impact the performance. The whole page will be uncacheable.

1) 2.1.x: seems that it's a Magento bug with FPC and Varnish: https://github.com/magento/magento2/issues/3890. In this case, I used Ajax.

2) 2.2.x or later: using UI component: https://magento.stackexchange.com/a/209658/33057