I've created a custom controller to add multiple items from a grid view to the cart all at one time.
Works just fine - the problem is the success and error messages are not being displayed on the view cart page.
Here is the controller:
<?php
class BigBlockStudios_UpdateCart_ManageController extends Mage_Core_Controller_Front_Action {
// add multiple products to the cart
public function multipleProdAddAction() {
$redirect = isset($_POST['redirect']) ? $_POST['redirect'] : 'checkout/cart';
$success = '';
$errors = '';
$cart = Mage::helper('checkout/cart')->getCart();
$items = $_POST['data'];
foreach($items as $key => $item){
$sku = $item['sku'];
$qty = $item['qty'];
$id = Mage::getModel('catalog/product')->getIdBySku($sku);
if(!$id || $qty <= 0) {
unset($items[$key]);
}else{
try {
$params = array('qty' => $qty);
$id = Mage::getModel('catalog/product')->getIdBySku($sku);
$product = Mage::getModel('catalog/product')->load($id);
$cart->addProduct($product, $params);
$success .= $product->getName(). " is successfully added into cart <br />";
}catch(Exception $e) {
$errors .= $e->getMessage() . '<br />';
}
}
}
$cart->save();
if(strlen($success) > 1) {
Mage::getSingleton('core/session')->addSuccess(Mage::helper('checkout')->__($success));
}
if(strlen($errors) > 1){
Mage::getSingleton('core/session')->addError(Mage::helper('checkout')->__($errors));
}
$this->_redirect($redirect);
} // multipleProdAddAction()
} // controller
?>
The view cart page is displaying messages, if I add an item from a default product page, I get the success message.
What is the correct way of doing this?