Guest User

Untitled

a guest
Nov 12th, 2018
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. (function (FC, $) {
  2. // This is kinda dumb - SS requires that elements have an ID
  3. $('select[name=shipping_region]').attr('id', 'shipping_region');
  4. $('select[name=shipping_country]').attr('id', 'shipping_country');
  5.  
  6. SS = jQuery.LiveAddress({
  7. key: "[PUBLIC-API-KEY]",
  8. verifySecondary: true,
  9. addresses: [{
  10. id: 'foxyAddress',
  11. address1: '#shipping_address1',
  12. address2: '#shipping_address2',
  13. locality: '#shipping_city',
  14. administrative_area: '#shipping_region',
  15. postal_code: '#shipping_postal_code',
  16. country: '#shipping_country'
  17. }]
  18. });
  19.  
  20. SS.on("AddressAccepted", function(event, data, previousHandler) {
  21. // The zipcode doesn't like to get saved to FC, so I have to do this
  22. var zipcode = $('#shipping_postal_code').val();
  23. FC.json.shipping_address.postal_code = zipcode;
  24. previousHandler(event, data);
  25. });
  26.  
  27. SS.on("Completed", function(event, data, previousHandler) {
  28. // This clears out any FC alerts on the DOM fields
  29. if ($("#shipping_address1").parent().hasClass('fc-alert-container--error'))
  30. $("#shipping_address1").trigger('focusout.fc', {validateNow: true});
  31. if ($("#shipping_address2").parent().hasClass('fc-alert-container--error'))
  32. $("#shipping_address2").trigger('focusout.fc', {validateNow: true});
  33. if ($("#shipping_city").parent().hasClass('fc-alert-container--error'))
  34. $("#shipping_city").trigger('focusout.fc', {validateNow: true});
  35. if ($("#shipping_region").parent().hasClass('fc-alert-container--error'))
  36. $("#shipping_region").trigger('focusout.fc', {validateNow: true});
  37. if ($("#shipping_postal_code").parent().hasClass('fc-alert-container--error'))
  38. $("#shipping_postal_code").trigger('focusout.fc', {validateNow: true});
  39. if ($("#shipping_country").parent().hasClass('fc-alert-container--error'))
  40. $("#shipping_country").trigger('focusout.fc', {validateNow: true});
  41.  
  42. if (FC.util.addressHasLocationInfo(FC.json.shipping_address)) {
  43. FC[FC.json.context].getShippingOptions({address:FC.json.shipping_address});
  44. }
  45. previousHandler(event, data);
  46. });
  47.  
  48. SS.on("MapInitialized", function(event, data, previousHandler) {
  49. // This has to be done to avoid really weird scenarios where addresses fail to re-verify after renders.
  50. SS.getMappedAddressByID('foxyAddress').set('administrative_area', '', false, true);
  51. SS.getMappedAddressByID('foxyAddress').set('address1', '', false, true);
  52. SS.getMappedAddressByID('foxyAddress').set('address2', '', false, true);
  53. SS.getMappedAddressByID('foxyAddress').set('locality', '', false, true);
  54. SS.getMappedAddressByID('foxyAddress').set('postal_code', '', false, true);
  55. SS.getMappedAddressByID('foxyAddress').set('country', '', false, true);
  56.  
  57. previousHandler(event, data);
  58. });
  59.  
  60. // SMARTYSTREETS RE-RENDER
  61. FC.client.on('render.done', function(params) {
  62. if (params.block_id == "customer_shipping" || params.block_id == "checkout") {
  63. $('select[name=shipping_region]').attr('id', 'shipping_region');
  64. $('select[name=shipping_country]').attr('id', 'shipping_country');
  65.  
  66. var status = SS.getMappedAddressByID('foxyAddress').status()
  67.  
  68. SS.mapFields([{
  69. id: 'foxyAddress',
  70. address1: '#shipping_address1',
  71. address2: '#shipping_address2',
  72. locality: '#shipping_city',
  73. administrative_area: '#shipping_region',
  74. postal_code: '#shipping_postal_code',
  75. country: '#shipping_country'
  76. }]);
  77.  
  78. if (status == "accepted") {
  79. SS.getMappedAddressByID('foxyAddress').accept();
  80. }
  81. }
  82. });
  83. })(FC, jQuery);
Add Comment
Please, Sign In to add comment