1

I just create a simple module for cart quantity update (code give bellow). My update link like that I call to ajax. http://example.com/ajaxcart/ajaxcart/quantitichange/item/55/qty/4 It's quantity update fine but I need to subtotal after quantity update. But their is problem. getRowTotal function return which loaded checkout/cart/ example my current cart

itemId 55 Price:5  Qty:2   RowTotal:10

Then I hit http://example.com/ajaxcart/ajaxcart/quantitichange/item/55/qty/4 It's should return

{"s":"Stock updated","subtotal":"20.0000"}

But not that It's return

{"s":"Stock updated","subtotal":"10.0000"}

If i refresh checkout/cart/ page and again hit ajax URL then It's return correct. That means Quantity update. But If I need subtotal then I need to refresh cart page manually.

My code

    public function quantitichangeAction(){
    $jsArray = array();
    $itemId = intval($this->getRequest()->getparam('item'));
    $qty = intval($this->getRequest()->getparam('qty'));
    $cart = Mage::getModel('checkout/cart')->getQuote()->getAllVisibleItems();
    $getItem=false;
    foreach($cart as $item){
        if($item->getItemId()==$itemId){
            $product = Mage::getModel('catalog/product')->load($item->getProductId());
            if ($product->getStockItem()->getIsInStock()) {
                $qtyStock = intval(Mage::getModel('cataloginventory/stock_item')->loadByProduct($product)->getQty());
                if($qtyStock>=$qty){
                    $item->setQty($qty);
                    $item->save();
                    $jsArray['s']='Stock updated';
                    $jsArray['subtotal']=$item->getRowTotal();
                }
                else{
                    $jsArray['e']='Product Stock not abailable';
                }
            }
            else{
                $jsArray['e']='Product Stock not abailable';
            }
        }
    }
    $this->getResponse()->setHeader('Content-type', 'application/json');
    $this->getResponse()->setBody(json_encode($jsArray));
}
3

Try to recollect quote totals by running this after saving quote item:

$item->getQuote()->setTotalsCollectedFlag(false)->collectTotals();
  • 1
    You saved my week and my mental health! Thanks. – Ferrrmolina Mar 12 '18 at 14:54
2

Try with this code :

if($qtyStock >= $qty) {
    $item->setQty($qty);
    $item->save();
    Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
    $jsArray['s']='Stock updated';
    $jsArray['subtotal']=$item->getRowTotal();
}

Call to setCartWasUpdated will force Magento to re-calculate totals for the cart.

  • No same things. If I want to see update then required cart page refresh. Otherwise It's Keep Old one. – Abdus Salam Nov 25 '15 at 8:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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