0

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);

3 Answers 3

1

Get the category from the Magento product. Override the controller in a module and before the redirect use:

$ids = $product->getCategoryIds();
$_category = Mage::getModel('catalog/category')->load($ids->getFirstItem());
$redirect = $_category->getUrl($_category); 
$this->getResponse()->setRedirect($redirect);

and use $redirect to make it happen. This code is untested, but the approach should work.

0

You could try

Add a hidden form field in

In app/design/frontend/default/OURTHEME/template/catalog/category/view.phtml

<input type="hidden" name="current_category_id" value=<?php $_category->getId() ?>" />

Then to redirect after

$category_id = $this->getRequest()->getPost('current_category_id');
$category = Mage::getModel('catalog/category')->load($category_id);
$this->_redirectReferer($category->getUrl());

All changes should be done in a custom module or in local

0

I Have simplest solution. Go to CartController file and search this code $this->getResponse()->setRedirect($backUrl); And replace it with this code $custmurl = $_SERVER['HTTP_REFERER']; $this->getResponse()->setRedirect($custmurl); Try this.

Your Answer

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

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