Monday, June 17, 2013

Overriding controller class in magento

Sometime we have to run in the situation where we need to overwrite the core module. Note if you make changes in the core file then it will trouble you at the time of up-gradation.

So I came up with the new blog which gives you the idea about how to override controllers in magento.

Now suppose we want to override the checkout/cart controller. In this case, we will have to edit just two files in our custom module.
Suppose our custom module is present in the namespace 'Donebynone' and our module is named as 'Mukund'.

First Step:-

Open the app\code\local\Donebynone\Mukund\etc\config.xml and edit it as follows:
The name of the core module to be overridden is written in between the router tags. Here we want to override the checkout module so checkout is to be wrapped in <routers> tags.

After this we will tell Magento to call our custom module before the Mage/Checkout module.

<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
  <Donebynone_Mukund>
    <version>0.0.1</version>
  </Donebynone_Mukund>
</modules>
<frontend>
  <routers>
    <checkout><!-- Name of core module to be overridden  -->
      <args>
        <modules>
          <Donebynone_Mukund before="Mage_Checkout">Donebynone_Mukund</Donebynone_Mukund>
    <!-- Above line will tell Magento to call our custom module before the Mage/Checkout module -->
        </modules>
      </args>
    </checkout>
  </routers>
</frontend>
</config>

Final Step:-

Now we need to create the controller file to be overridden, CartController.php in our case.
(app\code\local\Donebynone\Mukund\controllers\CartController.php)


<?php


/*
 Include the core file to be overridden
 */
require_once("Mage/Checkout/controllers/CartController.php");

/*
 * Extend the core controller in our custom controller.
 */
class Donebynone_Mukund_CartController extends Mage_Checkout_CartController
{
   //Write your fucntion here..

    public function indexAction()
    {
        echo "This controller has been overridden.";
    }
}

?>

Now its your turn to do the job...go ahead with confidence.





Need any help on this....write your comments...

Saturday, June 15, 2013

Custom Module in Magento

Hi Freinds,

Today I am posting about custom module in magento. Hope it will be fruitful.


Creating a new module in magento

Structure of sample file:
[root]\app\code\local\{Namespace}\{Modulename}
[root]\app\code\local\{Namespace}\{Modulename}\controllers
[root]\app\code\local\{Namespace}\{Modulename}\etc
[root]\app\code\local\{Namespace}\{Modulename}\etc\config.xml
[root]\app\code\local\{Namespace}\{Modulename}\Helper
[root]\app\code\local\{Namespace}\{Modulename}\Helper\Data.php
[root]\app\code\local\{Namespace}\{Modulename}\Model
[root]\app\code\local\{Namespace}\{Modulename}\{Modulename}.php

{Namespace} is a user defined variable. 
{Modulename} this is the name of your module.


For Example I am using Donebynone as the Namespace and Helloworld as my module name

First using controllers...all controllers goes here and name this file as it suits you .Below is controller code...just for example

####################################################################################################
<?php
//IndexController is the default controller
class Donebynone_Helloworld_IndexController extends Mage_Core_Controller_Front_Action {
    //indexAction is the default Action for any controller
    function indexAction() {
        echo "indexAction";
        $helloworld = Mage::getModel("donebynone_helloworld/helloworld");
        $helloworld->helloworld("helloworld");
    }
}

####################################################################################################


Data.php is just a helper file for the Magento , I’m not exactly sure what it does but it appears to be exactly the same in every module except for its class definition.

###################################################################################################

<?php
class Donebynone_Helloworld_Helper_Data extends Mage_Core_Helper_Abstract {
}

###################################################################################################

Coming to the Model. It holds all your module.In Magento core you’ll notice that most modules contain many models.

###################################################################################################
<?php
class Donebynone_Helloworld_Model_Helloworld extends Varien_Object {
    function __construct() {
        parent::__construct();
    }
    function helloworld($arg) {
        echo "<br>Hello World! My argument is : " . $arg;
    }
}
###################################################################################################

Next is important one...config.xml  where you connect all your code up to Magento.

####################################################################################################
<?xml version="1.0" encoding="UTF-8" ?>
<config>
    <modules>
        <Donebynone_Helloworld>
            <version>0.1.0</version>
        </Donebynone_Helloworld>
    </modules>
    <frontend>
        <routers>
            <Donebynone>    <!-- I make this match my front name but I'm not sure it matters -->
                <use>standard</use>    <!-- Use standard routing as opposed to admin.  IE: frontend vs admin -->
                <args>
                    <module>donebynone_Helloworld</module>    <!-- The module to search for controllers -->
                    <frontName>donebynone</frontName>    <!-- The first descriminator in the path.  "donebynone" in http://localhost/donebynone/ -->
                </args>
            </donebynone>
        </routers>
    </frontend>
    <global>
        <models>
            <donebynone_helloworld>    <!-- This is used when istanting your Model EG: Mage::getModel("donebynone_helloworld/hellworld") -->
                <class>donebynone_Helloworld_Model</class>    <!-- That class to use when isntanting objects of type above. -->
            </donebynone_helloworld>
        </models>
    </global>
</config>
####################################################################################################


Now Finally [root]\app\etc\modules\{Namespace}_{Modulename}.xml in our case it is Donebynone_Helloworld.xml. This file basically tells magento that there is a new module and you have to load it.

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


Thanks no more hard work just go to  Admin->System -> Configuration -> Advanced and you should see your module enabled here.

Dont forget to flush your magento cache.

You are done!!

Also check this for reference purpose
(http://www.magentocommerce.com/wiki/custom_module_with_custom_database_table)