Question

I would like to update the quote item programmatically,

I am trying below

    $walletAmount = Mage::app()->getRequest()->getPost('walletAmount'); //i AM GETTING THIS VALUE THROUGH AN AJAX REQUEST
    $item = Mage::getModel('sales/quote_item')->load($rowQuoteId); // $rowQuoteId CONTAINS item_id OF THE `sales_flat_quote_item` TABLE.
    $item->setWalletAmount($walletAmount);
    $item->getProduct()->setIsSuperMode(true);
    $item->save();

But above this code gives the error message as below,

Fatal error: Call to a member function getStoreId() on a non-object in \app\code\core\Mage\Sales\Model\Quote\Item\Abstract.php on line

Was it helpful?

Solution

Instead of direct call of sales/quote_item model,use checkout/cart singleton model (Mage::getSingleton('checkout/cart')) and it will resolve your issue.

try below code:

$cart=Mage::getSingleton('checkout/cart');
$item = $cart->getQuote()->getItemById($rowQuoteId);
 $item->getProduct()->setIsSuperMode(true);
$item->setWalletAmount($walletAmount)->save();
$cart->save();

OTHER TIPS

Here's the only place where getStoreId() is mentioned in Mage_Sales_Model_Quote_Item_Abstract:

 $product = $this->_getData('product');
        if ($product === null && $this->getProductId()) {
            $product = Mage::getModel('catalog/product')
                ->setStoreId($this->getQuote()->getStoreId())
                ->load($this->getProductId());
            $this->setProduct($product);
        }

It seems that the issue in $this->getQuote(). Try to check if the quote exists. Anyway, you can set the product manually:

$walletAmount = Mage::app()->getRequest()->getPost('walletAmount'); //i AM GETTING THIS VALUE THROUGH AN AJAX REQUEST
$item = Mage::getModel('sales/quote_item')->load($rowQuoteId); // $rowQuoteId CONTAINS item_id OF THE `sales_flat_quote_item` TABLE.
$item->setWalletAmount($walletAmount);
$product = Mage::getModel('catalog/product')
    ->setStoreId($item->getStoreId())
    ->load($item->getProductId());
$item->setProduct($product);
$item->getProduct()->setIsSuperMode(true);
$item->save();
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top