Magento – How to Render Widget (Product List) Inside PHTML

widget

I want to group a list of products by Attribute set.

What i did is, I created a CMS page, where I inserted a custom PHTML file. In this file I obtain all my Attributes set.

After the custom block, I want to put widgets of products list, filtering with the id's of Attributes I got in the custom PHTML:

{{widget type="Magento\CatalogWidget\Block\Product\ProductsList" title="polosde animales " show_pager="0" products_count="10" template="product/widget/content/grid.phtml" conditions_encoded="^[`1`:^[`type`:`Magento||CatalogWidget||Model||Rule||Condition||Combine`,`aggregator`:`all`,`value`:`1`,`new_child`:``^],`1--1`:^[`type`:`Magento||CatalogWidget||Model||Rule||Condition||Product`,`attribute`:`category_ids`,`operator`:`==`,`value`:`28`^],`1--2`:^[`type`:`Magento||CatalogWidget||Model||Rule||Condition||Product`,`attribute`:`attribute_set_id`,`operator`:`==`,`value`:`4`^]^]"}}

My question is: how should I render the widget? How can I render the widget inside the PHTML ? is that recommended? should I pass my variables to the CMS page and render the widget there?

Best Answer

I found the solution

$productsBlock = $this->getLayout()->createBlock(\Magento\CatalogWidget\Block\Product\ProductsList::class);
$productsBlock->setTitle("My products");
$productsBlock->setProductsCount(5);
$productsBlock->setTemplate("product/widget/content/grid.phtml");
$productsBlock->setConditionsEncoded("^[`1`:^[`type`:`Magento||CatalogWidget||Model||Rule||Condition||Combine`,`aggregator`:`all`,`value`:`1`,`new_child`:``^],`1--1`:^[`type`:`Magento||CatalogWidget||Model||Rule||Condition||Product`,`attribute`:`category_ids`,`operator`:`==`,`value`:`28`^],`1--2`:^[`type`:`Magento||CatalogWidget||Model||Rule||Condition||Product`,`attribute`:`attribute_set_id`,`operator`:`==`,`value`:`4`^]^]");

echo $productsBlock->toHtml();
Related Topic