2015-10-13 68 views
2

我想從購物車中的所有產品獲取數組。我想從購物車中刪除所有產品。在此之後,我想根據一些條件再次在購物車中添加相同數量和選項的產品,這些數量和選項由客戶再次選擇。我做了以下。Magento:如何從購物車獲取產品並再次添加它

1)將所有產品保存在購物車中的數組中。使用下面的代碼。

$quote = $this->getQuote(); 

$i=0; 

foreach ($quote->getAllItems() as $item) { 
     $bufferItems[$i] = $item; 
     $i++; 
} 

2)使用以下代碼從購物車中刪除所有產品。

foreach ($quote->getAllItems() as $item) { 
     $quote->getItemsCollection()->removeItemByKey($item->getId()); 
} 

直到這一切工作正常。

現在我不知道如何將產品添加到購物車...任何人都可以幫助我。

我做了以下不工作。

1) $quote->addProduct($bufferItems[1]); 
2) $quote->addItem($bufferItems[1]); 

回答

1

試試這個:

$cartItems = Mage::getModel("checkout/cart")->getItems(); 

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

$cart->init(); 

foreach($cartItems as $item) { 
    $_product = Mage::getModel('catalog/product')->load($item->getProductId()); 
    $cart->addProduct($_product, array('qty' => $item->getQty())); 
} 

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

哇,這是工作的罰款謝謝:)。但只有一個問題:(所有訂單的訂單總額相同,所有訂單產品的產品價格均爲「0.00」),您能幫我解決這個問題嗎? –

1

請嘗試以下代碼的代碼中

$params = array(
 
    'product' => 164, 
 
    'qty' => 2, 
 
); 
 
    
 
$cart = Mage::getSingleton('checkout/cart'); 
 
    
 
$product = new Mage_Catalog_Model_Product(); 
 
$product->load(164); 
 
    
 
$cart->addProduct($product, $params); 
 
$cart->save(); 
 
    
 
Mage::getSingleton('checkout/session')->setCartWasUpdated(true); 
 
    
 
$message = $this->__('Custom message: %s was successfully added to your shopping cart.', $product->getName()); 
 
Mage::getSingleton('checkout/session')->addSuccess($message);

相關問題