Skip to content

Instantly share code, notes, and snippets.

@aatronco
Last active August 6, 2020 11:25
Show Gist options
  • Select an option

  • Save aatronco/ae224a329e40e4d2650ec22cc1d065e6 to your computer and use it in GitHub Desktop.

Select an option

Save aatronco/ae224a329e40e4d2650ec22cc1d065e6 to your computer and use it in GitHub Desktop.
In Jumpseller
<!-- Estimate shipping values on the checkout page -->
<script>
function cleanEstimates(){
// remove all prices and errors
$('#shipping_options li').each(function(){ $(this).children().last().detach(); });
// add empty messages - placeholders
$('#shipping_options li').each(function(){ $(this).append('<span></span>') });
}
function shippingEstimates(){
cleanEstimates();
if($('#order_shipping_address_country').val() != "" && $('#order_shipping_address_region').val() != ""){
$.ajax({
method: "POST",
url: "/checkout/shipping_estimate",
data: {
estimate: {
country: $('#order_shipping_address_country').val(),
region: $('#order_shipping_address_region').val(),
municipality: $('#order_shipping_address_municipality').val(),
postal: $('#order_shipping_address_postal').val(),
city: $('#order_shipping_address_city').val()
}
}
}).done(function( data ) {
for(var i = 0; i < data.length; i++) {
// remove any previous messages & placeholders
$('#shipping_options #order_shipping_method_' + data[i].table.id).parent().children().last().detach();
if(data[i].table.error){
// disable options with errors
$('#shipping_options #order_shipping_method_' + data[i].table.id).attr('disabled', 'disabled');
// add error messages
$('#shipping_options #order_shipping_method_' + data[i].table.id).parent().append("<p class='shipping_information'><i>" + data[i].table.error_message + "</i></p>")
} else {
// enable options
$('#shipping_options #order_shipping_method_' + data[i].table.id).attr('disabled', false);
// add formatted shipping prices
$('#shipping_options #order_shipping_method_' + data[i].table.id).parent().append("<p class='shipping_information'><i>" + data[i].table.price + "</i></p>")
}
}
if ($('#shipping_options input:enabled').length == 0) {
$('#submit_review_order_2').addClass('disabled');
}
else {
$('#submit_review_order_2').removeClass('disabled');
}
});
} else {
// no Country or Region filled, clear shipping estimate info
cleanEstimates();
}
}
/* LISTENERS */
var debounceTimer = null; // so it does only a single request instead of lots of them.
$('#order_shipping_address_country, #order_shipping_address_region, #order_shipping_address_municipality, #order_shipping_address_city, #order_shipping_address_postal').change(function(){
clearTimeout(debounceTimer);
debounceTimer = setTimeout(function(){
shippingEstimates();
}, 300);
});
// END OF LISTENERS
$(document).ready(function(){
// add empty messages - placeholders
$('#shipping_options li').each(function(){ $(this).append('<span></span>') });
shippingEstimates();
})
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment