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();
    return $this;

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


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


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

  • thanks, it works! Jun 22, 2020 at 22:41
  • i am facing the similar issue. how can i fix it ? i am using this "$collection->addFieldToFilter('price',['from' => $pricerange[0], 'to' => $pricerange[1]]); "
    – Ankit Shah
    Mar 26, 2021 at 4:39

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.