Skip to content

Instantly share code, notes, and snippets.

@philcon93
Created September 12, 2019 04:57
Show Gist options
  • Save philcon93/da1b2d07c484f507e00fd695c80a7a9f to your computer and use it in GitHub Desktop.
Save philcon93/da1b2d07c484f507e00fd695c80a7a9f to your computer and use it in GitHub Desktop.
Hide payment options if bill country in EU
var nEU = {
// List of EU countries / their country code;
isInEu: function(){
var countries = ['ES', 'NO'];
var billCountry = $('#bill_country').val();
for (var i = 0; i < countries.length; i++) {
if(countries[i] == billCountry){
return true;
}
}
return false;
},
// Payment_desc_type to display for EU
nonEuPayments: function(){
return ['eway_securepanel', 'stripe'];
},
// Return array of specfic payment id's
nonEuPaymentsIds: function(){
var paymentOptions = nEU.nonEuPayments();
var cache = $.getCheckoutCache();
var payments = cache.payment.info;
var results = payments.reduce((accumulator, currentValue) => {
if (paymentOptions.includes(currentValue.type)) {
accumulator.push(currentValue.id);
}
return accumulator;
}, []);
return results;
},
// Hide specific payment option
hidePaymentOption: function(id, hide){
if(hide){
$(`#method${id}`).prop('checked', false);
$('#payment').val('');
$(`#collapse${id}`).collapse('hide');
$(`.checkout-payment[ref=${id}]`).hide();
}else{
$(`.checkout-payment[ref=${id}]`).show();
}
}
}
$('#bill_country').on('change', function(){
var euCountry = nEU.isInEu();
var paymentsToHide = nEU.nonEuPaymentsIds();
if(paymentsToHide.length > 0){
for (var i = 0; i < paymentsToHide.length; i++) {
nEU.hidePaymentOption(paymentsToHide[i], euCountry);
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment