0

In the cart screen, I've included a block that is calling a controller, where I modify the items of the cart, and after this, I want to reload the cart screen again, so I can see the changes in the quantities and prices.

But the redirect method is doing nothing.

This is my controller:

class MyModule_IndexController extends Mage_Core_Controller_Front_Action
{
    public function indexAction() {
        if ($this->getRequest()->isPost()) {
            ...
            try {
                $cart = Mage::getModel('checkout/cart')->getQuote();
                foreach ($cart->getAllItems() as $item) {
                    $item->setQty(2);
                }
                $cart->save();
                $this->_getSession()->setCartWasUpdated(true);
                Mage::dispatchEvent('checkout_cart_update_item_complete',
                    array('item' => $item, 'request' => $this->getRequest(), 'response' => $this->getResponse())
                );
            } catch (Mage_Core_Exception $e) {
                $this->_getSession()->addError(Mage::helper('core')->escapeHtml($e->getMessage()));
            } catch (Exception $e) {
                $this->_getSession()->addException($e, $this->__('Cannot update shopping cart.'));
                Mage::logException($e);
            }
            $this->_redirect('checkout/cart');
        }
    }

    protected function _getSession()
    {
        return Mage::getSingleton('customer/session');
    }
}

The line:

$this->_redirect('checkout/cart');

doesn't seem to be working...

  • use return $this->_redirect('checkout/cart'); – Anil Suthar Jul 22 '16 at 10:57
  • the same... I've also added return $this; at the end, but same thing... no redirecting – nano Jul 22 '16 at 11:02
  • Can you debug that it executes _redirect function ? Also your controller name MyModule_IndexController seems wrong, it should be MyCompany_MyModule_IndexController – Anil Suthar Jul 22 '16 at 11:16
  • The module name was changed on purpose, don't worry. That controller is called from an Ajax method, sgo I'm oing to return something like this echo json_encode(array('reload' => $reload_url, ...)); and handle the redirection through JS code. – nano Jul 22 '16 at 11:21
  • 1
    ahh!!. If you are calling this method by AJAX (not mentioned in question), then echo return url and use window.location.href to redirect by javascript – Anil Suthar Jul 22 '16 at 11:25
1

First of all - you'll need to put redirect code right before last closing "}". Because if your request is not POST type - you'll have no redirect at all.

And instead of

$this->_redirect('checkout/cart');

You can try to use these constructions:

$this->_redirectReferer();

or

$redirectUrl = $this->getUrl('checkout/cart');
$this->_redirectUrl($redirectUrl);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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