Magento – Modifying the redirect after editing a product in the cart

cartmagento-1.9magento-ceredirect

I have a problem modifying a redirect in magento CE 1.9.2.

Let's say a customer has a configurable product in the cart, and would like to edit that product (ie. changing color or size or something).

After changing the options and clicking the 'update cart' button I want the customer to stay on the same page (ie. the edit product page). However magento always redirect back to the cart page.

I think I've found source of the problem, in CartController.php from the Checkout module:

/**
 * Update product configuration for a cart item
 */
public function updateItemOptionsAction()
{
    $cart   = $this->_getCart();
    $id = (int) $this->getRequest()->getParam('id');
    $params = $this->getRequest()->getParams();

    if (!isset($params['options'])) {
        $params['options'] = array();
    }
    try {
        if (isset($params['qty'])) {
            $filter = new Zend_Filter_LocalizedToNormalized(
                array('locale' => Mage::app()->getLocale()->getLocaleCode())
            );
            $params['qty'] = $filter->filter($params['qty']);
        }

        $quoteItem = $cart->getQuote()->getItemById($id);
        if (!$quoteItem) {
            Mage::throwException($this->__('Quote item is not found.'));
        }

        $item = $cart->updateItem($id, new Varien_Object($params));
        if (is_string($item)) {
            Mage::throwException($item);
        }
        if ($item->getHasError()) {
            Mage::throwException($item->getMessage());
        }

        $related = $this->getRequest()->getParam('related_product');
        if (!empty($related)) {
            $cart->addProductsByIds(explode(',', $related));
        }

        $cart->save();

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

        Mage::dispatchEvent('checkout_cart_update_item_complete',
            array('item' => $item, 'request' => $this->getRequest(), 'response' => $this->getResponse())
        );
        if (!$this->_getSession()->getNoCartRedirect(true)) {
            if (!$cart->getQuote()->getHasError()) {
                $message = $this->__('%s was updated in your shopping cart.', Mage::helper('core')->escapeHtml($item->getProduct()->getName()));
                $this->_getSession()->addSuccess($message);
            }
            $this->_goBack();
        }
    } catch (Mage_Core_Exception $e) {
        if ($this->_getSession()->getUseNotice(true)) {
            $this->_getSession()->addNotice($e->getMessage());
        } else {
            $messages = array_unique(explode("\n", $e->getMessage()));
            foreach ($messages as $message) {
                $this->_getSession()->addError($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 update the item.'));
        Mage::logException($e);
        $this->_goBack();
    }
    $this->_redirect('*/*');
}

The call to $this->_goBack() in the end of the try-block seems to set the redirect URL correctly by looking at settings and a 'return_url' parameter (and I've verified that the code gets this far). But, as you can see, the last line always sets a redirect to */* no matter what. Just adding a return statement after the _goBack() call fixes the problem but I rather do this without modifying the core files.

Any ideas how I can solve this problem?

Best Answer

If you want the customer to say on the update page you could

  1. Use Ajax to post the changes to the server.

  2. Add a return_url parameter to your post action (with the current url see protected function _goBack())

  3. Change the global behavior for adding and updatig product in System -> Configuration -> Sales tab -> Checkout and then in the Shopping Cart tab you set the After Adding a Product Redirect to Shopping Cart

  4. Rewrite the controller using a custom module