Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (function (FC, $) {
- // This is kinda dumb - SS requires that elements have an ID
- $('select[name=shipping_region]').attr('id', 'shipping_region');
- $('select[name=shipping_country]').attr('id', 'shipping_country');
- SS = jQuery.LiveAddress({
- key: "[PUBLIC-API-KEY]",
- verifySecondary: true,
- addresses: [{
- id: 'foxyAddress',
- address1: '#shipping_address1',
- address2: '#shipping_address2',
- locality: '#shipping_city',
- administrative_area: '#shipping_region',
- postal_code: '#shipping_postal_code',
- country: '#shipping_country'
- }]
- });
- SS.on("AddressAccepted", function(event, data, previousHandler) {
- // The zipcode doesn't like to get saved to FC, so I have to do this
- var zipcode = $('#shipping_postal_code').val();
- FC.json.shipping_address.postal_code = zipcode;
- previousHandler(event, data);
- });
- SS.on("Completed", function(event, data, previousHandler) {
- // This clears out any FC alerts on the DOM fields
- if ($("#shipping_address1").parent().hasClass('fc-alert-container--error'))
- $("#shipping_address1").trigger('focusout.fc', {validateNow: true});
- if ($("#shipping_address2").parent().hasClass('fc-alert-container--error'))
- $("#shipping_address2").trigger('focusout.fc', {validateNow: true});
- if ($("#shipping_city").parent().hasClass('fc-alert-container--error'))
- $("#shipping_city").trigger('focusout.fc', {validateNow: true});
- if ($("#shipping_region").parent().hasClass('fc-alert-container--error'))
- $("#shipping_region").trigger('focusout.fc', {validateNow: true});
- if ($("#shipping_postal_code").parent().hasClass('fc-alert-container--error'))
- $("#shipping_postal_code").trigger('focusout.fc', {validateNow: true});
- if ($("#shipping_country").parent().hasClass('fc-alert-container--error'))
- $("#shipping_country").trigger('focusout.fc', {validateNow: true});
- if (FC.util.addressHasLocationInfo(FC.json.shipping_address)) {
- FC[FC.json.context].getShippingOptions({address:FC.json.shipping_address});
- }
- previousHandler(event, data);
- });
- SS.on("MapInitialized", function(event, data, previousHandler) {
- // This has to be done to avoid really weird scenarios where addresses fail to re-verify after renders.
- SS.getMappedAddressByID('foxyAddress').set('administrative_area', '', false, true);
- SS.getMappedAddressByID('foxyAddress').set('address1', '', false, true);
- SS.getMappedAddressByID('foxyAddress').set('address2', '', false, true);
- SS.getMappedAddressByID('foxyAddress').set('locality', '', false, true);
- SS.getMappedAddressByID('foxyAddress').set('postal_code', '', false, true);
- SS.getMappedAddressByID('foxyAddress').set('country', '', false, true);
- previousHandler(event, data);
- });
- // SMARTYSTREETS RE-RENDER
- FC.client.on('render.done', function(params) {
- if (params.block_id == "customer_shipping" || params.block_id == "checkout") {
- $('select[name=shipping_region]').attr('id', 'shipping_region');
- $('select[name=shipping_country]').attr('id', 'shipping_country');
- var status = SS.getMappedAddressByID('foxyAddress').status()
- SS.mapFields([{
- id: 'foxyAddress',
- address1: '#shipping_address1',
- address2: '#shipping_address2',
- locality: '#shipping_city',
- administrative_area: '#shipping_region',
- postal_code: '#shipping_postal_code',
- country: '#shipping_country'
- }]);
- if (status == "accepted") {
- SS.getMappedAddressByID('foxyAddress').accept();
- }
- }
- });
- })(FC, jQuery);
Add Comment
Please, Sign In to add comment