Magento 1.9 – Remove Item from Quote Item

magento-1.9quotequoteitemsales-quote

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.

Best Answer

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;
}