如何在根索引文件中获取 magento 客户会话值以切换商店视图
How to get magento customer session values in root index file for switching store view
我在 magento 1.9.2 下为我的 magento 网站运行创建了 2 个商店视图,我需要为两个网站使用相同的域名。当客户登录时,我需要将商店视图更改为第二个。我需要检查根目录中 index.php 文件中的客户会话。但是当我按照以下更改 index.php 时,我最终得到一个错误
$store_id = 1;
//Check customer session here
if(Mage::getSingleton('customer/session')->isLoggedIn()){ // Error occurs here
$customer = Mage::getModel('customer/session')->getCustomer();
$customerId = $customer->getId();
// $storeid get logged in customer storeid may from customer custom attribute value added on admin edit form
// $store_id value get changed here
}
Mage::app()->setCurrentStore($store_id);
umask(0);
$mageRunCode = isset($_SERVER['MAGE_RUN_CODE']) ? $_SERVER['MAGE_RUN_CODE'] : '';
$mageRunType = isset($_SERVER['MAGE_RUN_TYPE']) ? $_SERVER['MAGE_RUN_TYPE'] : 'store';
if($store_id == 2)
$mageRunCode = 'mysecondshop';
Mage::run($mageRunCode, $mageRunType);
我查问题的时候发现在Mage::run之前是不能使用Mage::getSingleton的,但是对于我现在切换店铺的情况,需要在mage之前先登录用户详情运行。有什么办法可以达到我的要求吗?
在 magento 中,除非初始化 Mage::run,否则无法检查会话数据。我们需要做一些类似cookie的临时存储来实现这一点。希望下面的步骤能让它起作用。
创建一个带有观察者的自定义模块,用于保存、清除和检查 cookie。您的 config.xml 文件如下
<!--------------------------------------------------------------------->
<customer_login>
<observers>
<Company_Modulename_model_customer_login>
<class>Company_Modulename_Model_Observer</class>
<method>afterCustomerLoggedIn</method>
</Company_Modulename_model_customer_login>
</observers>
</customer_login>
<customer_logout>
<observers>
<Company_Modulename_model_customer_logout>
<class>Company_Modulename_Model_Observer</class>
<method>afterCustomerLoggedOut</method>
</Company_Modulename_model_customer_logout>
</observers>
</customer_logout>
<controller_action_layout_load_before>
<observers>
<Company_Modulename_model_layoutload_observer>
<class>Company_Modulename_Model_Observer</class>
<method>beforeLoadLayout</method>
</Company_Modulename_model_layoutload_observer>
</observers>
</controller_action_layout_load_before>
<!--------------------------------------------------------------------->
观察者实现如下
public function afterCustomerLoggedIn(Varien_Event_Observer $observer)
{
$customer = $observer->getCustomer();
$cid = $customer->getid();
$webId = $customer->getWebsiteId();
Mage::getModel('core/cookie')->set('_storeuid',$cid, 60*60*24*1); // Create cookie
Mage::getModel('core/cookie')->set('_storeid',$webId, 60*60*24*1); // Create cookie
}
public function afterCustomerLoggedOut(Varien_Event_Observer $observer)
{
$customer = $observer->getCustomer();
$cid = $customer->getid();
$webId = $customer->getWebsiteId();
Mage::getModel('core/cookie')->set('_storeuid','0', 1); // Clear cookie
Mage::getModel('core/cookie')->set('_storeid','0', 1); // Clear cookie
}
渲染页面之前
public function beforeLoadLayout(Varien_Event_Observer $observer)
{
$cookieValue = Mage::getModel('core/cookie')->get('_storeuid');
$baseurl = Mage::getBaseUrl();
if(Mage::getSingleton('customer/session')->isLoggedIn()){ // For more security , if someone change the cookie value
$customer = Mage::getModel('customer/session')->getCustomer();
$webId = $customer->getWebsiteId();
$cid = $customer->getId();
if( $cookieValue != $cid ){
Mage::getModel('core/cookie')->set('_storeuid',$cid, 60*60*24*1);
Mage::getModel('core/cookie')->set('_storeid',$webId, 60*60*24*1);
header("Location: ".$baseurl); exit;
}
}else{
if( (isset($cookieValue)) && ($cookieValue > 0) ){ // Clear cookie if session is cleared
Mage::getModel('core/cookie')->set('_storeuid','0', 1);
Mage::getModel('core/cookie')->set('_storeid','0', 1);
header("Location: ".$baseurl); exit;
}
}
}
最后在 index.php 中,您可以检查用于为已登录客户切换商店价值的 cookie
$store_id = 1;
if( (isset($_COOKIE['_storeur'])) && ($_COOKIE['_storeur'] > 0) ){
$store_id = $_COOKIE['_storeur'];
}
$mageRunCode = isset($_SERVER['MAGE_RUN_CODE']) ? $_SERVER['MAGE_RUN_CODE'] : '';
$mageRunType = isset($_SERVER['MAGE_RUN_TYPE']) ? $_SERVER['MAGE_RUN_TYPE'] : 'store';
if($store_id == 2 ) $mageRunCode = 'store2';
if($store_id == 3 ) $mageRunCode = 'store3';
//....................
Mage::run($mageRunCode, $mageRunType);
我在 magento 1.9.2 下为我的 magento 网站运行创建了 2 个商店视图,我需要为两个网站使用相同的域名。当客户登录时,我需要将商店视图更改为第二个。我需要检查根目录中 index.php 文件中的客户会话。但是当我按照以下更改 index.php 时,我最终得到一个错误
$store_id = 1;
//Check customer session here
if(Mage::getSingleton('customer/session')->isLoggedIn()){ // Error occurs here
$customer = Mage::getModel('customer/session')->getCustomer();
$customerId = $customer->getId();
// $storeid get logged in customer storeid may from customer custom attribute value added on admin edit form
// $store_id value get changed here
}
Mage::app()->setCurrentStore($store_id);
umask(0);
$mageRunCode = isset($_SERVER['MAGE_RUN_CODE']) ? $_SERVER['MAGE_RUN_CODE'] : '';
$mageRunType = isset($_SERVER['MAGE_RUN_TYPE']) ? $_SERVER['MAGE_RUN_TYPE'] : 'store';
if($store_id == 2)
$mageRunCode = 'mysecondshop';
Mage::run($mageRunCode, $mageRunType);
我查问题的时候发现在Mage::run之前是不能使用Mage::getSingleton的,但是对于我现在切换店铺的情况,需要在mage之前先登录用户详情运行。有什么办法可以达到我的要求吗?
在 magento 中,除非初始化 Mage::run,否则无法检查会话数据。我们需要做一些类似cookie的临时存储来实现这一点。希望下面的步骤能让它起作用。
创建一个带有观察者的自定义模块,用于保存、清除和检查 cookie。您的 config.xml 文件如下
<!--------------------------------------------------------------------->
<customer_login>
<observers>
<Company_Modulename_model_customer_login>
<class>Company_Modulename_Model_Observer</class>
<method>afterCustomerLoggedIn</method>
</Company_Modulename_model_customer_login>
</observers>
</customer_login>
<customer_logout>
<observers>
<Company_Modulename_model_customer_logout>
<class>Company_Modulename_Model_Observer</class>
<method>afterCustomerLoggedOut</method>
</Company_Modulename_model_customer_logout>
</observers>
</customer_logout>
<controller_action_layout_load_before>
<observers>
<Company_Modulename_model_layoutload_observer>
<class>Company_Modulename_Model_Observer</class>
<method>beforeLoadLayout</method>
</Company_Modulename_model_layoutload_observer>
</observers>
</controller_action_layout_load_before>
<!--------------------------------------------------------------------->
观察者实现如下
public function afterCustomerLoggedIn(Varien_Event_Observer $observer)
{
$customer = $observer->getCustomer();
$cid = $customer->getid();
$webId = $customer->getWebsiteId();
Mage::getModel('core/cookie')->set('_storeuid',$cid, 60*60*24*1); // Create cookie
Mage::getModel('core/cookie')->set('_storeid',$webId, 60*60*24*1); // Create cookie
}
public function afterCustomerLoggedOut(Varien_Event_Observer $observer)
{
$customer = $observer->getCustomer();
$cid = $customer->getid();
$webId = $customer->getWebsiteId();
Mage::getModel('core/cookie')->set('_storeuid','0', 1); // Clear cookie
Mage::getModel('core/cookie')->set('_storeid','0', 1); // Clear cookie
}
渲染页面之前
public function beforeLoadLayout(Varien_Event_Observer $observer)
{
$cookieValue = Mage::getModel('core/cookie')->get('_storeuid');
$baseurl = Mage::getBaseUrl();
if(Mage::getSingleton('customer/session')->isLoggedIn()){ // For more security , if someone change the cookie value
$customer = Mage::getModel('customer/session')->getCustomer();
$webId = $customer->getWebsiteId();
$cid = $customer->getId();
if( $cookieValue != $cid ){
Mage::getModel('core/cookie')->set('_storeuid',$cid, 60*60*24*1);
Mage::getModel('core/cookie')->set('_storeid',$webId, 60*60*24*1);
header("Location: ".$baseurl); exit;
}
}else{
if( (isset($cookieValue)) && ($cookieValue > 0) ){ // Clear cookie if session is cleared
Mage::getModel('core/cookie')->set('_storeuid','0', 1);
Mage::getModel('core/cookie')->set('_storeid','0', 1);
header("Location: ".$baseurl); exit;
}
}
}
最后在 index.php 中,您可以检查用于为已登录客户切换商店价值的 cookie
$store_id = 1;
if( (isset($_COOKIE['_storeur'])) && ($_COOKIE['_storeur'] > 0) ){
$store_id = $_COOKIE['_storeur'];
}
$mageRunCode = isset($_SERVER['MAGE_RUN_CODE']) ? $_SERVER['MAGE_RUN_CODE'] : '';
$mageRunType = isset($_SERVER['MAGE_RUN_TYPE']) ? $_SERVER['MAGE_RUN_TYPE'] : 'store';
if($store_id == 2 ) $mageRunCode = 'store2';
if($store_id == 3 ) $mageRunCode = 'store3';
//....................
Mage::run($mageRunCode, $mageRunType);