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() {
    $productcollection = $this->_productCollection
        ->addAttributeToFilter($this->getAttributeFilter(), $this->getAttributeValue())
        ->addCategoriesFilter(['eq' => $this->_registry->registry('current_category')->getId()])

    return $productcollection;

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


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();
// 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.