
MAGENTO 1.X – REMOVE ITEMS FROM A COLLECTION WHEN ITERATING
Sometimes you may need to remove items from a collection (eg: products) after it was loaded, or when you are iterating it.
The following example shows you how can you remove non enabled products items from a product collection. The respective collection have to be inherited from Varien_Data_Collection.
1 2 3 4 5 6 7 8 9 |
$products = Mage::getModel('catalog/product')->getCollection() ->addAttributeToSelect("*") ->load(); foreach ($products as $_product) { if($_product->getStatus() != Mage_Catalog_Model_Product_Status::STATUS_ENABLED){ $products->removeItemByKey($_product->getId()); } } |