2

I want to redirect the customer after successful login to home page and not redirect in checkout page login i have used the following event and its not worked for me is there any other event to take place this

app/code/local/Cusredirection/Customer/Model/Observer.php

 /**
  * Run couple of 'php' codes after customer logs in
  *
  * @param Varien_Event_Observer $observer
  */
 public function customerLogin($observer)
 {
     Mage::log(__METHOD__ . '() Hello!'); // Remove afterwards. Check your var/log/system.log to see if came to this point
     $customer = $observer->getCustomer();
     $session = Mage::getSingleton('customer/session');
    if (strpos(Mage::helper('core/http')->getHttpReferer(), 'checkout') !== false){
        Mage::log("Checkout Page",null,"mylog.log");
        $session->setAfterAuthUrl(Mage::getBaseUrl());  
    }
    else {
        Mage::log("Other Pages",null,"mylog.log");
        $session->setAfterAuthUrl(Mage::helper('core/http')->getHttpReferer());
    }
    $session->setBeforeAuthUrl('');
     // "run couple of 'php' codes"
 }

}

app/code/local/Cusredirection/Customer/etc/config.xml

<?xml version="1.0"?>

0.1 Cusredirection_Customer_Model model cusredirection_customer/observer customerLogin

4 Answers 4

2

Try with updating your config.xml by updating,

<class>cusredirection_customer/observer</class>

to

<class>Cusredirection_Customer_Model_Observer</class>

Try below observer code.

<?php
class Cusredirection_Customer_Model_Observer
{
    public function setRedirectOnLogin(){
        $session = Mage::getSingleton('customer/session');
        if (strpos(Mage::helper('core/http')->getHttpReferer(), 'checkout') !== false){
            Mage::log("Checkout Page",null,"mylog.log");
            $session->setAfterAuthUrl(Mage::getBaseUrl());  
        }
        else {
            Mage::log("Other Pages",null,"mylog.log");
            $session->setAfterAuthUrl(Mage::helper('core/http')->getHttpReferer());
        }
        $session->setBeforeAuthUrl('');
    }
}
3
  • Still it going to my account page (customer/account/index/) is there any way to check that event is working or not?
    – amith lal
    Commented Mar 8, 2017 at 5:31
  • Clear your cache and check. You can add Mage::log("Checkout Page",null,"mylog.log") in your if and else condition to check if they are being called and which part is being called. You can also add Mage::log(Mage::helper('core/http')->getHttpReferer(),null,"mylog.log") to check referer page URL. It will generate mylog.log file in var/log location Commented Mar 8, 2017 at 5:36
  • Also make sure you have you have </config> in your config.xml Commented Mar 8, 2017 at 8:57
0

Use the event customer_login:

Add the event to your module's config.xml file (app/code/local/Company/Modulename/etc/config.xml):

<?xml version="1.0"?>
<config>
    <modules>
        <Company_Modulename>
            <version>0.1</version>
        </Company_Modulename>
    </modules>
    <global>
        <models>
            <modulename>
                <class>Company_Modulename_Model</class>
            </modulename>
        </models>
    </global>
    <events>
        <customer_login>
            <observers>
                <yourobservername>
                    <type>model</type>
                    <class>modulename/observer</class>
                    <method>customerLogin</method>
                </yourobservername>
            </observers>
        </customer_login>
    </events>
</config>

Now create a model Company_Modulename_Model_Observer (app/code/local/Company/Modulename/Model/Observer.php). Add a customerLogin() method to the class:

Your observer class would look like this:

class Company_Modulename_Model_Observer
{
    public function customerLogin($observer)
    {
        $customer = $observer->getCustomer();
    }
}

hope this will sure help you.

6
  • can i add the redirection script that i show above insidepublic function customerLogin($observer){
    – amith lal
    Commented Mar 8, 2017 at 8:44
  • Yes. you can add. Commented Mar 8, 2017 at 8:45
  • @vishal_gelani ERR (3): Warning: simplexml_load_string(): Entity: line 15: parser error : Premature end of data in tag layout line 4 in /home/qnhealth/public_html/demo/app/code/core/Mage/Core/Model/Layout/Update.php on line 450 i got this error in log file
    – amith lal
    Commented Mar 8, 2017 at 9:13
  • Updated comment. Please check it. Commented Mar 8, 2017 at 9:23
  • I have updated the code please check it its also not working
    – amith lal
    Commented Mar 8, 2017 at 9:36
0

Finally i got the answer i have make some changes on Observer.php

<?php
class Cusredirection_Customer_Model_Observer
{
     public function customerLogin($observer)
     {
         //$customer = $observer->getCustomer();
         $session = Mage::getSingleton('customer/session');
        if (strpos(Mage::helper('core/http')->getHttpReferer(), 'checkout') !== false){
            Mage::log("Checkout Page",null,"mylog.log");
            $session->setAfterAuthUrl(Mage::helper('core/http')->getHttpReferer());
        }
        else {
            Mage::log("Other Pages",null,"mylog.log");
            $session->setAfterAuthUrl(Mage::getBaseUrl());
        }
        $session->setBeforeAuthUrl('');
     }

}
1
  • @ Jaimin Sutariya but the url for redirect you had set is not properly there i make the change
    – amith lal
    Commented Mar 8, 2017 at 10:31
0

try

class Cusredirection_Customer_Model_Observer
{
    public function customerLogin()
    {
        return Mage::getSingleton('customer/session')->setBeforeAuthUrl(Mage::helper('checkout/cart')->getCartUrl());
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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