2

I am having problem with collection filter

I am using event catalog_product_collection_apply_limitations_after to add collection limitations

In the observer i am using below code to apply filter.

public function execute(\Magento\Framework\Event\Observer $observer)
{
    $collection = $observer->getEvent()->getCollection();
    $collection->addFieldToFilter('manufacturer','218');
    return $this;
}

It is throwing Exception #0 (RuntimeException): Illegal state

From

Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection::addFieldToFilter($field, $condition = null)

there is code that check $this->searchResult

 if ($this->searchResult !== null) {
        throw new \RuntimeException('Illegal state');
    }

check exception trace here

https://textsaver.flap.tv/lists/23zw

How to fix this if i remove throw new \RuntimeException('Illegal state'); then it is working but it's not a good idea right ?

1

You need to apply all filters prior a collection is loaded. If a search result is not null it means the collection is actually loaded. At least you need to check if it is loaded or not by means of the collection method "isLoaded".

| 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.