13

I have a widget:

{{widget type="Magento\Catalog\Block\Product\Widget\NewWidget" display_type="all_products" products_count="5" template="product/widget/new/content/new_grid.phtml"}}

So it works in the content of the CMS block or CMS page. I want to show it in a .phtml file, how can I do it?

1
  • Why you prefer add widget in phtml rather than in layout xml?
    – TFS
    Jul 30, 2016 at 10:56

3 Answers 3

14

For Display Type = All Product Widget

{{widget type="Magento\Catalog\Block\Product\Widget\NewWidget" 
display_type="all_products" products_count="5" template="product/widget/new/content/new_grid.phtml"}}

and

<?php echo $this->getLayout()->createBlock("Magento\Catalog\Block\Product\Widget\NewWidget")->setDisplayType("all_products")->setProductsCount("5")->setTemplate("product/widget/new/content/new_grid.phtml")->toHtml(); ?>

For Display Type = New Product Widget

{{widget type="Magento\Catalog\Block\Product\Widget\NewWidget" display_type="new_products" products_count="5" template="product/widget/new/content/new_grid.phtml"}}

and

<?php echo $this->getLayout()->createBlock("Magento\Catalog\Block\Product\Widget\NewWidget")->setDisplayType("new_products")->setProductsCount("5")->setTemplate("product/widget/new/content/new_grid.phtml")->toHtml(); ?>
2
  • This not working in Magento 2.1.1
    – thdoan
    Oct 7, 2016 at 7:22
  • check update its works fine Oct 8, 2016 at 5:22
1

If we take a look at the customer module, for example:

vendor/magento/module-customer/view/frontend/templates/form/edit.phtml

<?php $_dob = $block->getLayout()->createBlock('Magento\Customer\Block\Widget\Dob') ?>
<?php if ($_dob->isEnabled()): ?>
    <?php echo $_dob->setDate($block->getCustomer()->getDob())->toHtml() ?>
<?php endif ?>

As we can see, we can call a widget directly in the phtml template: $block->getLayout()->createBlock('your\widget').

-1

Try the following code and check if it works:

<?php
    $filter = new Mage_Widget_Model_Template_Filter();
    $_widget = $filter->filter('{{widget type="cms/widget_page_link" template="cms/widget/link/link_block.phtml" page_id="2"}}');
    echo $_widget; 
?>

Where page_id="2" is my widget is just replaced with your widget id .

3

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.