Disable Redirect After Adding Product to Basket in Magento

carthomeredirect

When I add something from the category to the basket/cart it redirects me to the basket/cart.

After searching I found that I could turn this functionality of in the admin system via

Configuration > Sales | Checkout > After Adding a Product Redirect to Shopping Cart

It now redirects to the Home page. I don't know if this a bug in the redirect?

Best Answer

If you look into the cart controller app/code/core/Mage/Checkout/controllers/CartController.php your will find the function _goBack. This is where the return url is decided by Magento.

protected function _goBack()
{
    $returnUrl = $this->getRequest()->getParam('return_url');
    if ($returnUrl) {

        if (!$this->_isUrlInternal($returnUrl)) {
            throw new Mage_Exception('External urls redirect to "' . $returnUrl . '" denied!');
        }

        $this->_getSession()->getMessages(true);
        $this->getResponse()->setRedirect($returnUrl);
    } elseif (!Mage::getStoreConfig('checkout/cart/redirect_to_cart')
        && !$this->getRequest()->getParam('in_cart')
        && $backUrl = $this->_getRefererUrl()
    ) {
        $this->getResponse()->setRedirect($backUrl);
    } else {
        if (($this->getRequest()->getActionName() == 'add') && !$this->getRequest()->getParam('in_cart')) {
            $this->_getSession()->setContinueShoppingUrl($this->_getRefererUrl());
        }
        $this->_redirect('checkout/cart');
    }
    return $this;
}

The section your are looking for is the call to _getRefererUrl this happens when you are not setting the return url as a parameter and not using the default redirect to cart option.

Inside the function _getRefererUrl the referrer url is checked to see if it is an internal url, when it is not internal than the base url is used.

I would suggest that either your referrer url is external or there is something wrong with the check.

Have a look at Mage_Core_Controller_Varien_Action::_isUrlInternal to debug if the url is internal or not.

Problem was that _isUrlInternal was failing because of having the port in the base url