5

I'm trying to update product collection (excluding some products, to be exact) through observer. I'm using the event catalog_block_product_list_collection Observer is catching the event fine.

in observer execute method:

    public function execute(\Magento\Framework\Event\Observer $observer) {
        $newCollection = $observer->getEvent()
            ->getCollection();
        $newCollection->addAttributeToFilter('entity_id', ['nin' => [1,2,13]]);
        return $this;
    }

but nothing happens.

I've also tried the event catalog_product_collection_load_after but the case is same.

How can i update this collection via observer?

edit: Using addFieldToFilter or addIdFilter results in "main.CRITICAL: illegal state" in log file system.log and no products shows up but overall page loads fine.

7
  • Please share ur observer code?
    – Amit Bera
    Commented May 6, 2016 at 7:07
  • I've as mentioned above. for now i'm manually giving ids to the filter. what more code should i share?
    – R T
    Commented May 6, 2016 at 7:10
  • share full code of observer class
    – Amit Bera
    Commented May 6, 2016 at 7:10
  • what is $newCollection ? it does have any collection
    – Amit Bera
    Commented May 6, 2016 at 7:12
  • sorry it was a typo. i edited the question.
    – R T
    Commented May 6, 2016 at 7:13

2 Answers 2

2

I was able to achieve the desired result like this:

$observer->getEvent()
    ->getCollection()
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('entity_id', ['nin' => [1,13,5]]);

and used the event catalog_block_product_list_collection

2
0

For this case,you can use addIdFilter(arrayofproductId,true). instead of addAttributeToFilter() at this case.

So in this case, use addIdFilter([1,2,13],true).instead of addAttributeToFilter('entity_id', ['nin' => [1,2,13]]);

That second parameter of addIdFilter() function put the condition nin

See at product collection class Magento\Catalog\Model\ResourceModel\Product\Collection;

 public function addIdFilter($productId, $exclude = false)
    {
        if (empty($productId)) {
            $this->_setIsLoaded(true);
            return $this;
        }
        if (is_array($productId)) {
            if (!empty($productId)) {
              //$exclude flag use for filter collection at nin condition
                if ($exclude) {
                    $condition = ['nin' => $productId];
                } else {
                    $condition = ['in' => $productId];
                }
            } else {
                $condition = '';
            }
        } else {
            if ($exclude) {
                $condition = ['neq' => $productId];
            } else {
                $condition = $productId;
            }
        }
        $this->addFieldToFilter('entity_id', $condition);
        return $this;
    }
3
  • yes I've tried that and also mentioned it in edit part that using the method generates an error in log "illegal state" and all products disappear.
    – R T
    Commented May 6, 2016 at 7:19
  • Please clear the cache and try
    – Amit Bera
    Commented May 6, 2016 at 7:24
  • did that, nothing happened. same issue
    – R T
    Commented May 6, 2016 at 7:28

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.