Note that there are some explanatory texts on larger screens.

plurals
  1. POMagento: Get all updated cart items data in AJAX response
    primarykey
    data
    text
    <p>My Cart is looking like this with popup window:</p> <p><img src="https://i.stack.imgur.com/yq0Er.png" alt="enter image description here"></p> <p>I am updating multiple configurable items in cart with custom options at a time by AJAX Call.</p> <p>But I am not able to get all items data back to AJAX response.</p> <p>I am getting just 1st itemPrice and rowTotal. For remaining items itemPrice and rowTotal are set to 0.</p> <p><strong>Code:</strong></p> <pre><code>public function updateItemOptionsAction() { $cartData = $this-&gt;getRequest()-&gt;getParam('cart'); Mage::log($cartData); if (is_array($cartData)) { $result = array(); $result['data'] = array(); foreach ($cartData as $index =&gt; $data) { $cart = $this-&gt;_getCart(); $params = $data; $id = (int)$data['id']; if (!isset($params['options'])) { $params['options'] = array(); } $result['data'][$index] = array(); $oldQty = null; $kitType = $params['efk_kType']; $params['super_attribute'] = array($data['sAttr']=&gt;$kitType); unset($params['sAttr']); $stock = null; try { if (isset($params['qty'])) { $product = Mage::getModel("catalog/product")-&gt;load($params['product']); $childProducts = Mage::getModel('catalog/product_type_configurable')-&gt;getUsedProducts(null, $product); foreach($childProducts as $cProd){ if($cProd-&gt;getKitType() == $kitType){ $stock = intval(Mage::getModel('cataloginventory/stock_item')-&gt;loadByProduct($cProd)-&gt;getQty()); } } if(intval($params['qty']) &gt; $stock){ $oldQty = intval($params['qty']); $params['qty'] = $stock; $result['data'][$index]['revised'] = true; } $filter = new Zend_Filter_LocalizedToNormalized( array('locale' =&gt; Mage::app()-&gt;getLocale()-&gt;getLocaleCode()) ); $params['qty'] = $filter-&gt;filter($params['qty']); } $quoteItem = Mage::getSingleton('checkout/cart')-&gt;getQuote()-&gt;getItemById($id); if (!$quoteItem) { Mage::throwException($this-&gt;__('Quote item is not found.')); } //Its going to infinity loop duwe to Varien Object need to check later //$item = $cart-&gt;updateItem($id, new Varien_Object($params)); $item = Mage::getSingleton('checkout/cart')-&gt;updateItem($id, $params); if (is_string($item)) { Mage::throwException($item); } if ($item-&gt;getHasError()) { Mage::throwException($item-&gt;getMessage()); } Mage::log('hi2'); $related = $params['related_product']; if (!empty($related)) { Mage::getSingleton('checkout/cart')-&gt;addProductsByIds(explode(',', $related)); } Mage::getSingleton('checkout/cart')-&gt;save(); Mage::getSingleton('checkout/session')-&gt;setCartWasUpdated(true); Mage::dispatchEvent('checkout_cart_update_item_complete', array('item' =&gt; $item, 'request' =&gt; $this-&gt;getRequest(), 'response' =&gt; $this-&gt;getResponse()) ); $cart-&gt;getQuote()-&gt;setTotalsCollectedFlag(false); Mage::getSingleton('checkout/cart')-&gt;init(); if (!Mage::getSingleton('checkout/session')-&gt;getNoCartRedirect(true)) { if (!Mage::getSingleton('checkout/cart')-&gt;getQuote()-&gt;getHasError()) { Mage::log('hi4'); $result['success'] = true; if($oldQty &gt; $item-&gt;getQty()){ $message = $this-&gt;__('%s has been revised due to stock limitations. You may proceed with the order for the revised quantity.', Mage::helper('core')-&gt;escapeHtml($item-&gt;getProduct()-&gt;getName())); }else{ $message = $this-&gt;__('%s was updated in your shopping cart.', Mage::helper('core')-&gt;escapeHtml($item-&gt;getProduct()-&gt;getName())); } $result['data'][$index]['message'] = $message; $result['data'][$index]['itemId'] = $item-&gt;getId(); $result['data'][$index]['itemPrice'] = Mage::helper('checkout')-&gt;formatPrice($item-&gt;getCalculationPrice()); $result['data'][$index]['qty'] = $item-&gt;getQty(); $result['data'][$index]['rowTotal'] = Mage::helper('checkout')-&gt;formatPrice($item-&gt;getRowTotal()); } } } catch (Mage_Core_Exception $e) { $result['success'] = false; $result['data'][$index]['success'] = 'qty'; $result['data'][$index]['message'] = $e-&gt;getMessage(); } catch (Exception $e) { $result['success'] = false; $result['data'][$index]['message'] = $e-&gt;getMessage(); $result['data'][$index]['secondMessage'] = $this-&gt;__('Cannot update the item.'); } } $result['data']['grandTotal'] = Mage::helper('checkout')-&gt;formatPrice(Mage::getSingleton('checkout/cart')-&gt;getQuote()-&gt;getGrandTotal()); $result['data']['totalItems'] = Mage::getSingleton('checkout/cart')-&gt;getSummaryQty(); $totals = Mage::getSingleton('checkout/cart')-&gt;getQuote()-&gt;getTotals(); $result['data']['subTotal'] = Mage::helper('checkout')-&gt;formatPrice($totals['subtotal']-&gt;getValue()); if(isset($totals['discount']) &amp;&amp; $totals['discount']-&gt;getValue()){ $result['data']['discount'] = Mage::helper('checkout')-&gt;formatPrice($totals['discount']-&gt;getValue()); }else{ $result['data']['discount'] = Mage::helper('checkout')-&gt;formatPrice(0); } } $this-&gt;getResponse()-&gt;setBody(Mage::helper('core')-&gt;jsonEncode($result)); } </code></pre> <p><strong>AJAX Response I'm getting</strong></p> <pre><code>{ "data": { "1187": { "success": true, "message": "THREE PHASE SOLID STATE RELAY WITH ZVS was updated in your shopping cart.", "itemId": "1191", "itemPrice": "&lt;span class=\"price\"&gt;20b9 3,799&lt;/span&gt;", "qty": 1, "rowTotal": "&lt;span class=\"price\"&gt;20b9 3,799&lt;/span&gt;", "forLoop": "yes" }, "1189": { "success": true, "message": "AUTO INTENSITY CONTROL OF STREET LIGHTS was updated in your shopping cart.", "itemId": "1193", "itemPrice": "&lt;span class=\"price\"&gt;20b9 0&lt;/span&gt;", "qty": 1, "rowTotal": "&lt;span class=\"price\"&gt;20b9 0&lt;/span&gt;", "forLoop": "yes" }, "grandTotal": "&lt;span class=\"price\"&gt;20b9 8,798&lt;/span&gt;", "totalItems": 2, "subTotal": "&lt;span class=\"price\"&gt;20b9 8,798&lt;/span&gt;", "discount": "&lt;span class=\"price\"&gt;20b9 0&lt;/span&gt;" } } </code></pre> <p>I am getting itemPrice and rowTotal for 2nd item as 0. Every time I am getting correct values for 1st item only. If i am update 5 items at a time (say for example) then i am getting correct values for 1st item and for remianing items i am getting 0's.</p> <p>If i refresh cart once i get AJAX response, then it is showing itemPrice and rowTotal updated values correctly for all items.</p> <p><strong>Note: 20b9 is HEX Code for Indian Rupee symbol</strong></p> <p>Please point out where i am wrong.</p> <p>Thanks in advance.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload