0

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.

0

you can try:

Add below the code inside your condition for remove item from quote.

Mage::helper('checkout/cart')->getCart()->removeItem($item->getId())->save();
  • But the event I'm using above is triggered after the quote loads its collection but as I understand it before it is rendered and saved - so the above should work? – Cyclonecode Jun 27 '18 at 11:24
  • you can try once @Cyclonecode – Nikunj Vadariya Jun 27 '18 at 11:31

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.