3

Approach One

  • Login to Admin panel
  • Click on Content -> Blocks
  • Create new product-detail block and edit then toggle show/hide editor which will show insert widget option
  • Click on insert widget select "Recently Viewed Products" from dropdown, add number of product to display (ex - 5)
  • Select template from dropdown then click on insert widget
  • Click on save Block

Although <p>{{widget type="Magento\Catalog\Block\Widget\RecentlyViewed" uiComponent="widget_recently_viewed" page_size="5" show_attributes="name,image,price" show_buttons="add_to_cart" template="product/widget/viewed/grid.phtml"}}</p> works in home-page block

Approach Two

For testing purpose added below code in vendor/magento/theme-frontend-luma/Magento_Catalog/layout/catalog_product_view

<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">5</argument>
</action>

Final code for catalog_product_view

<page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <move element="page.main.title" destination="product.info.main" before="-"/>
        <move element="product.info.stock.sku" destination="product.info.price" after="product.price.final"/>
        <move element="product.info.review" destination="product.info.main" before="product.info.price"/>

         <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">5</argument>
            </action>
        </block>
    </body>
</page>

Did reindexing and cleared cache too but both the above approaches are not working.

  • How could I make this work and show recently viewed products block on product detail page ?
  • Does Magento 2 supports local.xml just like earlier mage versions ?
  • Above section is not working for you? – Rakesh Jesadiya Nov 3 '17 at 6:39
  • You can directly call recently product in template file using call block no need to do xml or other way. – Rakesh Jesadiya Nov 3 '17 at 6:43
  • What is the correct approach ? Using xml/call directly in template/create widget from admin ? – Slimshadddyyy Nov 3 '17 at 9:04
11

Unfortunately the accepted answer didn't work for me and I wasn't able to use widget instance layout updates in my use case

If for whatever reason you need to add a 'Recently Viewed' products block specifically via layout xml, here's my solution (tested in version 2.2.2):

I decided to use the same class Magento\Catalog\Block\Widget\RecentlyViewed which the 'Recently Viewed' Product widget uses in my block declaration.

<!-- Basic recently viewed products block -->
<block class="Magento\Catalog\Block\Widget\RecentlyViewed" name="recently_viewed" template="Magento_Catalog::product/widget/viewed/grid.phtml" after="-">
    <arguments>
        <argument name="uiComponent" xsi:type="string">widget_recently_viewed</argument>
        <argument name="page_size" xsi:type="number">4</argument>
    </arguments>
</block>

This will render a simple grid with the last 4 products view by the user.

You can configure the block further, just as you would the widget instance:

<!-- Configured recently viewed products block -->
<block class="Magento\Catalog\Block\Widget\RecentlyViewed" name="recently_viewed" template="Magento_Catalog::product/widget/viewed/grid.phtml" after="-">
    <arguments>
        <argument name="uiComponent" xsi:type="string">widget_recently_viewed</argument>
        <argument name="page_size" xsi:type="number">4</argument>
        <!-- 'Product attributes to show' configuration -->
        <argument name="show_attributes" xsi:type="string">name,image,price,learn_more</argument>
        <!-- 'Buttons to show' configuration -->
        <argument name="show_buttons" xsi:type="string">add_to_cart,add_to_compare,add_to_wishlist</argument>
    </arguments>
</block>

One thing to note is that the argument values for show_attributes and show_buttons need to be declared as comma delimited strings (no spaces).

Declaring them as arrays or including spaces next to your comma's won't work due to the way the UI Component parse the content before Knockout renders it.

And since I said Knockout... it should play nicely with FPC, should.

Finally this solution doesn't use the 'action' instruction which appears to have been deprecated

  • This should be the accepted answer. cacheable="false" makes the whole page uncacheable which is not advisable. – Dynomite Jul 11 '18 at 13:53
  • Where are the above files? – Chris K Aug 1 at 16:14
  • @ChrisK The code snippets need to be added to layout xml files, which one depends on what you are trying to achieve. If you are unfamiliar with with layout xml check the docs here, there will also be other questions on here related to layout xml in m2 – Chedaroo Aug 1 at 16:56
6

Recently Viewed Products product details page now try this.

enter image description here

enter image description here

enter image description here

  • This is the best answer so far as it works natively and is compatible with FPC – Dan Norris Jan 13 '18 at 12:53
  • Magento 2.3.2 recently viewed widgets do not work. – Chris K Aug 1 at 16:15
3

Just keep below code in your template file for get recently viewed products in any place,

<?php echo $this->getLayout()->createBlock("Magento\Reports\Block\Product\Widget\Viewed")->setDisplayType("recently.view.products")->setProductsCount("5")->setTemplate("widget/viewed/content/viewed_list.phtml")->toHtml(); ?>
  • If I am testing with luma theme, in which template file, should I place above snippet ? – Slimshadddyyy Nov 3 '17 at 6:46
  • You can placed any file in your system its working fine. – Rakesh Jesadiya Nov 3 '17 at 6:49
  • @Vikram Please let me know if you have any issue. – Rakesh Jesadiya Nov 3 '17 at 7:08
  • You can add it inside vendor/magento/module-catalog/view/frontend/templates/product/view/form.phtml – Rakesh Jesadiya Nov 3 '17 at 7:10
  • 3
    Bad approach. Avoid creating blocks in PHP if you can. Main reason: your blocks could be updated from other layout updates and Layout engine will handle errors. – Kirby Feb 28 '18 at 21:43
-2

I got it working by using below code in vendor/magento/theme-frontend-luma/Magento_Catalog/layout/catalog_product_view.xml for testing purpose

<referenceContainer name="content">
  <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>
</referenceContainer>

You can add above snippet to catalog_product_view.xml file of your custom theme.

  • 7
    cacheable="false" - will disable page caching completely. – Kirby Feb 28 '18 at 21:41
  • Downvotes should be explained, upvoting to counteract. – Chris K Aug 1 at 16:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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