Magento 1.9: Overwrite Core Account Controller for Custom Login Actions

customcustomermagento-1.9module

I have been trying without success to overwrite a couple of functions in the Account Controller within the customer folder of Magento core (originally I had just modified the core files, but soon realised that isn't the correct way).

To get to where I am currently I have:

  1. Created a folder structure in app/code/local:
    My folder structure is:

    - Speedcrete (module name)
        - Customer (core magento module to overwrite)
            - controllers (contains accountcontroller.php)
            - etc (which contains the modules config.xml)
    
  2. Created an XML file called Speedcrete_All.xml in app/etc/modules

    The files look like:
    Account controller:

    <?php
    /**
     * Customer account controller
     *
     * @category   Speedcrete
     * @package    Speedcrete_Customer
     * @author     Speedcrete 
     */
    require_once 'Mage/Customer/controllers/AccountController.php';
    //we need to add this one since Magento wont recognize it automatically
    
    class Speedcrete_Customer_AccountController extends Mage_Customer_AccountController 
    {
        public function indexAction()
        {
            echo "jlkgfjdlfjgf";
            die();
            parent::indexAction();
        }
    
        /**
         * Customer login form page
         */
        public function loginAction()
        {
            echo "hello world i am being overwritten";
            die();
            // if we've got come from the checkout page, send variable to front end to be displayed as message
            if (isset($_GET['checkout_page'])) {
                $origin_page = $_GET['checkout_page'];
                $coupon_code = $_GET['coupon_code'];
    
                Mage::register('message' , 'You need to be logged in before you can use coupons!');
                Mage::register('checkout_flag', '1');
                Mage::register('coupon_code', $coupon_code);
    
            }
            if ($this->_getSession()->isLoggedIn()) {
                $this->_redirect('*/*/');
                return;
            }
            $this->getResponse()->setHeader('Login-Required', 'true');
            $this->loadLayout();
            $this->_initLayoutMessages('customer/session');
            $this->_initLayoutMessages('catalog/session');
            $this->renderLayout();
        }
    
        /**
         * Login post action
         */
        public function loginPostAction()
        {
            echo "hkjhfkjhdskfhkjdfsh";
            die();
            if (!$this->_validateFormKey()) {
                $this->_redirect('*/*/');
                return;
            }
    
            if ($this->_getSession()->isLoggedIn()) {
                $this->_redirect('*/*/');
                return;
            }
            $session = $this->_getSession();
    
            if ($this->getRequest()->isPost()) {
                $login = $this->getRequest()->getPost('login');
                //hidden field in front end
                if($login['checkout_flag'] == 1) {
                    $checkout_flag = $login['checkout_flag'];
                    $coupon_code = $login['coupon_code'];
                } else {
                    $checkout_flag = '';
                    $coupon_code = '';
                }
    
                if (!empty($login['username']) && !empty($login['password'])) {
                    try {
                        $session->login($login['username'], $login['password']);
                        if ($session->getCustomer()->getIsJustConfirmed()) {
                            $this->_welcomeCustomer($session->getCustomer(), true);
                        }
                    } catch (Mage_Core_Exception $e) {
                        switch ($e->getCode()) {
                            case Mage_Customer_Model_Customer::EXCEPTION_EMAIL_NOT_CONFIRMED:
                                $value = $this->_getHelper('customer')->getEmailConfirmationUrl($login['username']);
                                $message = $this->_getHelper('customer')->__('This account is not confirmed. <a href="%s">Click here</a> to resend confirmation email.', $value);
                                break;
                            case Mage_Customer_Model_Customer::EXCEPTION_INVALID_EMAIL_OR_PASSWORD:
                                $message = $e->getMessage();
                                break;
                            default:
                                $message = $e->getMessage();
                        }
                        $session->addError($message);
                        $session->setUsername($login['username']);
                    } catch (Exception $e) {
                        // Mage::logException($e); // PA DSS violation: this exception log can disclose customer password
                    }
                } else {
                    $session->addError($this->__('Login and password are required.'));
                }
            }
    
            $this->_loginPostRedirect($checkout_flag, $coupon_code);
        }
    
        /**
         * Define target URL and redirect customer after logging in
         */
        protected function _loginPostRedirect($checkout_flag, $coupon_code)
        {
            $session = $this->_getSession();
            //var_dump($checkout_flag);
            //die();
            if ($checkout_flag == "") {
    
                if (!$session->getBeforeAuthUrl() || $session->getBeforeAuthUrl() == Mage::getBaseUrl()) {
                    // Set default URL to redirect customer to
                    $session->setBeforeAuthUrl($this->_getHelper('customer')->getAccountUrl());
                    // Redirect customer to the last page visited after logging in
                    if ($session->isLoggedIn()) {
                        if (!Mage::getStoreConfigFlag(
                            Mage_Customer_Helper_Data::XML_PATH_CUSTOMER_STARTUP_REDIRECT_TO_DASHBOARD
                        )) {
    
                            $referer = $this->getRequest()->getParam(Mage_Customer_Helper_Data::REFERER_QUERY_PARAM_NAME);
                            if ($referer) {
                                // Rebuild referer URL to handle the case when SID was changed
                                $referer = $this->_getModel('core/url')
                                    ->getRebuiltUrl( $this->_getHelper('core')->urlDecodeAndEscape($referer));
                                if ($this->_isUrlInternal($referer)) {
                                    $session->setBeforeAuthUrl($referer);
                                }
                            }
                        } else if ($session->getAfterAuthUrl()) {
    
                            $session->setBeforeAuthUrl($session->getAfterAuthUrl(true));
                        }
                    } else {
                        $session->setBeforeAuthUrl( $this->_getHelper('customer')->getLoginUrl());
                    }
                } else if ($session->getBeforeAuthUrl() ==  $this->_getHelper('customer')->getLogoutUrl()) {
    
                    $session->setBeforeAuthUrl( $this->_getHelper('customer')->getDashboardUrl());
                } else {
    
                    if (!$session->getAfterAuthUrl()) {
                        $session->setAfterAuthUrl($session->getBeforeAuthUrl());
                    }
                    if ($session->isLoggedIn()) {
                        $session->setBeforeAuthUrl($session->getAfterAuthUrl(true));
                    }
                }
                $this->_redirectUrl($session->getBeforeAuthUrl(true));
            } else {
    
                $this->_redirect('checkout/cart', array('_query' => 'checkout_page=1&coupon_code='.$coupon_code));
            }
        }
    
    
    }
    
  3. config.xml within module folder

    <?xml version="1.0"?>
    <config>
        <modules>
            <Speedcrete_Customer>
                <version>1.0</version>
            </Speedcrete_Customer>
        </modules>
    
        <frontend>
            <routers>
                <customer>
                    <args>
                        <modules>
                            <Speedcrete_Customer_controllers_Account before="Mage_Customer">Speedcrete_Customer_controllers_Account</Speedcrete_Customer_controllers_Account>
                        </modules>
                    </args>
                </customer>
            </routers>
        </frontend>
        <layout>
            <updates>
                <mage_customer>
                    <file>speedcrete.xml</file>
                </mage_customer>
            </updates>
        </layout>
    </config>
    
  4. then finally the Speedcrete_All.xml which is within app/etc/modules

    <config>
        <modules>
            <Speedcrete_Customer>
                <active>true</active>
                <codePool>local</codePool>
                <depends>
                    <Mage_Customer />
                </depends>
            </Speedcrete_Customer>
        </modules>
    </config>
    

Hopefully this makes sense, nothing happens when refreshing the account login page either when logging in!

Best Answer

Check if your module is registered. `

system->configuration->advance->disable modules output

Then in your config.xml file change the code like

  <frontend>
    <routers>
        <customer>
            <args>
                <modules>
                    <Speedcrete_Customer before="Mage_Customer">Speedcrete_Customer</Speedcrete_Customer>
                </modules>
            </args>
        </customer>
    </routers>
</frontend>

`

Related Topic