本篇文章于2019年05月23日 ,作为magento的开发人员,我们可能会用的magento的url的获取。接下来我跟大家讲讲
如何在magento 中正确是使用获取url的方法。
在一些较大的Magento EE(企业版)设置中,我们有可能具有多个不同的store商店。商店之间为了公用购物车,这里就讲到了重要的知识是共享会话。
幸运的是,magento具有一些内置功能,能够将会话ID作为SID作为查询参数传递。
这与用户本地cookie一起使用,以确保购物车在商店之间是持久的。有几种方法可以解决它,但我发现下面的解决方案对我们来说非常好。
首先,我认为看一下这个页面是个好主意。下面的链接将介绍一些可以传递给”Mage::getUrl()”方法的参数。
欢迎你的回来。你看了上面的链接吗?如果你很难看懂。我将给你展示下列代码示例,看看下面。
你会看到我正在使用 Mage:getURL(); 我传递了一些参数。
- 我链接到的商店ID(所以我得到了正确的基本网址。)确保它用适当的商店ID替换$ YOUR_STORE_ID作为整数
- 我们的会话ID我从核心/会话单个实例中获取
- 最后我还将实际商店“slug”附加到网址上,以确保我们切换商店。
1 2 3 4 5 6 7 8 | // get our session $session_id = Mage::getSingleton("core/session")->getEncryptedSessionId(); // pass along a store id IF NEEDED echo Mage::getUrl( '/', array('_store' => $YOUR_STORE_ID, //make sure we append our session '_query' => array('SID' => $session_id ), // lastly add store to our url if needed '_store_to_url' => true ) ); |
接下来,我将讲述magento 1的session id的传递即使SID
首先我这里以用户登录后监听事件为例:
第一步:创建自己的事件的监听在配置文件里面配置如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | <?xml version="1.0"?> <config> <modules> <Sky8g_Magento110> <version>2.0.0</version> </Sky8g_Magento110> </modules> <global> <models> <magento110> <class>Sky8g_Magento110_Model</class> </magento110> </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>magento110/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.php类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | <?php class Sky8g_Magento110_Model_Observer { public function redirectoSourceDomain(Varien_Event_Observer $observer) { //获取的cutomer对象 $_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; } } |
希望对你有帮助,欢迎下次再来。
文章不错支持下一吧