Magento – Display Widget in phtml file magento 2

magento-2.1magento2magento2.3productwidgets

I have Following widget and its working in cms page but now I want it to display in phtml file

{{widget type="Magento\Catalog\Block\Widget\RecentlyViewed" uiComponent="widget_recently_viewed" page_size="10" 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"}}

Best Answer

Try with below way.

$recViewBlock = $this->getLayout()->createBlock(\Magento\Catalog\Block\Widget\RecentlyViewed::class);
$recViewBlock->setTitle("Recently Viewed Products");
$recViewBlock->setProductsCount(10);
$recViewBlock->setTemplate("product/widget/viewed/grid.phtml");
$recViewBlock->setDisplayType("recently.view.products");
$recViewBlock->setShowAttribute(add_to_cart,add_to_compare,add_to_wishlist);

echo $recViewBlock->toHtml();

Note : Above code is not tested you have to check. For more detail click here

I hope it helps!

Related Topic