0

i want to redirect all my customers after login or registration to Dashboard if their cart is empty and to Checkout if they have products in cart. I use extension to do this redirection after login but is work only if is a product in cart. How I can sort this? This is my code:

   if (Mage::helper('customerredirect')->isEnabled() && !Mage::getSingleton("core/session")->getRedirectregister()){    
       $lasturl = Mage::getSingleton('core/session')->getLastUrl();
     if (strpos(Mage::helper('core/http')->getHttpReferer(), 'checkout') === false){
         if (! preg_match("#customer/account/create#", $lasturl) && Mage::helper('customerredirect')->isoptionEnabled('login_customerredirect')) {
                  if($this->_CustomerGroup()) {
                   $_session = $this->_getSession();
                   $_session->setBeforeAuthUrl(Mage::helper('customerredirect')->setRedirectOnLogin());
                 }
        }
    }

2 Answers 2

1

This should work like a charm...

app/etc/modules/Namespace_RedirectLogin.xml

Contains

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

next

local/Namespace/RedirectLogin/etc/config.xml

Contains

<?xml version="1.0"?>
<config>
    <modules>
        <Namespace_RedirectLogin>
            <version>1.0.0</version>
        </Namespace_RedirectLogin>
    </modules>
    <global>
        <models>
            <namespace_redirectlogin>
                <class>Namespace_RedirectLogin_Model</class>
            </namespace_redirectlogin>
        </models>
    </global>
    <frontend>
        <events>
            <customer_login>
                <observers>
                    <namespace_redirectlogin_customer_login>
                        <type>model</type>
                        <class>namespace_redirectlogin/observer</class>
                        <method>customerLogin</method>
                    </namespace_redirectlogin_customer_login>
                </observers>
            </customer_login>
        </events>
    </frontend>
</config>

and

local/Namespace/RedirectLogin/Model/Observer.php

Contains

<?php
class Namespace_RedirectLogin_Model_Observer
{

     public function customerLogin($observer)
     {
        $numberOfItems = Mage::helper('checkout/cart')->getItemsCount();
        if($numberOfItems > 0){
            Mage::app()->getFrontController()->getResponse()->setRedirect(Mage::getUrl('checkout/onepage'));
            Mage::app()->getResponse()->sendResponse();
            exit;
        }else{ 
            Mage::app()->getFrontController()->getResponse()->setRedirect(Mage::getUrl('cust‌​omer/account'));
            Mage::app()->getResponse()->sendResponse();
            exit;
        }



     }

}

To detect 'customer login action', i have used customer_login event. In this point, all I have to do was to check if cart contains products and redirect the user to the corresponding page.

7
  • thank you alex, but i see there is only when the $numberOfItems > 0 but how we can redirect to dashboard when $numberOfItems < 0 ?
    – Robert
    Commented Feb 14, 2016 at 21:17
  • see my updated answer from above Commented Feb 15, 2016 at 6:20
  • i think we are almost there but I don't see there the registration is only the lfor ogin process
    – Robert
    Commented Feb 15, 2016 at 6:48
  • This is working for registration as well as login. I have tested that. Commented Feb 15, 2016 at 7:07
  • ok I will try it right now
    – Robert
    Commented Feb 15, 2016 at 7:08
0

You can do it by using observer.
Step 1.Create config file for your module.

app/code/local/Arun/Rai/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Arun_Rai>
            <version>1.0.0</version>
        </Arun_Rai>
    </modules>
    <global>
        <models>
            <arun_rai>
                <class>Arun_Rai_Model</class>
            </arun_rai>
        </models>
    </global>
    <frontend>
        <events>
            <customer_login>
                <observers>
                    <arun_rai_customer_login>
                        <type>model</type>
                        <class>arun_rai/observer</class>
                        <method>customerLogin</method>
                    </arun_rai_customer_login>
                </observers>
            </customer_login>
        </events>
    </frontend>
</config>

Step 2.Create observer file to setup your logic.

app/code/local/Arun/Rai/Model/Observer.php

<?php

class Arun_Rai_Model_Observer
{
    public function customerLogin($observer)
    {
        // you can put your business logic here
        $item = Mage::helper('checkout/cart')->getSummaryCount();
        if($item >0)
        {
            Mage::app()->getFrontController()->getResponse()->setRedirect(Mage::getUrl('checkout/onepage'));
            Mage::app()->getResponse()->sendResponse();
            exit;
        }else{
            Mage::app()->getResponse()->setRedirect(Mage::getUrl('customer/account/'));
            Mage::app()->getResponse()->sendResponse();
            exit;
        }
    }

}

Step 3.Register your Module

app/etc/modules/Arun_Rai.xml

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

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.