I want to get all the product from cart in a array. and i want to remove all the products from cart. after that i want to add the same products with same quantity and options which are selected by customer again in the cart based on some conditions. i did the following.
1) Save all the products in a array which are available in cart. Using following code.
$quote = $this->getQuote();
$i=0;
foreach ($quote->getAllItems() as $item) {
$bufferItems[$i] = $item;
$i++;
}
2) Remove all the products from cart using following code.
foreach ($quote->getAllItems() as $item) {
$quote->getItemsCollection()->removeItemByKey($item->getId());
}
Up to this everything is working fine.
Now i don't know how to add the product back to the cart... can any one help me.
i did the following which is not working.
1) $quote->addProduct($bufferItems[1]);
2) $quote->addItem($bufferItems[1]);
Try this :
$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);
Please try the following code within your code
$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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.