登录并将客户重定向到他的网站


10

我想在一个mutistore-multisite多站点Magento中,强迫用户登录他们注册的同一网站。他们可以在任何网站中使用任何登录表单,但是该表单应检查其凭据并将其重定向到正确的网站。

我试图检查客户的网站,并强迫他登录。不过效果不是很好。用户登录到当前网站,而不是他注册的网站。

在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;
}

有任何想法吗?


如果您从一个站点登录,然后自动登录我们的站点?
阿米特·贝拉

我不确定是否理解您的问题。用户应登录并重定向到他注册的网站。并非两个网站
-zekia

假设,客户在网站A上注册。客户尝试从网站B登录,然后客户应在网站A上使用Qutologin重定向到网站A。对?
阿米特·贝拉

是的,没错
-zekia

Answers:


10

首先,您需要对设置进行一些更改。

在多个网站之间共享客户帐户

您应该在此处配置此功能:System -> Configuration -> Customer Configuration -> Share Customer Accounts

将此设置设置为Global,共享每个客户到所有网站

在此处输入图片说明

在网站之间共享登录

在不同网站上的商店之间切换时保持会话,请在系统>配置>常规> 网站中启用“在前端使用SID” :

在此处输入图片说明

强制用户重定向到他们注册的相同网站

当我们尝试从另一个网站登录时,强制客户登录到他们注册的相同网站。

采用 customer_login

定义事件到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> 

观察者类:

<?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;
    }

}

注意:

我已经在我的MAGENTO DEMO STORE网站上测试了此代码。

这两个网站使用网站概念从同一magento实例运行。

在此处输入图片说明


1
这是处理此问题的正确方法。但是,通过这种方法,网站A中的客户可以在网站B中下订单。这可能是项目规范的限制。
弗兰克·卡尼尔

是的,这取决于项目规范
阿米特·贝拉

是否可以防止客户从其他网站订购。这是零售/批发多店配置,因此不应允许零售客户登录批发网站
zekia

是的,这有可能
阿米特·贝拉

我有什么问题吗?您如何定义客户是零售商还是批发商?
阿米特·贝拉

1

您可以根据需要重写以下方法

改写下课

Mage_Customer_Model_Session setCustomerAsLoggedIn 方法

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

您是说我应该将我发布的代码放置在setCustomerAsLoggedIn()中,而不是login()中吗?请发布更详细的答案。
zekia

1

使用该customer_login事件可避免更改/重写/覆盖核心文件。

在你的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>

您的观察者班级(/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); 
    }
}

请注意,您必须将不同的客户组分配给具有不同商店视图的客户。

您还可以分配客户服装,并在​​注册过程中通过注册表单中的隐藏字段将其设置为,而不是分配客户组。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.