0

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.

3 Answers 3

1

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;
}
9
  • I have tried a different function where save worked for collection ,To update qty in sales item
    – Melvin
    Commented Feb 22, 2017 at 13:25
  • @Melvin try above code and let me know Commented Feb 22, 2017 at 13:31
  • returning the same error
    – Melvin
    Commented Feb 22, 2017 at 13:34
  • @Melvin check updated code Commented Feb 22, 2017 at 13:45
  • This is working now however the sales quote qty is not getting deducted.IShould I need to add a different logic or this code will take care ?
    – Melvin
    Commented Feb 22, 2017 at 14:01
0

Use below code :

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.