I want to show recently searched products on homepage without using Recently Viewed Products Widget I want to get this in my custom block and also want to show this in my custom .phtml file how it would be possible?

  • You can use recently viewed product options instead of recently search, because when you search you will get list of other products as well. Commented Jun 17, 2019 at 9:51
  • Sorry it was a mistake... actually it is Recently Viewed Products Widget now I want to create the block to get all recently viewed product... how it would be possible Sir?
    – Asad Khan
    Commented Jun 17, 2019 at 10:12
  • Please check my ans Commented Jun 17, 2019 at 10:20

1 Answer 1


Please create a static block with the following code

{{widget type="Magento\Catalog\Block\Widget\RecentlyViewed" uiComponent="widget_recently_viewed" page_size="5" show_attributes="name,image,price,learn_more" show_buttons="add_to_cart,add_to_compare,add_to_wishlist" template="product/widget/viewed/grid.phtml" type_name="Recently Viewed Products"}}

Now, Call that static block in your phtml

<?php echo $this->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId('recently_viewed')->toHtml(); ?>

You can manage it via XML using following way.

<!-- Configured recently viewed products block -->
<block class="Magento\Catalog\Block\Widget\RecentlyViewed" name="recently_viewed" template="Magento_Catalog::product/widget/viewed/grid.phtml" after="-">
        <argument name="uiComponent" xsi:type="string">widget_recently_viewed</argument>
        <argument name="page_size" xsi:type="number">5</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>

Please clear cache and view some product pages, so the viewed product will where you want.

  • Sir it gives me an error Error filtering template: Invalid template file: 'product/widget/viewed/grid.phtml' in module: 'Magento_Reports' block's name: 'product\widget\viewed_0'
    – Asad Khan
    Commented Jun 17, 2019 at 11:04
  • Sorry Its my mistake, I have added wrong block before, I have just updated my ans, so please check and let me know if any issue Commented Jun 17, 2019 at 11:47
  • Sir now it gives me Error filtering template: Notice: Undefined index: attributes in /var/www/html/magento231/vendor/magento/framework/View/Element/UiComponentFactory.php on line 176
    – Asad Khan
    Commented Jun 17, 2019 at 12:01
  • Sir n I'm showing this on Homepage mean in grid.phtml may coz of this file it give me these issues
    – Asad Khan
    Commented Jun 17, 2019 at 12:02
  • Sir I also tried <?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(); ?> this does't give me any error but it also does't show any recent searched products
    – Asad Khan
    Commented Jun 17, 2019 at 12:27

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.