开发者问题收集

通过 javascript 更改 Shopify 货币

2021-10-13
1613

我正在寻找一种基于 URL 更改货币的解决方案。

对于 puroxx.net,我想将 GBP 设置为货币 对于 nl.puroxx.net,我想将 EUR 设置为货币。

这是我目前得到的结果:

if(window.location.host == 'nl.puroxx.net'){
    $("#localization_form").val('NL');
    $("#localization_form").change();
} 
else {
    $("#localization_form").val('GB');
    $("#localization_form").change();
}

并且在网站页脚中我添加了:

      {% form 'localization' %}
        <select name="country_code">
          {% for country in localization.available_countries %}
          <option value="{{ country.iso_code }}"
                  {%- if country == localization.country %} selected="selected"{% endif %}>
            {{ country.name }} ({{ country.iso_code }}) - {{ country.currency.symbol }}
          </option>
          {% endfor %}
        </select>
      {% endform %}

还添加了:

  $('.shopify-localization-form select').on('change', function() {
  $(this)
    .parents('form')
    .submit();
});

我遗漏了什么? 谢谢!

2个回答

尝试更新 JS 代码,如下所示:

if (window.location.host == 'nl.puroxx.net') {
    if ($('#localization_form [name=country_code]').val() != 'NL') {
        $("#localization_form [name=country_code]").val('NL');
        $("#localization_form").submit();
    }
} else {
    if ($('#localization_form [name=country_code]').val() != 'GB') {
        $("#localization_form [name=country_code]").val('GB');
        $("#localization_form").submit();
    }
}
$('#localization_form [name=country_code]').on('change', function() {
  $('#localization_form').submit();
});

尝试优化代码。参考: https://shopify.dev/themes/internationalization/multiple-currencies-languages#javascript-submission-of-the-localization-form

user9371102
2021-10-13

这是工作代码:

 $('.shopify-localization-form select').on('change', function() {
  $(this)
    .parents('form')
    .submit();
});



if(window.location.host == 'nl.puroxx.net'){ 
if($('#localization_form [name=country_code]').val() != 'NL') { 
  $("#localization_form [name=country_code]").val('NL'); 
$("#localization_form").submit(); 
}
} 
else { 
if($('#localization_form [name=country_code]').val() != 'GB') { 
  $("#localization_form [name=country_code]").val('GB'); 
$("#localization_form").submit(); 
}
}
Thomas Danninger
2021-10-13