Assuming I want to create a link for the customer to click on that automatically adds some items to cart and redirects them to the cart.
I created a controller that adds two items to cart, problem is, it's only adding the first item to cart, tried switching the order of the items and same problem.
Any ideas?
public function specialsAction(){
$prod_one = Mage::getModel('catalog/product')->setStoreId(Mage::app()->getStore()->getId())
->load($prod_one_id);
$prod_two = Mage::getModel('catalog/product')->setStoreId(Mage::app()->getStore()->getId())
->load($prod_two_id);
$params_one = array(
'product' => $prod_one->getId(),
'qty' => 1
);
$params_two = array(
'product' => $prod_two->getId(),
'qty' => 1
);
$cart = Mage::helper('checkout/cart')->getCart();
// this item gets saved to cart, since its the first item i'm adding
$cart->addProduct($prod_one, $params_one);
$cart->save();
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
// this item does NOT get added (unless I add it first to the cart)
$cart->addProduct($prod_two, $params_two);
$cart->save();
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
$this->_redirect('checkout/cart');
}
Answer
Since the answer below is a bit ambiguous I'm pasting the solution below:
$cart->addProduct($prod_one, $params_one);
// REMOVE these two lines and place them only at the end of all the product additions
//$cart->save();
//Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
$cart->addProduct($prod_two, $params_two);
// HERE is where we save
$cart->save();
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
Best Answer
Try
or
Read more Add Products to cart Programmatically in Magento
See /app/code/core/Mage/Adminhtml/Model/Sales/Order/Create.php