更改URL并保留“更改”事件下拉列表中的值

Changing URL and retaining values on dropdown onChange event

本文关键字:更改 下拉列表 事件 URL 保留      更新时间:2024-03-30

我创建了一个有日语和英语商店的Magento网站。对于这两家商店,我有几乎相同的产品。我只是在必要的地方添加了额外的属性来添加英语文本,比如名字。例如,在我的日本商店里,产品的名称是"靴然后在英语商店里是"鞋子"。考虑到这一点,我必须对我的url和面包屑进行调整,以适应这两种语言。

不管怎样,我为我的英语商店添加了一个货币选择器,效果很好。我对它唯一的问题是,它完全重新加载了我的页面,我在url和面包屑中所做的更改消失了,并返回到默认值。我的网址和面包屑在英文商店应该是这样的:

url: http://mywebsite.com/mycategory/shoes.html?cat=mycategory&prod=shoes
breadcrumbs: Home > My Category > Shoes

但每当我试图更改货币时,它都会重新加载我的页面,我最终会得到这个url和面包屑:

url: http://mywebsite.com/mycategory/shoes.html
breadcrumbs: Home > My Category > 靴

我的货币下拉列表的代码如下所示:

<?php $_product = Mage::registry('current_product');
$root = Mage::app()->getStore()->getRootCategoryId();
//UPDATE:
$currency = Mage::app()->getRequest()->getParam('currency');
Mage::app()->getStore()->setCurrentCurrencyCode($currency);
//if I try to echo these two, it both returns the correct current currency
$cats = $_product->getCategoryCollection()
        ->addAttributeToSelect('name')
        ->addAttributeToFilter('parent_id', $root);     
foreach($cats as $_cat):
    $cat_name = $_cat->getName();
endforeach;
//UPDATE:
$productUrl = $_product->getProductUrl."?cat=".urlencode($cat_name)."&prodname=".urlencode($_product->getName_en());
if($this->getCurrencyCount() > 1): ?>
<label for="custom-currency-selector"><?php echo $this->__('Select Currency:') ?></label>
//UPDATE: changed window.location.href to setLocation
<select onchange="setLocation('<?php echo $productUrl ?>' + '&currency=' + this.value") name="custom-currency-selector" id="custom-currency-selector">
    <?php foreach ($this->getCurrencies() as $_code => $_name): ?>
  //UPDATE: option value
  //if I echo $this->getCurrentCurrencyCode(), its value is different from the current. It returns the previously selected currency
  <option value="<?php echo $_code; ?>"
        <?php if($_code == $this->getCurrentCurrencyCode()): ?>
            selected="SELECTED"
        <?php endif; ?>>
        <?php echo $_code ?>
    </option>
    <?php endforeach; ?>
</select>
<?php endif; ?>

我尝试将onchange事件值更改为window.location.href=this.value+'',但根本不起作用。我的网址没有保留。所以我的问题是,如何在保留我为url和面包屑创建的更改的同时更改货币?

更新:根据Natalie的建议,我将$this->getSwitchCurrencyUrl($_code)更改为简单的$_code,并对我的currency.phtml进行了更多更改以满足我的需求。我现在可以保留我的url参数并更改货币。现在的问题是,即使我可以通过编程设置货币,选择选项和货币也不会立即更改。例如,如果我的当前货币是美元,而我试图更改为日元,结果是它仍然是美元,然后第二次,我选择欧元,使用日元货币,然后下一个使用的货币将是欧元。基本上,我的代码似乎得到了以前选择的货币,而不是当前货币。为什么会发生这种情况,我该如何解决?

您是否使用Magentos默认语言/存储切换器?如果是,您是否为日语和英语商店视图创建了不同的类别名称?因为这不应该是一个问题,所以magento将在cookie中保存语言/商店选择,然后显示连接到该商店的类别。

编辑:我将在此处添加此内容,因为注释太短:

对不起。我错过了你在将params添加到原始值时遇到的麻烦。当我想在同一个按钮上更改商店视图和货币时,你可以做我做的事情。

您可以自己手动设置货币。如果您将货币下拉列表的值更改为仅包含如下货币代码:

<option value="<?php echo $_code?>" ...

在更改时,不要转到this.value,而是转到以下url

onchange="setLocation('?cat=mycategory&prod=shoes&currency='+this.value)"

在header.phtml文件的顶部添加以下内容以获取货币参数并设置新货币。

if($currency = Mage::app()->getRequest()->getParam('currency')){
    Mage::app()->getStore()->setCurrentCurrencyCode($currency);
}

我还没有测试过这个确切的代码,所以我不能保证它会完美工作。但我自己也做了一个版本,效果很好。