2011-12-09 136 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()