10

In a mutistore-multiwebsite Magento, I'd like to force users to login to the same website they have registered. They might use any login form, in any website, but the form should check their credentials and redirect them to the correct website.

I tried to check the customer's website, and force him to login. It's not working quite good though. The user gets logged in to the current website, not the one he's registered.

In app/code/local/mage/Customer/Session.php

public function login($username, $password)
{
    /**************************************************/
    $customer = Mage::getModel("customer/customer");
    $customer_website = null;


    foreach (Mage::app()->getWebsites() as $website) {
        $customer->setWebsiteId($website->getId());
        $customer->loadByEmail($username);
        //check if user exists
        if($customer->getName()){
            $customer_website = $website->getId();
        }
    }
    /*************************************************/
    $customer = Mage::getModel('customer/customer')->setWebsiteId($customer_website);

    if ($customer->authenticate($username, $password)) {
        $this->setCustomerAsLoggedIn($customer);
        return true;
    }
    return false;
}

Any ideas?

  • If you login from one site then automatically login at our site? – Amit Bera Mar 24 '17 at 8:14
  • I'm not sure if understand your question. The user should be logged in and redirect to the website he's registered. Not both websites – zekia Mar 24 '17 at 8:59
  • Suppose, customer register at Website A. Customer Have try login from Website B,Then customer should redirect to WebsiteA with qutologin at websiteA. right? – Amit Bera Mar 24 '17 at 11:26
  • Yes, that's correct – zekia Mar 26 '17 at 21:46
10
+200

First,you need to do some changes in setting.

Share customer accounts between multiple Website

You should configure this feature here: System -> Configuration -> Customer Configuration -> Share Customer Accounts.

Make this setting to Global for sharing every customer to all websites.

enter image description here

Share login between websites

To keep the session when switching between stores on different website, enable "Use SID on Frontend" in System > Configuration > General > Web:

enter image description here

Force users to redirect to the same website they have registered

Forcefully customer login to the same website they have registered when we have tried to login from another website.

Use customer_login

Define event to config.xml

<?xml version="1.0"?>
<config>
  <modules>
    <Stackexchange_Magento165528>
      <version>1.0.0</version>
    </Stackexchange_Magento165528>
  </modules>
  <global>
    <models>
      <magento165528>
        <class>Stackexchange_Magento165528_Model</class>
      </magento165528>
    </models>
    <events>
      <customer_login> <!-- identifier of the event we want to catch -->
        <observers>
          <customer_login_handler> <!-- identifier of the event handler -->
            <type>singleton</type> <!-- class method call type; valid are model, object and singleton -->
            <class>magento165528/observer</class> <!-- observers class alias -->
            <method>redirectoSourceDomain</method>  <!-- observer's method to be called -->
            <args></args> <!-- additional arguments passed to observer -->
          </customer_login_handler>
        </observers>
      </customer_login>
    </events>
  </global>
</config> 

Observer class:

<?php
class Stackexchange_Magento165528_Model_Observer
{

    public function redirectoSourceDomain(Varien_Event_Observer $observer)
    {
        $_customer = $observer->getEvent()->getCustomer();
        /* 
        * Store of website from which website  Customer have registered
        */
        $_customer_resgister_store_id= $_customer->getStoreId();

        if($_customer_resgister_store_id != Mage::app()->getStore()->getStoreId()){
            $allStores=Mage::app()->getStores(); //get list of all stores,websites

            foreach ($allStores as $_eachStoreId => $val){
                $_storeId = Mage::app()->getStore($_eachStoreId)->getId();
                //get url using store id
                if($_customer_resgister_store_id  == $_eachStoreId ){
                $Websiteurl= Mage::app()->getStore($_storeId)->getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);
                $_redirecUrl =  $Websiteurl."customer/account/login?SID=".Mage::getModel("core/session")->getEncryptedSessionId(); 
                /* Force redirect to repective Website */
                Mage::app()->getFrontController()->getResponse()
                            ->setRedirect($_redirecUrl)
                            ->sendResponse();
                        exit;   
                }

            }

        }
        return;
    }

}

NOTE:

I HAVE TEST THIS CODE on MY MAGENTO DEMO STORE WEBSITES.

This two website is running from same magento instance using website concept .

enter image description here

  • 1
    This is a correct way to handle this problem. But with this method, a customer in Website A can place an order in Website B. It can be a restriction from the project specification. – Franck Garnier Mar 27 '17 at 11:16
  • right, it depends on project specification – Amit Bera Mar 27 '17 at 11:19
  • Is it possible to prevent customers from ordering from another website. This is a retail/wholesale multistore configuration, so retail customers should not be allowed to login to wholesale website – zekia Mar 30 '17 at 12:36
  • yes, it possiable – Amit Bera Mar 30 '17 at 18:00
  • I have few question?. how you define a customer is retailer or wholesaler? – Amit Bera Mar 30 '17 at 20:59
1

you can rewrite below method for your requirment

rewrite below class

Mage_Customer_Model_Session setCustomerAsLoggedIn method

public function setCustomerAsLoggedIn($customer)
{
    $this->setCustomer($customer);
    $this->renewSession();
    Mage::dispatchEvent('customer_login', array('customer'=>$customer));
    // check here customer website ID and redirect to their own registered website  
    return $this;
}
  • Do you mean that I should place the code i posted in setCustomerAsLoggedIn(), instead of login()? Post a more detailed answer please. – zekia Mar 24 '17 at 9:28
1

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;
        }
        $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);

        //add this if you want them to stay in that store even after logout
        Mage::getModel('core/cookie')->set('store', $storeCode); 
    }
}

Note that you will have to assign different customers groups to customers of different store views.

You can also assign customer attibute and set them at during registration via hidden field in registration form instead of assigning customer groups.

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.