Home > Mobile >  Currency Change Shopify via java script
Currency Change Shopify via java script

Time:10-14

I am searching for a solution to change the Currency based on the url.

For puroxx.net I would like to set GBP as currency For nl.puroxx.net I would like to set EUR as currency.

This is what I got so far:

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

And in the footer of the site I added:

      {% 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 %}

Also added:

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

What am I missing? Thanks!

CodePudding user response:

Try to Update the JS Code as below:

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();
});

Try to optimize the code. Ref: https://shopify.dev/themes/internationalization/multiple-currencies-languages#javascript-submission-of-the-localization-form

CodePudding user response:

Here is the working code:

 $('.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(); 
}
}
  • Related