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?

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

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"? Sep 26, 2015 at 17:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.