Magento:在AJAX响应中获取所有更新的购物车项目数据
我的购物车与弹出窗口类似: 我通过AJAX调用一次更新购物车中具有自定义选项的多个可配置项目 但我无法将所有项目数据返回到AJAX响应 我得到的只是第一个项目的价格和rowTotal。对于其余项目,itemPrice和rowTotal设置为0 代码:Magento:在AJAX响应中获取所有更新的购物车项目数据,ajax,magento,magento-1.7,cart,Ajax,Magento,Magento 1.7,Cart,我的购物车与弹出窗口类似: 我通过AJAX调用一次更新购物车中具有自定义选项的多个可配置项目 但我无法将所有项目数据返回到AJAX响应 我得到的只是第一个项目的价格和rowTotal。对于其余项目,itemPrice和rowTotal设置为0 代码: public function updateItemOptionsAction() { $cartData = $this->getRequest()->getParam('cart'); Mage::log($car
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>"
}
}
{
“数据”:{
"1187": {
“成功”:没错,
“消息”:“带ZVS的三相固态继电器已在您的购物车中更新。”,
“itemId”:“1191”,
“项目价格”:“20b9 3799”,
“数量”:1,
“rowTotal”:“20b9 3799”,
“forLoop”:“是”
},
"1189": {
“成功”:没错,
“消息”:“您的购物车中的路灯自动亮度控制已更新。”,
“itemId”:“1193”,
“项目价格”:“20b9 0”,
“数量”:1,
“rowTotal”:“20b9 0”,
“forLoop”:“是”
},
“总计”:“20b9 8798”,
“总计项目”:2,
“小计”:“20b9 8798”,
“折扣”:“20b9 0”
}
}
我将第二个项目的itemPrice和rowTotal设置为0。每次我只得到第一项的正确值。如果我一次更新5个项目(例如),那么第一个项目的值是正确的,而重新定义项目的值是0
如果我在得到AJAX响应后刷新购物车,那么它将正确显示所有项目的itemPrice和rowTotal更新值
注:20b9是印度卢比符号的十六进制代码
请指出我错在哪里
提前谢谢。你工作太努力了。。。尝试更新控制器服务器端的项目,保存当前报价。。。然后有一个控制器方法,加载当前的.phtml并将html作为cart块的json返回,然后用新的块替换整个cart html块(div) 在控制器方法的末尾
$this->getResponse()->setBody( json_encode(
array("html" =>
Mage::app()->getLayout()->createBlock('checkout/[[whatevertag_is_to_cart_div phtml block]]')->toHtml()
)
);
我没听懂你的话。如果可能,请用代码向我解释。我是magento的新手。现在有意义了吗?购物车中的项目更新是否正确,而您在ajax响应中只得到错误的数据,以便再次重新填充购物车弹出窗口?