1

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"}}
2

4 Answers 4

1

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!

1
  • thanks for your answer but is shows error Commented Mar 20, 2019 at 12:49
0

phtml file :-

<?php echo $this->getLayout()->createBlock("Magento\Catalog\Block\Widget\RecentlyViewed")->setDisplayType("widget_recently_viewed")->setProductsCount("5")->setTemplate("product/widget/viewed/grid.phtml")->toHtml(); ?>
1
  • hi @Rathod, This is not working in 2.3.5 Commented Feb 24, 2021 at 13:47
0

Try this :

echo $this->getLayout()->createBlock("Magento\Catalog\Block\Widget\RecentlyViewed")->setDisplayType("your_display_type")->toHtml();
0

If you only have such type of string

$widget = '{{widget type="Magento\Cms\Block\Widget\Block" template="widget/static_block/default.phtml" block_id="100"}}';

you can show widget content without createBlock etc.

You need to add \Magento\Widget\Model\Template\Filter to your block or create it via Object Manager in phtml.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
/** @var $filter \Magento\Widget\Model\Template\Filter */
$filter = $objectManager>create(\Magento\Widget\Model\Template\Filter::class);

Than you can show your widget like this

<?= html_entity_decode($filter->filter($widget)) ?>

Works even if $widget contains other html tags, they will also be shown.

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.