1

I try to overwrite a function from a core class without having to create an extra extension for it.

I need to overwrite the function _loginPostRedirect() from Mage_Customer_AccountController to change the redirect behaviour.

The class is in app\code\core\Mage\Customer\controllers\AccountController.php

Im not sure about the fall-back way of magento, but I remember I readed that magento first looks in app\code\local\ instead of app\code\core to find out if the file exists there and then app\code\local\Mage\Customer\controllers\AccountController.php is getting loaded instead of app\code\core\Mage\Customer\controllers\AccountController.php.

But I tried it and magento still loads the file from app\code\core.

How does it work?

Basically I just want to change one line at the very bottom:

    /**
     * Define target URL and redirect customer after logging in
     */
    protected function _loginPostRedirect()
    {
        $session = $this->_getSession();

        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));
        $this->_redirectUrl("/customer/account");
    }
  • 1
    You cannot copy controllers to local. You need to create an extension then override it. – Sukumar Gorai Sep 12 '18 at 11:19
  • Ok, so I can only overwrite Block , Model , Helper not controller by placing a file in app/code/local/ as @Ansar Husain said – Black Sep 12 '18 at 11:37
  • 1
    Ansar saying to create extension with extension. And extension can be placed under local or community. – Sukumar Gorai Sep 12 '18 at 11:43
  • 1
    My means you can copy core file in app/code/local/Mage directly (Block , Model , Helper) without module but controller does not work in directly copy inside local/Mage – Ansar Husain Sep 12 '18 at 12:07
  • @AnsarHusain, you are incorrect. I also tried it with a model and it does not work either. I copied app\code\core\Mage\Core\Model\Resource\Setup.php to app\code\local\Mage\Core\Model\Resource\Setup.php and edited it, but the original is still getting loaded. – Black Sep 17 '18 at 14:36
1

You will have to override that core controller by simple module what i have made . you can copy code inside app/code/local/Mage only Block , Model , Helper not controller.

1. Create a file config.xml /app/code/local/Vendor/Module/etc.

<config>
    <modules>
        <Vendor_Module>
            <version>1.0.1</version>
        </Vendor_Module>
    </modules>
        <frontend>
            <routers>
                <customer>
                    <args>
                        <modules>
                            <vendor_module before="Mage_Customer">Vendor_Module_Customer</vendor_module>
                        </modules>
                    </args>
                </customer>
            </routers>
        </frontend>
</config>

2. Create a controller file app/code/local/Vendor/Module/controllers/Customer/AccountController.php

<?php
require_once 'Mage/Customer/controllers/AccountController.php';
class Vendor_Module_AccountController extends Mage_Customer_AccountController
{
         protected function _loginPostRedirect()
    {
        $session = $this->_getSession();

        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));
        $this->_redirectUrl("/customer/account");
    }
}

3. Create file app/etc/modules/Vendor_Module.xml

<?xml version="1.0"?>
    <config>
        <modules>
            <Vendor_Module>
                <active>true</active>    //  is active or not
                <codePool>local</codePool> //location of the module i.e inside the local folder
            </Vendor_Module>
        </modules>
    </config>
  • But this is an extension? I wanted to do it without. I see many occurences of Mage Core code in app/code/local made by other devs from another company of us, so I thought it is possible – Black Sep 12 '18 at 11:25
  • 1
    you will have to override that core controller by simple module what i have made . you can copy code inside app/code/local/Mage only Block , Model , Helper not controller – Ansar Husain Sep 12 '18 at 11:28
  • Please include your last comment in your answer and I will accept it – Black Sep 12 '18 at 11:30
  • I have added comment in post. – Ansar Husain Sep 12 '18 at 11:32
  • Ok, I will test your code and report back. – Black Sep 12 '18 at 11:34

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.