Customize Magento’s Cart Controller

Recently I had to customise Magento’s Cart Controller to ajaxify the process to add products to the cart.

It’s not advisable to edit any core files because updates won’t go smooth after. So extend Magento’s Cart Controller with your own extension is the way to go and it’s not that hard if you follow some rules.

1. Create your module hierarchy & folders

My_Checkout
app
code
local
My
Checkout
controllers
CartController.php
etc
config.xml
etc
modules
My_Checkout.xml

2. Create your module’s xml

<?xml version="1.0"?>
<config>
<modules>
<My_Checkout>
<active>true</active>
<codePool>local</codePool>
<depends>
<Mage_Checkout/>
</depends>
</My_Checkout>
</modules>
</config>

3. Create your module’s configuration

<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<My_Checkout>
<version>0.1.0</version>
</My_Checkout>
</modules>
<frontend>
<routers>
<checkout>
<args>
<modules>
<My_Checkout before="Mage_Checkout">My_Checkout</My_Checkout>
</modules>
</args>
</checkout>
</routers>
</frontend>
</config>

4. Extend & customize Magento to your need

<?php
require_once Mage::getModuleDir('controllers', 'Mage_Checkout').DS.'CartController.php';
/**
* Shopping cart controller
*/
class My_Checkout_CartController extends Mage_Checkout_CartController
{
/**
* Action list where need check enabled cookie
*
* @var array
*/
protected $_cookieCheckActions = array('add');
/**
* Add product to shopping cart action
*
* @return Mage_Core_Controller_Varien_Action
* @throws Exception
*/
public function addAction()
{
...
}
public function deleteAction()
{
...
}

Tip:¬†Modman makes extension development more easy. It allows you to completely separate your extension from your app by creating symlinks and version control will be a piece of cake. You won’t regret it. Just read the Wiki.

Leave a Reply

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