2011-05-19 144 views
1

我從包含一些產品ID的extrenal網站收到請求。 在我的模塊中,我嘗試加載產品並將它們添加到購物車。我試過這個代碼:Magento - 使用循環將產品添加到購物車

public function indexAction() { 
    $ids = explode(',', $this->getRequest()->getParam('products')); 

    Mage::log('ADDING PRODUCTS'); 

    $cart = Mage::getModel('checkout/cart'); 
    $cart->init(); 

    $pModel = Mage::getSingleton('catalog/product'); 

    //$cart->addProductsByIDs($ids); 

    foreach ($ids as $id) { 

     Mage::log('Loading: ' . $id); 

     $product = $pModel->load($id); 

     Mage::log('Loaded: ' . $product->getId()); 

     try { 
      $cart->addProduct($product, array('qty' => '1')); 
     } catch (Exception $e) { 
      Mage::log($e); 
      continue; 
     } 
    } 

    $cart->save(); 

    if ($this->getRequest()->isXmlHttpRequest()) { 
     exit('1'); 
    } 

    $this->_redirect('checkout/cart'); 
} 

我可以在system.log中看到它正確地加載了產品。但重新定向後,我的購物車中有第二個產品兩次。第一個失蹤。使用$ cart-> addProductsByIDs($ ids)效果很好,但後來我不能再影響產品的數量了。

有人知道我做錯了什麼,給我一個提示嗎?

THX

回答

1

只是一些自定義添加到我有車代碼進行覈對,並確認是否正常工作,而我唯一的區別是:

$cart->addProduct($product, array(
    'qty'  => 1, 
    'product' => $product->getId(), 
    'uenc' => Mage::helper('core')->urlEncode(Mage::helper('core/url')->getCurrentUrl()) 
)); 

// Also make sure we know that the cart was updated 
Mage::getSingleton('checkout/session')->setCartWasUpdated(true); 

這就是說,你的錯誤不會讓你聽起來像這個區域真的有問題。我無法想象會是這樣,但是每次將產品添加到購物車時,購物車型號是否可能需要爲save()d?這是值得的平底船。

3

我有同樣的問題,我通過加載每一個循環內的產品型號固定它:

public function AddMultipleItemsAction() { 
    $products = explode(',', $this->getRequest()->getParam('products')); 
    $quantities = explode(',', $this->getRequest()->getParam('quantities')); 
    $numProducts = count($products); 
    $cart = $this->_getCart(); 
    for($i=0;$i<$numProducts;$i++) { 
     $product_id = $products[$i]; 
     $quantity = $quantities[$i]; 
     if ($product_id == '') continue; 
     if(!is_numeric($quantity) || $quantity <= 0) continue; 
     $pModel = Mage::getModel('catalog/product')->load($product_id); 
     if($pModel->getTypeId() == Mage_Catalog_Model_Product_Type::TYPE_SIMPLE) { 
      try { 
       $eventArgs = array(
        'product' => $pModel, 
        'qty' => $quantity, 
        'additional_ids' => array(), 
        'request' => $this->getRequest(), 
        'response' => $this->getResponse(), 
       ); 
       Mage::dispatchEvent('checkout_cart_before_add', $eventArgs); 
       $cart->addProduct($pModel, array('product'=>$product_id,'qty' => $quantity)); 
       Mage::dispatchEvent('checkout_cart_after_add', $eventArgs); 
       Mage::dispatchEvent('checkout_cart_add_product', array('product'=>$pModel)); 
       $message = $this->__('%s was successfully added to your shopping cart.', $pModel->getName()); 
       Mage::getSingleton('checkout/session')->addSuccess($message); 
      } catch (Mage_Core_Exception $e) { 
       if (Mage::getSingleton('checkout/session')->getUseNotice(true)) { 
        Mage::getSingleton('checkout/session')->addNotice($pModel->getName() . ': ' . $e->getMessage()); 
       } 
       else { 
        Mage::getSingleton('checkout/session')->addError($pModel->getName() . ': ' . $e->getMessage()); 
       } 
      } catch (Exception $e) { 
       Mage::getSingleton('checkout/session')->addException($e, $this->__('Can not add item to shopping cart')); 
      } 
     } 
    } 
    $cart->save(); 
    $this->_getSession()->setCartWasUpdated(true); 
    $this->_redirect('checkout/cart'); 
} 

然後,我有一個「添加所有項目到購物車」按鈕,執行下面的JavaScript代碼:

<script type="text/javascript"> 
function addAllItemsToCart() { 
    productsArr = new Array(); 
    quantitiesArr = new Array(); 
    $$('#product-listing-table .qty').each(
     function (input, index) { 
      productsArr[index] = encodeURIComponent(input.readAttribute('product_id')); 
      quantitiesArr[index] = encodeURIComponent(input.value); 
     } 
    ); 
    var url = '/MyModule/Cart/AddMultipleItems/products/'+productsArr.join(',')+'/quantities/'+quantitiesArr.join(',')+'/'; 
    setLocation(url); 
} 


對於這個能夠工作,我把一個額外的product_id屬性的數量文本框,如:

<input type="text" size="2" product_id="<?php echo $_product->getId();?>" name="productqty_<?php echo $_product->getId();?>" class="qty" /> 

和產品的整個列表是一個div裏面有ID 產品上市表

+0

+1。我有同樣的問題,並在每個循環內加載產品模型也適用於我,謝謝!我只需要弄清楚爲什麼每個項目都要增加5次而不是一次,不知道你是否越過了同樣的問題? – Ozzy 2012-09-21 15:49:32

相關問題