我安装了Magento,默认网站和多个特许经营权'网站。
我的问题是,当客户离开后返回网站并通过Magento的持久登录机制自动登录时,如何捕获事件?我想捕获该事件,加载客户,检查他们是否在正确的网站ID中,然后重定向他们,如果他们不是。
答案 0 :(得分:1)
为了使这项工作,您必须在/app/code/local/Extension/Module/etc/config.xml中注册一个事件,如下所示:
<customer_session_init>
<observers>
<sessioninit_handler>
<type>singleton</type>
<class>Extension_Module_Model_Observer</class>
<method>on_customer_session_init</method>
</sessioninit_handler>
</observers>
</customer_session_init>
然后在/app/code/local/Extension/Module/Model/Observer.php中创建一个Observer方法,如下所示:
/*
** on customer session init, checks for current website id and redirects if no-match
*/
public function on_customer_session_init(Varien_Event_Observer $observer){
$customer = $observer->customer_session->getCustomer();
$customer_website_id = $customer->getWebsiteId();
$current_website_id = Mage::app()->getWebsite()->getId();
if ($customer_website_id != $current_website_id){
$website = Mage::app()->getWebsite($customer_website_id);
$request = $this->_getRequest();
$response = $this->_getResponse();
$url = $website->getDefaultStore()->getBaseUrl().substr($request->getRequestString(), 1);
$response->setRedirect($url);
}
return $this;
}