我创建了一个包含日语和英语商店的 Magento 网站。对于这两家商店,我有几乎相同的产品。我只是添加了其他属性来在必要时添加英文文本,例如名称。例如,在我的日本商店中,产品名称是“靴”,那么在英语商店中,产品名称是“Shoes”。鉴于此,我必须调整我的网址和面包屑以适应两种语言。
无论如何,我为我的英语商店添加了一个货币选择器,效果非常好。我遇到的唯一问题是它完全重新加载我的页面,并且我在网址和面包屑中所做的更改消失并恢复为默认值。我的网址和面包屑在英文商店中应如下所示:
url: http://mywebsite.com/mycategory/shoes.html?cat=mycategory&prod=shoes
breadcrumbs: Home > My Category > Shoes
但是每当我尝试更改货币时,它都会重新加载我的页面,最后我会得到以下网址和面包屑:
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 ?>' + '¤cy=' + 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+'' 但这根本不起作用。我的网址没有保留。所以我的问题是,如何更改货币,同时保留我为网址和面包屑创建的更改?
更新: 根据 Natalie 的建议,我已将 $this->getSwitchCurrencyUrl($_code) 更改为简单的 $_code,并对currency.phtml 进行了更多更改以满足我的需求。我现在可以保留我的网址参数并更改货币。现在的问题是,即使我能够以编程方式设置货币,选择的选项和货币也不会立即更改。例如,如果我当前的货币是美元,我尝试更改为日元,会发生什么情况,它仍然是美元,然后第二次,我选择欧元,使用日元货币,然后下一个使用的货币将是欧元。基本上,我的代码似乎获取了之前选择的货币而不是当前的货币。为什么会发生这种情况以及如何解决这个问题?
最佳答案
您是否使用 Magentos 默认语言/商店切换器?如果是的话,您是否为日语和英语商店 View 创建了不同的类别名称?因为这不应该是一个问题,magento 会将语言/商店选择保存在 cookie 中,然后显示连接到该商店的类别。
编辑:我将在此处添加此内容,因为评论太短:
抱歉。我错过了您在将参数添加到原始值时遇到了麻烦。当我想通过单击同一按钮来更改商店 View 和货币时,您可以执行我所做的操作。
您可以自己手动设置货币。如果您将货币下拉列表的值更改为仅包含货币代码,如下所示:
<option value="<?php echo $_code?>" ...
并且不要转到 this.value
而是在更改时转到以下网址
onchange="setLocation('?cat=mycategory&prod=shoes¤cy='+this.value)"
在 header.phtml 文件的顶部添加以下内容以获取货币参数并设置新货币。
if($currency = Mage::app()->getRequest()->getParam('currency')){
Mage::app()->getStore()->setCurrentCurrencyCode($currency);
}
我还没有测试过这个确切的代码,所以我不能保证它能完美工作。但我自己也做了一个效果很好的版本。
关于javascript - 更改 URL 并保留下拉 onChange 事件中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20390761/