2011-12-09 142 views
0

我正在magento网站上工作,并且有一个部分可以使用ajax将产品加载到购物车中。这工作正常,但它只允许一次处理一个请求。在发布数据库时,您不能选择其他产品,否则会终止第一个请求。我的问题是如何(或可能)有多个请求同时运行而没有一个取消另一个呢?处理该职位的动作是:有多个ajax请求异步运行

public function addAjaxAction() 
{ 
    $response = array(); 
    $error  = false; 
    $cart  = $this->_getCart(); 
    $params  = $this->getRequest()->getParams(); 
    if (isset($params['simple_sku']) && $params['simple_sku'] != '') 
     $product = Mage::getModel('catalog/product')->load(Mage::getSingleton('catalog/product')->getIdBySku($params['simple_sku'])); 
    else 
     $product = $this->_initProductInStock(); 
    $related = $this->getRequest()->getParam('related_product'); 

    if (!$product) { 
     $error = 'Can not add item to shopping cart'; 
    }else{ 
     try { 
      $cart->addProduct($product, $params);        
      if (!empty($related)) { 
       $cart->addProductsByIds(explode(',', $related)); 
      } 
      $cart->save(); 
      $this->_getSession()->setCartWasUpdated(true); 
      Mage::dispatchEvent('checkout_cart_add_product_complete', array('product'=>$product, 'request'=>$this->getRequest())); 
      $response['status'] = 'success'; 
      $response['cart'] = $this->getLayout()->createBlock('checkout/cart_minicart') 
       ->setTemplate('page/html/modals/added_to_bag.phtml') 
       ->setChild(
        'breads'     ,$this->getLayout()->createBlock('modcheckout/cart_breadcrumbs')->setTemplate('page/html/modals/mini_crumb.phtml') 
       ) 
       ->toHtml(); 
       $response['cartStatus'] = $this->getLayout()->createBlock('core/template')->setTemplate('checkout/cart/status.phtml')->toHtml(); 
       $response['checkoutNow'] = $this->getLayout()->createBlock('core/template')->setTemplate('checkout/cart/checkoutnow.phtml')->toHtml(); 
     } 
     catch (Exception $e) { 
      $error = 'Can not add item to shopping cart'; 
     } 
    } 
    if($error){ 
     $response['status'] = 'error'; 
     $response['message'] = $error; /*= $this->getLayout()->createBlock('modcore/messages') 
      ->addMessage(new Mage_Core_Model_Message_Error($error)) 
      ->getGroupedHtml();*/ 
    } 
    echo Zend_Json::encode($response); 
} 

并处理该响应的JS:

function addQuickviewAjax() { 
    if(productForm.validate()){ 
     if(Product<?php echo $_product->getId() ?>.validate()) {       
      var params = 'product='+Product<?php echo $_product->getId() ?>.productId+'&qty='+Product<?php echo $_product->getId() ?>.currentQty+'&super_attribute[76]='+Product<?php echo $_product->getId() ?>.currentColor+'&super_attribute[491]='+Product<?php echo $_product->getId() ?>.currentSize; 
      new Ajax.Request('/modcheckout/cart/addAjax', { 
      parameters: params, 
       onFailure: reportError, 
       onSuccess: function(transport) { 
        var json = transport.responseText.evalJSON(); 
        if(json.status == 'error'){ 
         alert (json.message);  
        } else if(json.status == 'success') {      
         alert ('success'); 
        } 
       } 
      }); 
     } else { 
      return false; 
     } 
    } 
    return false; 
} 

谁能赐教一下就所有这一切,并告诉我如何/如果我能有多重使用该操作的POST?

+0

感谢您的链接,但只是建议使用.live(),一个不赞成使用的方法,我没有这个项目的jquery豪华..我真的不明白这是如何涉及到我的问题.. 。 – Zac

回答

0

浏览器倾向于限制同时发生的请求数 - 因此不要发送多个请求,请将单个XHR发送到多产品添加操作。想更好的想法看看Mage_Checkout_CartController::addgroupAction()