[英]Magento : How to Get product from cart and add it again
我想從購物車中以陣列的形式獲取所有產品。 我想從購物車中刪除所有產品。 之后,我要添加相同數量和選項的相同產品,這些數量和選項由客戶根據某些條件再次在購物車中選擇。 我做了以下。
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]);
嘗試這個 :
$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);
請在您的代碼中嘗試以下代碼
$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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.