rit*_*ita 5 session shopping-cart magento
我正在建立一个Magento网站,我有点困境.我有两个网站,每个网站都设置了一个商店,以便为每个网站启用多种货币结算.因此,通过一个常见的magento安装管理的两个站点(在两个不同的域上)之间的唯一区别是货币显示和结账货币.到目前为止,这一切都很好.但是,我正在尝试分享网站之间的结帐会话,以便在网站之间切换时购物车保持不变.我设法将正确的会话ID添加到切换URL,以便每个站点知道我们正在寻找什么会话.然而,实际显示购物车的方法似乎不适用于网站独立 - 例如在
Mage_Checkout_Model_Session
_getQuoteIdKey() - >使用当前网站ID检查会话中的引用ID.
我无法弄清楚是什么/如何覆盖这个功能,以便每个网站共享完全相同的购物车数据!
我的$ _SESSION ['checkout']变量在每个网站上显示相同但是包含此网站ID的数据对购物车没有用处:'quote_id_4'=> string'13'(长度= 2)
有任何想法是否可行?
rit*_*ita 10
在我搜索到这个问题的答案并偶然发现自2009年以来没有明确解决方案的问题后,我终于不得不自己深入研究代码 - 瞧,我有一个有效的解决方案.这里有一个详细指南,供任何想要设置Magento的人使用
这种组合的主要问题是,使用默认的Magento结构,你只能做一个或另一个,而不是两个组合.
因此,首先让Magento为多种货币设置.
在.htaccess文件中,将其添加到顶部(替换您在设置网站时输入的相应网站域和代码(此处http://website-us.local with base_us,http://website-uk.local包含代码) base_uk)
SetEnvIf主机网站-us.local MAGE_RUN_CODE = base_us SetEnvIf主机网站-us.local MAGE_RUN_TYPE =网站SetEnvIf主机^ website-us.local MAGE_RUN_CODE = base_us SetEnvIf主机^ website-us.local MAGE_RUN_TYPE =网站
SetEnvIf主机网站-uk.local MAGE_RUN_CODE = base_uk SetEnvIf主机网站-uk.local MAGE_RUN_TYPE =网站SetEnvIf主机^ website-uk.local MAGE_RUN_CODE = base_uk SetEnvIf主机^ website-uk.local MAGE_RUN_TYPE =网站
在Mage/Core/Model/Store中覆盖convertPrice方法并更改convertPrice方法 - 这将确保价格始终以正确的转换和正确的货币符号显示.
/**
* Convert price from default currency to current currency
*
* @param double $price
* @param boolean $format Format price to currency format
* @param boolean $includeContainer Enclose into <span class="price"><span>
* @return double
*/
public function convertPrice($price, $format = false, $includeContainer = true)
{
$categories = Mage::getModel('catalog/category')->getCollection();
$categ_ids=$categories->getAllIds();
$baseCurrencyCode = Mage::app()->getBaseCurrencyCode();
$allowedCurrencies = Mage::getModel('directory/currency')->getConfigAllowCurrencies();
$currencyRates = Mage::getModel('directory/currency')->getCurrencyRates($baseCurrencyCode,array_values($allowedCurrencies));
if ($this->getCurrentCurrency() && $this->getBaseCurrency()) {
$value = $this->getBaseCurrency()->convert($price, $this->getCurrentCurrency());
} else {
$value = $price;
}
if($this->getCurrentCurrencyCode() != $baseCurrencyCode)
{
$value = $price * $currencyRates[$this->getCurrentCurrencyCode()];
}
if ($this->getCurrentCurrency() && $format) {
$value = $this->formatPrice($value, $includeContainer);
}
return $value;
}
Run Code Online (Sandbox Code Playgroud)
}
但我们当然也想在我们刚刚设置的网站上分享用户数据,购物车和登录信息.
在默认配置范围设置系统 - 配置 - 客户配置 - 帐户共享选项 - 将客户帐户共享到全局
覆盖magento/app/code/core/Mage/Checkout/Model/Session.php并替换此方法:
protected function _getQuoteIdKey(){return' quote_id'; //返回'quote_id_'.$网站[1]; }
覆盖magento/app/code/core/Mage/Sales/Model/Quote.php并将方法getSharedStoreIds更改为:
public function getSharedStoreIds(){$ ids = $ this - > _ getData('shared_store_ids'); if(is_null($ ids)||!is_array($ ids)){$ arrStoreIds = array(); foreach(Mage :: getModel('core/website') - > getCollection()as $ website){$ arrStoreIds = array_merge($ arrStoreIds,$ website-> getStoreIds()); } return $ arrStoreIds;/*if($ website = $ this-> getWebsite()){return $ website-> getStoreIds(); } var_dump($ this-> getStore() - > getWebsite() - > getStoreIds()); exit(); return $ this-> getStore() - > getWebsite() - > getStoreIds();*/}返回$ ids; }
覆盖magento/app/code/core/Mage/Customers/Model/Customer.php并再次将方法getSharedWebsiteIds()更改为:
public function getSharedWebsiteIds(){$ ids = $ this - > _ getData('shared_website_ids'); if($ ids === null){$ ids = array(); if((bool)$ this-> getSharingConfig() - > isWebsiteScope()){$ ids [] = $ this-> getWebsiteId(); } else {foreach(Mage :: app() - > getWebsites()as $ website){$ ids [] = $ website-> getId(); $ this-> setData('shared_website_ids',$ ids); } return $ ids; }
如果您使用wishlist选项,您应该对magento/app/code/core/Mage/Wishlist/Model/Wishlist.php中的方法执行相同操作并更改getSharedWebsiteIds,以便它不仅从当前网站加载商店ID,而且从所有网站加载他们
现在我们还必须在前端存储上实现货币(网站)切换,并在中间传递正确的会话ID,以便magento知道要查找的商店.我在这里模仿货币开关并添加了以下下拉列表
Magento的/应用程序/设计/前端/默认/ yourtheme /模板/目录/ currency.phtml
这将加载所有网站并将当前会话ID应用为查询字符串,因此magento在任何域上都知道要使用哪个会话.
<?php
/**
* Currency switcher
*
* @see Mage_Directory_Block_Currency
*/
?>
<div class="top-currency">
<?php
$websites = Mage::getModel('core/website')->getCollection();
$this_session_id = Mage::getSingleton('core/session', array('name' => 'frontend'))->getSessionId();
?>
<select id="website-changer" onChange="document.location=this.options[selectedIndex].value">
<?php
foreach($websites as $website):
$default_store = $website->getDefaultStore();
$website_currency = $default_store->getBaseCurrency()->getCurrencyCode();
$url_obj = new Mage_Core_Model_Url();
$default_store_path = $url_obj->getBaseUrl(array('_store'=> $default_store->getCode()));
$default_store_path .= Mage::getSingleton('core/url')->escape(ltrim(Mage::app()->getRequest()->getRequestString(), '/'));
$default_store_path = explode('?', $default_store_path);
$default_store_path = $default_store_path[0] . '?SID=' . $this_session_id;
?>
<option <? if(strstr($default_store_path,Mage::getBaseUrl())):?>selected="selected"<?endif; ?> value="<?=$default_store_path ?>">
<?=$website_currency?>
</option>
<?endforeach;?>
</select>
</div>
Run Code Online (Sandbox Code Playgroud)
此查询字符串仅在您第一次切换时应用,但magento将记住存储在cookie中的会话ID之后.
的magento /应用程序/代码/核心/法师/核心/型号/会话/摘要/ Varien.php
和
替换这个
// potential custom logic for session id (ex. switching between hosts)
$this->setSessionId();
Run Code Online (Sandbox Code Playgroud)
同
// potential custom logic for session id (ex. switching between hosts)
/* Amend to ensure shopping carts are shared between websites */
if (isset($_COOKIE['lastsid']))
{
session_decode(file_get_contents(Mage::getBaseDir('session').'/sess_'.$_COOKIE['lastsid']));
setcookie ('lastsid', '', time() - 3600);
}
if (isset($_GET['SID']))
{
$this->setSessionId($_GET['SID']);
session_decode(file_get_contents(Mage::getBaseDir('session') . '/sess_' . $_GET['SID']));
setcookie('lastsid', $_GET['SID']);
$_COOKIE['lastsid'] = $_GET['SID'];
}
else
{
$this->setSessionId();
}
/* Amend end */
Run Code Online (Sandbox Code Playgroud)
现在应该显示多种货币,在多个网站上以多种货币收费,并在此共享登录和购物车之上.这个解决方案是我在互联网上找到的知识的集合,结合了我自己想出的一些零碎的东西,非常感谢任何给出建议的人!
归档时间: |
|
查看次数: |
9797 次 |
最近记录: |