Guest User

Untitled

a guest
Nov 19th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. <!-- Estimate shipping values on the checkout page -->
  2. <script>
  3. function cleanEstimates(){
  4. // remove all prices and errors
  5. $('#shipping_options li').each(function(){ $(this).children().last().detach(); });
  6.  
  7. // add empty messages - placeholders
  8. $('#shipping_options li').each(function(){ $(this).append('<span></span>') });
  9. }
  10.  
  11. function shippingEstimates(){
  12. if($('#order_shipping_address_country').val() != "" && $('#order_shipping_address_region').val() != ""){
  13. $.ajax({
  14. method: "POST",
  15. url: "/checkout/shipping_estimate",
  16. data: {
  17. estimate: {
  18. country: $('#order_shipping_address_country').val(),
  19. region: $('#order_shipping_address_region').val(),
  20. municipality: $('#order_shipping_address_municipality').val(),
  21. postal: $('#order_shipping_address_postal') .val()
  22. }
  23. }
  24. }).done(function( data ) {
  25. for(var i = 0; i < data.length; i++){
  26. // remove any previous messages & placeholders
  27. $('#shipping_options #order_shipping_method_' + data[i].table.id).parent().children().last().detach();
  28.  
  29. if(data[i].table.error){
  30. // disable options with errors
  31. $('#shipping_options #order_shipping_method_' + data[i].table.id).attr('disabled', 'disabled');
  32.  
  33. // add error messages
  34. $('#shipping_options #order_shipping_method_' + data[i].table.id).parent().append("<p class='shipping_information'><i>" + data[i].table.error_message + "</i></p>")
  35. } else {
  36. // enable options
  37. $('#shipping_options #order_shipping_method_' + data[i].table.id).attr('disabled', false);
  38.  
  39. // add formatted shipping prices
  40. $('#shipping_options #order_shipping_method_' + data[i].table.id).parent().append("<p class='shipping_information'><i>" + data[i].table.price + "</i></p>")
  41. }
  42. }
  43. });
  44. } else {
  45. // no Country or Region filled, clear shipping estimate info
  46. cleanEstimates();
  47. }
  48. }
  49.  
  50. $('#order_shipping_address_country').change(function(){shippingEstimates()});
  51. $('#order_shipping_address_region').change(function(){shippingEstimates()});
  52. $('#order_shipping_address_municipality').change(function(){shippingEstimates()});
  53. $(document).ready(function(){
  54. // add empty messages - placeholders
  55. $('#shipping_options li').each(function(){ $(this).append('<span></span>') });
  56. shippingEstimates();
  57. })
  58. </script>
Add Comment
Please, Sign In to add comment