3

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?

asked Sep 26, 2015 at 17:08

1 Answer 1

5

Right now you have set the message in core/session. Have to set message in checkout/session to get it in checkout page.

Mage::getSingleton('checkout/session')->addError(Mage::helper('checkout')->__($errors));

Magento has set the message in checkout session while adding a product to cart.

Refer this files

answered Sep 26, 2015 at 17:42
1
  • ok- perfect - thanks... so hmmm. you can see that I have a variable on where to redirect the page to. Say I redirected it somewhere else [account/whatever]. is there a global/session? will something like that display errors "anywhere"? Commented Sep 26, 2015 at 17:49

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.