【问题标题】:Magento: Switch between websites using Mage::run($mageRunCode, $mageRunType)Magento:使用 Mage::run($mageRunCode, $mageRunType) 在网站之间切换
【发布时间】:2014-02-11 00:26:07
【问题描述】:

我目前在index.php 中使用 GeoIP 将客户发送到两个不同的 Magento 网站(完全相同的商店视图,除了一个是加元,一个是美元)。它工作得很好。我现在要做的是允许客户通过下拉菜单切换网站。我正在使用“网站切换器”的代码

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="location.href=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->getDefaultStore()->getCurrentUrl() ?>"<?php echo $_selected ?>><?php echo $this->htmlEscape($website->getName()) ?></option>
<?php endforeach; ?>
</select>
</div>
<?php endif; ?>

除了这只会更改当前页面的网站 - 一旦您单击另一个链接,它就会恢复到 index.php 选择的网站。

我希望下拉菜单实际更改网站(因此,如果此人在 CAD 网站上购物车中有物品,它实际上会清除购物车,然后向他们显示一个空的美元购物车和美元网站中的美元定价)。

Magento 如何设置网站?它看起来不像是在设置 cookie,我猜这是 Mage::run($mageRunCode,$mageRunType) 在更深层次上完成的?我也不希望启用“将商店代码添加到 URL”,即使这提供了一种解决方法来拥有此功能,因为它对于 URL 和 SEO 来说非常难看。

感谢您的帮助!

【问题讨论】:

    标签: php magento


    【解决方案1】:

    因此,当您在商店切换器上更改选择时,我通过向网站添加 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-&gt;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 由于某种原因变得未定义,然后它只是加载默认存储。

    现在我默认有地理位置,但如果客户想要更改商店,那么他们可以从下拉菜单中进行更改,它将覆盖地理位置。希望它可以帮助某人!

    【讨论】:

      【解决方案2】:

      System -> Configuration -> Web -> URL Options 第一项是“Add Shop Code to URLs”。这将设置为“否”。

      在您的 template -&gt; header.phtml 中添加以下代码

      <?php echo $this->getChildHtml('store_language') ?>
      

      【讨论】:

      • 很抱歉我没看懂你的回复....我已经说过我不想开启 Add Shop Code to Urls 并且商店在相同的语言,所以我认为语言切换器不会做任何事情?
      猜你喜欢
      • 1970-01-01
      • 2012-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-04
      • 1970-01-01
      • 2021-02-06
      • 2014-02-04
      相关资源
      最近更新 更多