简体   繁体   中英

Magento - update cart item by item ID

How to update cart item using item ID ? I have searched a lot but could not success, here is my codes -

require_once '../app/Mage.php';
Mage::app('default');

$qty = $_REQUEST['quantity'];
$item_id = (int) $_REQUEST['item_id'];
$cart = Mage::getSingleton('checkout/cart');
$quoteItem = $cart->getQuote()->getItemById($item_id);

/*$quoteItem = Mage::getModel('sales/quote_item')->getCollection()
   ->addFieldToFilter('item_id', array('in' => array($item_id)));*/
print_r($quoteItem);
if (!$quoteItem) {
    Mage::throwException('Quote item is not found.');
}

if ($qty == 0) {
    $cart->removeItem($id);
} else {
    $quoteItem->setQty($qty)->save();
}
$cart->save();

I am implementing it in API , however same code is working fine in website.Please help!!!

I used the below code to update cart items and I am using observer to do that. can you please try this.

    $quote = $observer->getEvent()->getQuote();
    var_dump($quote->getId());

    $quote = Mage::getSingleton('checkout/session')->getQuote();
    //var_dump($quote->getId());
    $cartItems = $quote->getAllItems();
    //$storeId = Mage::app()->getStore()->getId();
    foreach ($cartItems as $item) {
        //echo $item->getSku();
        $item->set__($value); // desired quote field from database
        $item->save();
    }
    $quote->save();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM