因此,当您在商店切换器上更改选择时,我通过向网站添加 cookie 来解决这个问题。对于其他想知道如何做到这一点的人:
您的网站切换器的代码(存储在app/design/frontend/YOUR THEME/template/page/switch/stores.phtml):
if (count($websites) > 1): ?>
<div class="website-switcher">
<label for="select-website"><?php echo $this->__('Select Store:') ?></label>
<select id="select-website" title="<?php echo $this->__('Select Store') ?>" onchange="setCookie('redirect',this.value)">
<?php foreach ($websites as $website): // print out each website name and code as a dropdown box item ?>
<?php $_selected = $website->getCode() == Mage::app()->getWebsite()->getCode() ? ' selected="selected"' : '' ?>
<option value="<?php echo $website->getCode() ?>"<?php echo $_selected ?>><?php echo $this->htmlEscape($website->getName()) ?></option>
<?php endforeach; ?>
</select>
您会注意到选择框有一个触发 js 代码的 onchange 事件。它提供 cookie 的名称(“重定向”)和一个值。该值只是对$website->getCode()的调用所代表的商店代码
接下来在您的 js 文件夹中为 cookie 创建创建一个自定义 javascript 文件。使用 local.xml 操作方法将其添加到文档的头部(如果您不知道如何使用,请在此搜索)。在js文件中放:
function setCookie(name, value, expires, path, domain, secure)
{
var cookieStr = name + "=" + escape(value) + "; ";
if (expires) {
var expiresDate = new Date(new Date().getTime() + expires * 24 * 60 * 60 * 1000);
cookieStr += "expires=" + expiresDate.toGMTString() + "; ";
}
cookieStr += "path=/" + "; ";
if (domain) {
cookieStr += "domain=" + domain + "; ";
}
if (secure) {
cookieStr += "secure; ";
}
document.cookie = cookieStr;
document.location.reload();
}
setCookie 允许您创建一个 cookie,输入名称、值等值。在这种情况下,我将其硬编码为 path="/",并且 expires 将设置为 Session。
然后在您的index.php 中输入此代码:
if (isset($_COOKIE['redirect'])) {
if ($_COOKIE['redirect'] == 'base') {
$mageRunCode = isset($_SERVER['MAGE_RUN_CODE']) ? $_SERVER['MAGE_RUN_CODE'] : '';
$mageRunType = isset($_SERVER['MAGE_RUN_TYPE']) ? $_SERVER['MAGE_RUN_TYPE'] : 'store';
Mage::run($mageRunCode, $mageRunType);
}
elseif ($_COOKIE['redirect'] == 'ca') {
$mageRunCode = isset($_SERVER['MAGE_RUN_CODE']) ? $_SERVER['MAGE_RUN_CODE'] : 'ca';
$mageRunType = isset($_SERVER['MAGE_RUN_TYPE']) ? $_SERVER['MAGE_RUN_TYPE'] : 'store';
Mage::run($mageRunCode, $mageRunType);
}
else {
$mageRunCode = isset($_SERVER['MAGE_RUN_CODE']) ? $_SERVER['MAGE_RUN_CODE'] : '';
$mageRunType = isset($_SERVER['MAGE_RUN_TYPE']) ? $_SERVER['MAGE_RUN_TYPE'] : 'store';
Mage::run($mageRunCode, $mageRunType);
}
}
此代码检查是否设置了名为“重定向”的 cookie。如果存在并且 cookie 的值为“base”(默认存储),则加载默认存储。如果是“ca”,请加载加拿大商店。然后我只是在末尾加上一个“catch”术语,以防 cookie 由于某种原因变得未定义,然后它只是加载默认存储。
现在我默认有地理位置,但如果客户想要更改商店,那么他们可以从下拉菜单中进行更改,它将覆盖地理位置。希望它可以帮助某人!