0

I need to add some custom controller actions in customer module core files in magento. How I can do this without touching the core files? I need to update the following files from the core customer module:

frontend/base/default/layout/customer.xml

app/code/core/mage/customer/controller/AccountController.php, navigation

frontend/base/default/template/customer/account/navigation.phtml

frontend/base/default/template/persistent/customer/form/register.phtml

Another file is the theme file which I also need to override/overwrite:

frontend/mtghost/default/template/catalog/product/view.phtml

Any help would be appreciated.

4

a) frontend/base/default/layout/customer.xml

You should not override this file. If you want to make any in layout, create a new local.xml file in your theme.

b) app/code/core/mage/customer/controller/AccountController.php

You must create a module for overriding the AccountController.php file as follow.

1) app/etc/modules/Namespace_Module.xml

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

2) app/code/local/Namespace/Module/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Namespace_Module>
            <version>0.1.0</version>
        </Namespace_Module>
    </modules>
    <frontend>
        <routers>
            <customer>
                <args>
                    <modules>
                        <namespace_module before="Mage_Customer">Namespace_Module</namespace_module>
                    </modules>
                </args>
            </customer>
        </routers>
    </frontend>
</config>

3) app/code/local/Namespace/Module/controllers/AccountController.php

<?php
require_once(Mage::getModuleDir('controllers','Mage_Customer').DS.'AccountController.php');

class Namespace_Module_AccountController extends Mage_Customer_AccountController
{
    /**
     * Override the required function here, 
     * e.g. createAction
     */
    public function createAction()
    {
        $this->_redirect('*/*/login');
    }
}

That's the proper way you should use to override the controller.

c) frontend/base/default/template/customer/account/navigation.phtml

Simple place this file in your theme using the same hierarchy as follow.

frontend/your_package/your_theme/template/customer/account/navigation.phtml

d) frontend/base/default/template/persistent/customer/form/register.phtml

Simple place this file in your theme using the same hierarchy as follow.

frontend/your_package/your_theme/template/persistent/customer/form/register.phtml

e) frontend/mtghost/default/template/catalog/product/view.phtml

This file is already in a custom theme. If its your theme then there is no harm in editing in it. If you have another theme then place this file in it as follow and override.

frontend/your_package/your_theme/template/catalog/product/view.phtml

f) For adding custom tab in customer account, add the file with content below.

frontend/your_package/your_theme/layout/local.xml

<?xml version="1.0" encoding="UTF-8"?>
<layout>
    <customer_account>
        <reference name="customer_account_navigation">
            <action method="addLink">
                <name>custom_tab</name>
                <path>customtab/index/index</path>
                <label>My custom tab</label>
            </action>
        </reference>
    </customer_account>
</layout>
  • Thanks for the awesome answer. Can I add new functions also in the AccountController.php file and these functions will add to the customer module right? – mudit mehrotra Sep 22 '16 at 10:22
  • Yes you can add new as well as override old ones. Also you can call the parent class functions which is the true essence of overriding. – saiid Sep 22 '16 at 10:50
  • Thanks and I am using the mtghost magento theme so should I add all the xml in mtghost/default/layout/local.xml which I need to add in frontend/base/default/layout/customer.xml – mudit mehrotra Sep 22 '16 at 11:07
  • I need to add a new link in account section sidebar of the customer of specific group that's why I have edited the core customer.xml. I have add all the xml in local.xml of the theme but its not working – mudit mehrotra Sep 22 '16 at 11:14
  • I am not using my own theme I am using mtghost magento theme so I have edited its file directly is there any issue with it. Please, suggest as I am new to magento – mudit mehrotra Sep 22 '16 at 11:17
1

You should have your own theme for overwriting template files. something like:

frontend/yourtheme/default/layout/customer.xml

put files you want to overwrite in the exact same part in your theme. configure that the shop uses your theme in magento backend:

system/configuration/general/design/ adjust here package & theme

but there are different ways to do so + you should inform you a bit more

your core file problem

i think simplest solution is:

duplicate that file

app/code/core/mage/customer/controller/AccountController.php

into

app/code/local/mage/customer/controller/AccountController.php

make your changes.

but generally you should have an own module and extend your desired function.

  • So I need to add the require_once 'Mage/Customer/controllers/AccountController.php'; in my custom controller file right? – mudit mehrotra Sep 22 '16 at 9:47
  • also how to inform magento that my module has override function for the customer account? Is it the config.xml that do this but how? – mudit mehrotra Sep 22 '16 at 9:48
  • You cannot override controller by just changing the code pool b'coz its not bind with autoload class of magento – Murtuza Zabuawala Sep 22 '16 at 9:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.