1

I have a widget that shows a few products on a category page. This works if there is one instance of the widget however if i have a second widget the filters seem to reapply to the existing collection leaving no products in every instance after the first.

public function getProducts() {
    //print_r(get_class_methods($this->_productCollection));
    $this->_productCollection->clear();
    $productcollection = $this->_productCollection
        ->addAttributeToSelect('*')
        ->addAttributeToFilter($this->getAttributeFilter(), $this->getAttributeValue())
        ->addCategoriesFilter(['eq' => $this->_registry->registry('current_category')->getId()])
        ->setPageSize($this->getProductLimit())
        ->setCurPage(1)
        ->load();

    return $productcollection;
}

I have attempted to clear with $this->_productCollection->clear(); however still I have empty collections.

2

Instead of reusing the same productColletion and trying to clear this instance. You can create a new collection with the CollectionFactory:

In your __construct use:

public function __construct(
    \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $collectionFactory,
} {
    $this->collectionFactory = $collectionFactory;
}

Then create a new collection for each widget:

$collection = $this->collectionFactory->create();
$collection->addAttributeToSelect('*');
// Etc

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.