Remove item magento cart redirects homepage

This tutorial explains how to fix the remove item magento cart redirects homepage error.When removing an item from the cart using the remove button in the checkout page it redirects to the homepage.The error is due the redirect function in the cartcontroller.php file

The error can be fix by rewriting the core files. In magento for editing the core files we need to create a module.Please create the following files.

remove item magento cart redirects homepage

Step by step explanation :

  1. Enable Module
  2. Create Config.xml
  3. Rewrite core file

Enable Module

For enabling the module create core_cart.xml file inside the app/etc/mdodules folder.In the core_cart.xml file write the following code.

<?xml version="1.0"?>
<config>
    <modules>
        <Core_Cart>
            <active>true</active>
            <codePool>local</codePool>
        </Core_Cart>
    </modules>
</config>

Create Config.xml

For editing the core file create the config.xml as shown below.

<?xml version="1.0"?>
<config>
    <modules>
        <Core_Cart>
            <version>0.1.0</version>
        </Core_Cart>
    </modules>
    <global>
        <models>
            <core_cart>
                <class>Core_Cart_Model</class>
                <resourceModel>core_cart_resource</resourceModel>
            </core_cart>
            <core_cart_resource>
                <class>Core_Cart_Model_Resource</class>
            </core_cart_resource>
        </models>
        <helpers>
            <core_cart>
                <class>Core_Cart_Helper</class>
            </core_cart>
        </helpers>
    </global>
    <frontend>
        <routers>
            <checkout>
                <args>
                    <modules>
                        <core_cart before="-">Core_Cart</core_cart>
                    </modules>
                </args>
            </checkout>
        </routers>
    </frontend>
</config>

Rewrite core file

The error is due the redirect function in the deleteAction() function in cartcontroller.php. For fixing this error change the $this->_redirectReferer(Mage::getUrl(‘*/*’)); to $this->_redirect(‘checkout/cart’);

Copy and paste the following code.

require_once 'Mage/Checkout/controllers/CartController.php';


class Core_Cart_CartController extends Mage_Checkout_CartController {


    /**
     * Delete shoping cart item action
     */
    public function deleteAction()
    {
        if ($this->_validateFormKey()) {
            $id = (int)$this->getRequest()->getParam('id');
            if ($id) {
                try {
                    $this->_getCart()->removeItem($id)
                        ->save();
                } catch (Exception $e) {
                    $this->_getSession()->addError($this->__('Cannot remove the item.'));
                    Mage::logException($e);
                }
            }
        } else {
            $this->_getSession()->addError($this->__('Cannot remove the item.'));
        }

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

}

If you have any further queries please leave a reply.

Thanks.

Share this Tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *

*