Last active
January 25, 2018 10:52
-
-
Save rgoytacaz/5ada2a574564da95ed310b964ac67961 to your computer and use it in GitHub Desktop.
Disables Cash on Delivery payment types, for carriers that do not accept cash on delivery.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(){ | |
//Carriers that accepts Cash on Delivery | |
var carriesCashOnDelivery = [ | |
'Delivery' //Carrier name | |
]; | |
var paymentGroupCashOnDelivery = '#payment-group-custom201PaymentGroupPaymentGroup'; //Cash On Delivery Payment Group | |
var defaultPaymentGroup = '#payment-group-creditCardPaymentGroup'; //Default Payment Group | |
function acceptsCashOnDelivery(orderForm){ | |
if(orderForm&&orderForm.shippingData&&orderForm.shippingData.logisticsInfo&&orderForm.shippingData.logisticsInfo[0]&&orderForm.shippingData.logisticsInfo[0].selectedSla) | |
return carriesCashOnDelivery.includes(vtexjs.checkout.orderForm.shippingData.logisticsInfo[0].selectedSla); | |
else | |
return true; | |
} | |
//Displays Cash On Delivery or | |
//Hides and click on the default payment method, just once. | |
function toggleCashOnDelivery(boolean){ | |
if(boolean){ | |
$(paymentGroupCashOnDelivery).show(); | |
}else{ | |
$(paymentGroupCashOnDelivery).hide(); | |
if(('#'+$('.payment-group-item.active').attr('id')) == paymentGroupCashOnDelivery) | |
$(defaultPaymentGroup)[0] ? $(defaultPaymentGroup).click() : null; | |
} | |
} | |
function cashOnDelivery(){ | |
if(vtexjs&&vtexjs.checkout&&vtexjs.checkout.orderForm){ | |
toggleCashOnDelivery(acceptsCashOnDelivery(vtexjs.checkout.orderForm)); | |
} | |
} | |
//Executes when updated | |
$(window).on('orderFormUpdated.vtex', function(ev, orderForm) { | |
cashOnDelivery(); | |
}); | |
//Executes right away | |
cashOnDelivery(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment