0

I want to redirect a user after sign in to a store after checking customer group type. Now in login.phtml file, where should i make change? Where should i apply my condition of checking of customer type in the code? please help.

5

Use the customer_login event to avoid changing/rewriting/overriding core files.

In your config.xml

<config>
  <global>
    <models>
        ....
    </models>
    <events>
        <customer_login>
            <observers>
                <yourobservername>
                    <type>model</type>
                    <class>yourmodule/path_to_class</class>
                    <method>loginSwitchStore</method>
                </yourobservername>
            </observers>
        </customer_login>    
    </events>
  </global>
</config>

Your observer class (/app/code/local/YourCompany/YourModule/Model/Observer.php):

class YourCompany_YourModule_Model_Observer
{
    public function loginSwitchStore($observer)
    {
        $customer = $observer->getCustomer();

        switch($customer->getCustomerGroup())
        {
            case 1: $storeCode = 'storeview1';break;
            case 2: $storeCode = 'storeview2';break;
            case 3: $storeCode = 'storeview3';break;
        }

        Mage::app()->setCurrentStore($storeCode);

        //add this if you want them to stay in that store even after logout
        Mage::getModel('core/cookie')->set('store', $storeCode); 
    }
}
1
  • for redirecting "setCurrentStore" wasn't enough. I had to add the following Code: $params = array( '_current' => TRUE, '_use_rewrite' => TRUE, '_store_to_url' => TRUE, '_store' => Mage::app()->getStore($storeCode)->getId() ); $url = Mage::getUrl('', $params); Mage::app()->getResponse()->setRedirect($url); – fbtb Jan 10 '17 at 8:08
-1

Its simple, In your controller action use $this->_redirect('url'); at end of the action .The above code for redirect to a url for any action.

For your problem, goto _loginPostRedirect() in AccountController.php at Mage>customer>controller> here it calls helper function to get the redirect url,so change the required function in Data.php.

5
  • in login.php on which line i should place the above code? – Muhammad Shoaib Murtaza Oct 17 '14 at 7:06
  • Check my updated answer to find the file location where you need to change the code.I have not provided the function you need to change – Manikandan Arunachalam Oct 17 '14 at 9:08
  • i am trying.... – Muhammad Shoaib Murtaza Oct 17 '14 at 9:08
  • what should be the task of method present in data.php?? because there is a blank class in data.php, i am newbie in magento. – Muhammad Shoaib Murtaza Oct 21 '14 at 8:55
  • understand the codes in _loginPostRedirect() it gets the redirect url with the help of helper method..(helper method just return the redirect url like (name/controllername/actionname)). – Manikandan Arunachalam Oct 21 '14 at 9:04
-1

Redirection after logged In, Logged Out and Registration is very common issue in magento. Use the below code. It can help you. In you Model file write the following code.

/*method for Login Redirection*/
public function customerLogin(Varien_Event_Observer $observer)
{    
  if($this->_CustomerGroup()) {
     $_session = $this->_getSession();
     $_session->setBeforeAuthUrl(customurl);
  }
}
/*check the customer group*/
protected function _CustomerGroup()
{
   $customer = $this->_getSession()->getCustomer();
   if($customer) {
       //for Retailer
       if($customer->getGroupId() == 1) {
         return TRUE;
       }

      //for wholeseller
     if($customer->getGroupId() == 2) {
        return TRUE;
     }

    // for General
    if($customer->getGroupId() == 3) {
        return TRUE;
    }

 }
}
protected function _getSession()
{
  return Mage::getSingleton('customer/session');
}

"Customurl" is a url on which you want to redirect after Logged In.

If you want complete solution for custom url redirection different customer groups after logged In, Logged Out and Registration. Custom Redirection extension can help you. Click on link to get extension. http://www.magentocommerce.com/magento-connect/custom-redirection.html

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.