Skip to main content

Redirect to Category Page after “Add to Cart”

Magento uses the default process when product is “Add to Cart” from product detail page: product is added to cart and redirect to the cart page or user can stay on product page. We need user to be directed to the previous category page.

Our site is http://www.grubtogo.com and this is a query about the navigation of our mobile site as our primary site uses Ajax popup for product detail page. On mobile site, user navigates from Category Page to Product Detail Page then "Add to Cart" needs to redirect user to the previous page they were on before the product detail page.

In Magento Admin we have set:

System>Configuration>Sales>Checkout>Shopping Cart>"After Adding a Product Redirect to Shopping Cart": No

Many products are associated with multiple categories so using the category of the product in the redirect is not a practical solution.

app/code/core/Mage/Checkout/controllers/CartController.php responsible for the redirect is this:

/**
* Add product to shopping cart action
*/
    public function addAction()
    {
        $cart   = $this->_getCart();
        $params = $this->getRequest()->getParams();
        try {
            if (isset($params['qty'])) {
                $filter = new Zend_Filter_LocalizedToNormalized(
                    array('locale' => Mage::app()->getLocale()->getLocaleCode())
                );
                $params['qty'] = $filter->filter($params['qty']);
            }

            $product = $this->_initProduct();
            $related = $this->getRequest()->getParam('related_product');

            /**
             * Check product availability
             */
            if (!$product) {
                $this->_goBack();
        return;
            }

            $cart->addProduct($product, $params);
            if (!empty($related)) {
                $cart->addProductsByIds(explode(',', $related));
            }

            $cart->save();

            $this->_getSession()->setCartWasUpdated(true);

            /**
             * @todo remove wishlist observer processAddToCart
             */
            Mage::dispatchEvent('checkout_cart_add_product_complete',
                array('product' => $product, 'request' => $this->getRequest(), 'response' => $this->getResponse())
            );

            if (!$this->_getSession()->getNoCartRedirect(true)) {
                if (!$cart->getQuote()->getHasError()){
                    $message = $this->__('%s was added to your cart.', Mage::helper('core')->escapeHtml($product->getName()));
                    $this->_getSession()->addSuccess($message);
                }
                $this->_goBack();
            }
        } catch (Mage_Core_Exception $e) {
            if ($this->_getSession()->getUseNotice(true)) {
                $this->_getSession()->addNotice(Mage::helper('core')->escapeHtml($e->getMessage()));
            } else {
                $messages = array_unique(explode("\n", $e->getMessage()));
                foreach ($messages as $message) {
                    $this->_getSession()->addError(Mage::helper('core')->escapeHtml($message));
                }
            }

            $url = $this->_getSession()->getRedirectUrl(true);
            if ($url) {
                $this->getResponse()->setRedirect($url);
            } else {
                $this->_redirectReferer(Mage::helper('checkout/cart')->getCartUrl());
            }
        } catch (Exception $e) {
            $this->_getSession()->addException($e, $this->__('Cannot add the item to shopping cart.'));
            Mage::logException($e);
            $this->_goBack();
        }
    }

I am thinking we can store the url of the category/menu page in a variable and then assign it to 'return_url' like so:

$this->_getSession()->addSuccess($message);
$this->getRequest()->setParam('return_url', $menu_url);
}
$this->_goBack()

Is this an applicable solution?

If so, where would we assign the url to the variable?

perhaps in app/design/frontend/default/OURTHEME/template/catalog/category/view.phtml???

If not, any ideas on how I can accomplish this redirect? I've been stuck for 3 days.

Maybe we can sneak in some simple javascript: window.history.go(-1);