1

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?

0

the safe way would be to add it to a cms block and after that render the block in phtml file or you can try to implement something like the following code but you have to find a way to pass the conditions.

<?php echo $this->getLayout()->createBlock("Magento\Catalog\Block\Product\ProductsList.php")->setTemplate("template_file.phtml")->toHtml(); ?>
| improve this answer | |
0

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();
| improve this answer | |

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.