Skip to content

Instantly share code, notes, and snippets.

@philcon93
Created April 7, 2017 04:44
Show Gist options
  • Save philcon93/e7426a3cca4741e23add5f0e53e413fd to your computer and use it in GitHub Desktop.
Save philcon93/e7426a3cca4741e23add5f0e53e413fd to your computer and use it in GitHub Desktop.
<div class="c-dropdown">
<form method="post" action="" class="currency_form">
<input type="hidden" name="ship_country">
<span>Change currency</span>
<span>
<select style="margin: 3px" name="dc_selected_currency">
<option value="AUD">AUD Australian Dollar</option>
<option value="NZD">NZD New Zealand Dollar</option>
</select>
</span>
</form>
</div>
$(document).ready(function(){
// For toggling dropdown
$('.c-title') .click(function(){
$('.currency-wrap').toggleClass('open');
});
// Shipping Country Set
var get_country_selected = localStorage.getItem("country_selected");
if(typeof(get_country_selected)!== "undefined" && typeof(get_country_selected)!== ""){
$('.currency_form input[name=ship_country]').val(get_country_selected);
}
// Currency Set
var currency = localStorage.getItem('cur') || "AUD";
changeCurrency(currency);
});
$(document.body).on('change','.currency-wrap select',function(){
// Event listener that triggers all the functions
$('.currency-wrap').removeClass('open');
changeCurrency($(this).val());
shippingCountryChange($(this).val());
$('.currency_form').submit();
});
function changeCurrency(currency) {
// Cosmetic flag image change
if (currency == 'AUD') {
$('.c-title').html("<img src='[%ntheme_asset%]img/aud-flag.png[%END ntheme_asset%]'/> $ AUD");
} else if(currency == 'NZD') {
$('.c-title').html("<img src='[%ntheme_asset%]img/nzd-flag.png[%END ntheme_asset%]'/> $ NZD");
}
}
function shippingCountryChange(country) {
// Sets shipping country in local storage
if (country == 'NZD'){
var country_selected = 'NZ';
}else{
var country_selected = 'AU';
}
$('.currency_form input[name=ship_country]').val(country_selected);
localStorage.setItem("country_selected",country_selected);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment