I am trying to use sales_quote_item_collection_products_after_load
to remove items from a quote depending on a custom condition.
public function filterQuote(Varien_Event_Observer $observer)
{
/** @var Mage_Catalog_Model_Resource_Product_Collection $collection */
$collection = $observer->getProductCollection();
/** @var Mage_Sales_Model_Quote $quote */
$quote = Mage::helper('checkout/cart')->getQuote();
$items = $quote->getAllVisibleItems();
foreach ($collection as $product) {
$customQty = $product->getCustomQty();
foreach ($items as $item) {
// We need to get the id of the simple product and not the parent.
$simpleProduct = Mage::getModel('catalog/product')->loadByAttribute('sku', $item->getSku());
$qty = (int) $item->getQty();
// Check so the quote item matches the product and
// compare the custom attribute.
if ($simpleProduct->getId() == $product->getId() &&
$qty >= $customQty) {
// Remove the item from the collection.
$collection->removeItemByKey($product->getId());
break;
}
}
}
}
I thought the above should work, but perhaps I should use some other approach. Right now all items still seems to be left in the cart even if I use $collection->removeItemByKey($product->getId());
for all items.