1

I try to display list of products with the widget ProductsList, without success.

I have the following code :

$block = $this->_view->getLayout()->createBlock('\Magento\CatalogWidget\Block\Product\ProductsList');
$condition = [
   '1' => [
       'type' => \Magento\CatalogWidget\Model\Rule\Condition\Combine::class,
       'aggregator' => 'all',
       'value' => '1',
       'new_child' => '',
   ],
   '1--1' => [
       'type' => \Magento\CatalogWidget\Model\Rule\Condition\Product::class,
       'attribute' => 'sku',
       'operator' => '==',
       'value' => 'my_sku',
   ]
];
$conditionEncoded = $this->conditionsHelper->encode($condition); // conditionsHelper = \Magento\Widget\Helper\Conditions
$block->setConditions($conditionEncoded);
$block->toHtml();

Currently, I only try to display 1 sku, but at the end, I want to display a list of sku. However, $block->toHtml(); return empty and I din't get any error.

Then, I try to debug :

$block->toHtml() call protected function _beforeToHtml() in ProductList.php. In this function, we have $this->setProductCollection($this->createCollection()); In createCollection, I display the $collection->getSelect()->__toString(). This request is good and return my sku. So why nothing is displaying with ->toHtml() ?

Do you know where can I get a documentation about how to create this $condition ?

1 Answer 1

0

Try to use it as

$block = $this->_view->getLayout()->createBlock('\Magento\CatalogWidget\Block\Product\ProductsList');
$condition = [
   '1' => [
       'type' => \Magento\CatalogWidget\Model\Rule\Condition\Combine::class,
       'aggregator' => 'all',
       'value' => '1',
       'new_child' => '',
   ],
   '1--1' => [
       'type' => \Magento\CatalogWidget\Model\Rule\Condition\Product::class,
       'attribute' => 'sku',
       'operator' => '==',
       'value' => 'my_sku',
   ]
];
$block->setConditions($condition);
$block->toHtml();

Note: No need to encode it for setConditions() method

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.