Quantcast
Channel: Ghan – Code Insects – Insects who Code
Viewing all articles
Browse latest Browse all 26

Overriding Magento controller or Extend Magento core functionality

$
0
0

In Magento sometimes we need to customize Magento core functionality. Today I’ll show you how to customize Magento core functionality without messing with Magento core files

First of choose Magento module you need to customize, e.g. I need to customize Magento checkout module to add Ajax add to cart functionality.

To do so I have to override Cart Controller in Magento checkout module. i.e. /app/code/core/Mage/Checkout/controllers/CartController.php

To override any controller first of all create new module and files as below

    • /app/etc/modules/codeinsects_checkout.xml
    • /app/code/local/codeinsects/checkout/etc/config.xml
    • /aap/code/local/codeinsects/checkout/controllers/CartController.php

Add following code in /app/etc/modules/codeinsects_checkout.xml

1
2
3
4
5
6
7
8
9
<?xml version="1.0"?>
	<config>
		<modules>
			<codeinsects_checkout>
				<active>true</active>
				<codePool>local</codePool>
			</ codeinsects_checkout>
		</modules>
	</config>

And then add following code in /app/code/local/codeinsects/checkout/etc/config.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?xml version="1.0" encoding="UTF-8"?>
	<config>
		<modules>
			<codeinsects_checkout> <!-- Change codeinsects To Your Module NameSpace and change checkout to Your Module name-->
				<version>0.1.0</version>
			</codeinsects_checkout>
		</modules>
		<frontend>
			<routers>
				<checkout> <!-- checkout is core module name for mage_customer it will be customer -->
					<args>
						<modules>
							<codeinsects_checkout before="Mage_Checkout">codeinsects_checkout</codeinsects_checkout> <!-- Change codeinsects To Your Module NameSpace and change checkout to Your Module name-->
						</modules>
					</args>
				</checkout>
			</routers>
		</frontend>
	</config>

In above code “Mage_Checkout” is module in name in core directory of Magento, It will be “Mage_Customer” for customer module in Magento

Now add following line in /app/code/local/codeinsects/checkout/controller/CartController.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
require_once 'Mage/Checkout/controllers/CartController.php';
class codeinsects_checkout_CartController extends Mage_Checkout_CartController
{
	public function addAction()
	{
      /** CUSTOM CODE for override add action of magento cart controller */
		$cart   = $this->_getCart();
        $params = $this->getRequest()->getParams();
        $product= $this->_initProduct();
        $related= $this->getRequest()->getParam('related_product');
 
         /**
          * Check product availability
          */
         if (!$product) {
             $this->_goBack();
             return;
         }
 
         try {
             $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())
             );
             $message = $this->__('%s was successfully added to your shopping cart.', $product->getName());
			 $output=array('status'=>'success','message'=>$message);
			echo Mage::helper('core')->jsonDecode($output);             
         }
         catch (Mage_Core_Exception $e) {
			 $message=$this->__('Can not add item to shopping cart');
			 $output=array('status'=>'success','message'=>$message);
			echo Mage::helper('core')->jsonDecode($output);             
         }
         catch (Exception $e) {
			 $message=$this->__('Can not add item to shopping cart');
			 $output=array('status'=>'success','message'=>$message);
			echo Mage::helper('core')->jsonDecode($output);             
         }
	}
}
?>

Now after doing this, when you will click on add to cart button on front end, It’ll show you json out on page, After this you can add your own javascript or prototype script to show this output in popup or as message on page.

In case of any question please feel free to comment below


Viewing all articles
Browse latest Browse all 26

Trending Articles