Question

Hi I was trying to remove an item from the sales quote item.

$quoteItemCollection = Mage::getModel('sales/quote_item')->getCollection()->addFieldToFilter('quote_id', $quoteId)->addFieldToFilter('product_id', $product_id)->getFirstItem();;
    $itemId = $quoteItemCollection->getItemId();
    $quoteItemCollection->removeItem($itemId);
    $quoteItemCollection->save();

This is returning me the following error.

{"messages":{"error":[{"code":0,"message":"Invalid method Mage_Sales_Model_Quote_Item::removeItem(Array\n(\n [0] => 1355\n)\n)"}]}}

If this is not the way,is there any other way to do or what made me wrong here.Please help.

Was it helpful?

Solution

try below code because save method doesnot work with collection

$quote = Mage::getModel('sales/quote')->load(quote_id);
// get item id by your logic 
$quote->removeItem($itemId);
$quote->save();

removeItem method code

public function removeItem($itemId)
{
    $item = $this->getItemById($itemId);

    if ($item) {
        $item->setQuote($this);
        /**
         * If we remove item from quote - we can't use multishipping mode
         */
        $this->setIsMultiShipping(false);
        $item->isDeleted(true);
        if ($item->getHasChildren()) {
            foreach ($item->getChildren() as $child) {
                $child->isDeleted(true);
            }
        }

        $parent = $item->getParentItem();
        if ($parent) {
            $parent->isDeleted(true);
        }

        Mage::dispatchEvent('sales_quote_remove_item', array('quote_item' => $item));
    }

    return $this;
}

OTHER TIPS

Use below code :

$cartHelper = Mage::helper('checkout/cart');
$items = $cartHelper->getCart()->getItems();        
foreach ($items as $item) 
{
   $itemId = $item->getItemId();
   $cartHelper->getCart()->removeItem($itemId)->save();
} 

Try the bellow code:

 $quote = Mage::getModel("sales/quote");
 $quote->loadByIdWithoutStore($quoteId);
 $quoteItems=$quote->getAllItems();
 if(count($quoteItems)>0){
     foreach($quoteItems as $oneItem){
         if($oneItem->getProductId()==$product_id){
           $quote->removeItem($oneItem->getId());
           $quote->collectTotals()->save();
         }
     }
 }
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top