2013-12-19 32 views
3

我的车是看起来像这样用弹出窗口:Magento的:获取AJAX响应的所有更新购物车的物品数据

enter image description here

我在通过Ajax调用一次更新在车用自定义选项多个配置项。

但我无法将所有项目数据都返回给AJAX响应。

我刚刚得到第1条itemPrice和rowTotal。对于剩余的项目ITEMPRICE和rowTotal设置为0

代码:

public function updateItemOptionsAction() 
{ 
    $cartData = $this->getRequest()->getParam('cart'); 
    Mage::log($cartData); 
    if (is_array($cartData)) { 
     $result = array(); 
     $result['data'] = array(); 
     foreach ($cartData as $index => $data) { 
      $cart = $this->_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']=>$kitType); 
      unset($params['sAttr']); 
      $stock = null; 
      try { 
       if (isset($params['qty'])) { 
        $product = Mage::getModel("catalog/product")->load($params['product']); 
        $childProducts = Mage::getModel('catalog/product_type_configurable')->getUsedProducts(null, $product); 
        foreach($childProducts as $cProd){ 
         if($cProd->getKitType() == $kitType){ 
          $stock = intval(Mage::getModel('cataloginventory/stock_item')->loadByProduct($cProd)->getQty()); 
         } 
        } 
        if(intval($params['qty']) > $stock){ 
         $oldQty = intval($params['qty']); 
         $params['qty'] = $stock; 
         $result['data'][$index]['revised'] = true; 
        } 

        $filter = new Zend_Filter_LocalizedToNormalized(
         array('locale' => Mage::app()->getLocale()->getLocaleCode()) 
        ); 
        $params['qty'] = $filter->filter($params['qty']); 
       } 

       $quoteItem = Mage::getSingleton('checkout/cart')->getQuote()->getItemById($id); 
       if (!$quoteItem) { 
        Mage::throwException($this->__('Quote item is not found.')); 
       } 

       //Its going to infinity loop duwe to Varien Object need to check later 
       //$item = $cart->updateItem($id, new Varien_Object($params)); 
       $item = Mage::getSingleton('checkout/cart')->updateItem($id, $params); 

       if (is_string($item)) { 
        Mage::throwException($item); 
       } 
       if ($item->getHasError()) { 
        Mage::throwException($item->getMessage()); 
       } 
       Mage::log('hi2'); 

       $related = $params['related_product']; 
       if (!empty($related)) { 
        Mage::getSingleton('checkout/cart')->addProductsByIds(explode(',', $related)); 
       } 

       Mage::getSingleton('checkout/cart')->save(); 
       Mage::getSingleton('checkout/session')->setCartWasUpdated(true); 

       Mage::dispatchEvent('checkout_cart_update_item_complete', 
        array('item' => $item, 'request' => $this->getRequest(), 'response' => $this->getResponse()) 
       ); 
       $cart->getQuote()->setTotalsCollectedFlag(false); 
       Mage::getSingleton('checkout/cart')->init(); 

       if (!Mage::getSingleton('checkout/session')->getNoCartRedirect(true)) { 
        if (!Mage::getSingleton('checkout/cart')->getQuote()->getHasError()) { 
         Mage::log('hi4'); 
         $result['success'] = true; 
         if($oldQty > $item->getQty()){ 
          $message = $this->__('%s has been revised due to stock limitations. You may proceed with the order for the revised quantity.', Mage::helper('core')->escapeHtml($item->getProduct()->getName())); 
         }else{ 
          $message = $this->__('%s was updated in your shopping cart.', Mage::helper('core')->escapeHtml($item->getProduct()->getName())); 
         } 
         $result['data'][$index]['message'] = $message; 
         $result['data'][$index]['itemId'] = $item->getId(); 
         $result['data'][$index]['itemPrice'] = Mage::helper('checkout')->formatPrice($item->getCalculationPrice()); 
         $result['data'][$index]['qty'] = $item->getQty(); 
         $result['data'][$index]['rowTotal'] = Mage::helper('checkout')->formatPrice($item->getRowTotal()); 
        } 
       } 

      } catch (Mage_Core_Exception $e) { 
       $result['success'] = false; 
       $result['data'][$index]['success'] = 'qty'; 
       $result['data'][$index]['message'] = $e->getMessage(); 
      } catch (Exception $e) { 
       $result['success'] = false; 
       $result['data'][$index]['message'] = $e->getMessage(); 
       $result['data'][$index]['secondMessage'] = $this->__('Cannot update the item.'); 
      } 
     } 



     $result['data']['grandTotal'] = Mage::helper('checkout')->formatPrice(Mage::getSingleton('checkout/cart')->getQuote()->getGrandTotal()); 
     $result['data']['totalItems'] = Mage::getSingleton('checkout/cart')->getSummaryQty(); 
     $totals = Mage::getSingleton('checkout/cart')->getQuote()->getTotals(); 
     $result['data']['subTotal'] = Mage::helper('checkout')->formatPrice($totals['subtotal']->getValue()); 
     if(isset($totals['discount']) && $totals['discount']->getValue()){ 
      $result['data']['discount'] = Mage::helper('checkout')->formatPrice($totals['discount']->getValue()); 
     }else{ 
      $result['data']['discount'] = Mage::helper('checkout')->formatPrice(0); 
     } 
    } 
    $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result)); 
} 

Ajax响应我得到

{ 
    "data": { 
     "1187": { 
      "success": true, 
      "message": "THREE PHASE SOLID STATE RELAY WITH ZVS was updated in your shopping cart.", 
      "itemId": "1191", 
      "itemPrice": "<span class=\"price\">20b9 3,799</span>", 
      "qty": 1, 
      "rowTotal": "<span class=\"price\">20b9 3,799</span>", 
      "forLoop": "yes" 
     }, 
     "1189": { 
      "success": true, 
      "message": "AUTO INTENSITY CONTROL OF STREET LIGHTS was updated in your shopping cart.", 
      "itemId": "1193", 
      "itemPrice": "<span class=\"price\">20b9 0</span>", 
      "qty": 1, 
      "rowTotal": "<span class=\"price\">20b9 0</span>", 
      "forLoop": "yes" 
     }, 
     "grandTotal": "<span class=\"price\">20b9 8,798</span>", 
     "totalItems": 2, 
     "subTotal": "<span class=\"price\">20b9 8,798</span>", 
     "discount": "<span class=\"price\">20b9 0</span>" 
    } 
} 

我越来越ITEMPRICE和rowTotal的第二项作为0.每次我只获得第一个项目的正确值。如果我每次更新5个物品(比如说),那么我会得到第一个物品的正确值,而对于重新变形的物品我会得到0。

如果我一次刷新购物车,我得到AJAX响应,那么它显示itemPrice和rowTotal正确更新所有项目的值。

注:20b9是十六进制代码印度卢比符号

请指出我哪里错了。

在此先感谢。

+0

是项正确购物车,你只在Ajax响应再次重新填充车弹出得到错误的数据被更新? –

+0

是的,你是对的! – Sivakumar

回答

1

你正在努力工作tooooo ...尝试更新控制器服务器端的项目,因为你是,保存当前报价...然后只是有一个控制器方法,加载当前.phtml并返回将html作为购物车块的json,并用新购物车替换整个购物车html块(div)。

在你的控制方法结束

$this->getResponse()->setBody(json_encode(
array("html" => 
      Mage::app()->getLayout()->createBlock('checkout/[[whatevertag_is_to_cart_div phtml block]]')->toHtml() 
    ) 
); 
+0

sry我没有得到你。如果可能的话用代码展示给我。我是magento的新手。 – Sivakumar

+0

现在有意义吗? – mprototype

相关问题