1

I'm trying to modify items in a collection on page load (catalog) and using the event: catalog_product_collection_load_after

and the code for the event is :

$observer->getCollection()
            ->addFieldToFilter('entity_id', array('nin' => array_keys($_excludeProducts)))
            ->clear()
            ->load();

this does exclude product(s) but the count is wrong. like there're 5 products in the collection and on the grid as well but the count is 6 items. How can i correct this?

7
  • 1
    the load event is not fired for getSize() and I'm not too sure about count() so it makes sense. A solution I don't have for you at this moment – Sander Mangel Sep 30 '15 at 9:49
  • 2
    Wait, you load the collection again in load_after? Even if Magento is smart enough to not go into endless recursion, why don't you use load_before to add the field to filter? – Fabian Schmengler Sep 30 '15 at 10:40
  • @fschmengler it goes into recursion when using load_before – R T Sep 30 '15 at 10:52
  • Only if you call load() in the observer, which you should not and need not then – Fabian Schmengler Sep 30 '15 at 10:52
  • (these're just desperate attempts) removing load() and using load_before in event didn't help either. – R T Sep 30 '15 at 10:56
0

I guess that you can use count() function,But not sure 100%.

Basically count() is give result after load() executed that reason it will give you proper result

 public function count()
    {
        $this->load();
        return count($this->_items);
    }

See at Varien_Data_Collection class.

count() may be give you proper result whenever you remove clear() from collection.As this make collection load false.setIsLoaded(false) and remove all element from Collection

See at function:

 public function clear()
    {
        $this->_setIsLoaded(false);
        $this->_items = array();
        return $this;
    }

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.