Magento 1.9 – Fix Cart Quantity Update but Row Total Problem

cartcheckoutmagento-1.9

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

Best Answer

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

$item->getQuote()->setTotalsCollectedFlag(false)->collectTotals();
Related Topic