Last active
November 13, 2019 19:49
-
-
Save rafaelpatro/e22360f45d8808c3f561f6e153ec35c6 to your computer and use it in GitHub Desktop.
Magento Brazilian Address Autocomplete (Firecheckout compatible)
This file contains 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
window.addEventListener('load', function(){ | |
Postmon = { | |
data: {}, | |
uf: {AC:"Acre",AL:"Alagoas",AP:"Amapá",AM:"Amazonas",BA:"Bahia",CE:"Ceará",DF:"Distrito Federal",ES:"Espírito Santo",GO:"Goiás",MA:"Maranhão",MT:"Mato Grosso",MS:"Mato Grosso do Sul",MG:"Minas Gerais",PA:"Pará",PB:"Paraíba",PR:"Paraná",PE:"Pernambuco",PI:"Piauí",RJ:"Rio de Janeiro",RN:"Rio Grande do Norte",RS:"Rio Grande do Sul",RO:"Rondônia",RR:"Roraima",SC:"Santa Catarina",SP:"São Paulo",SE:"Sergipe",TO:"Tocantins"}, | |
autofill: function() { | |
if ($j('#billing\\:street1').val().trim() == '' && typeof this.data.logradouro != 'undefined') { | |
$j('#billing\\:street1').val(this.data.logradouro); | |
} | |
if ($j('#billing\\:street4').val().trim() == '' && typeof this.data.bairro != 'undefined') { | |
$j('#billing\\:street4').val(this.data.bairro); | |
} | |
if ($j('#billing\\:city').val().trim() == '') { | |
if (typeof this.data.cidade != 'undefined') { | |
$j('#billing\\:city').val(this.data.cidade); | |
} else if (typeof this.data.localidade != 'undefined') { | |
$j('#billing\\:city').val(this.data.localidade); | |
} | |
} | |
if ($j('#billing\\:region_id').val() == '') { | |
var ufName = ''; | |
if (typeof this.data.estado != 'undefined') { | |
ufName = this.uf[this.data.estado]; | |
} else if (typeof this.data.uf != 'undefined') { | |
ufName = this.uf[this.data.uf]; | |
} | |
if (ufName != '') { | |
$j('#billing\\:region_id option:contains("' + ufName + '")').attr('selected', true); | |
$j('#billing\\:region_id').val( | |
$j('#billing\\:region_id option:contains("' + ufName + '")').val() | |
); | |
} | |
} | |
} | |
}; | |
Validation.add('validate-postmon', Translator.translate('Invalid Zip Code'), function(v){ | |
var test = true; | |
var postcode = v.replace(/[^0-9]/g, ''); | |
if (postcode.length != 8) { | |
test = false; | |
} else { | |
$j.ajax({ | |
//url: 'https://api.postmon.com.br/v1/cep/' + postcode, | |
url: ['https://viacep.com.br/ws/', postcode, '/json/'].join(''), | |
method: 'GET', | |
async: false, | |
timeout: 5000, | |
success: function(response) { | |
if (typeof Postmon != 'undefined') { | |
Postmon.data = response; | |
} | |
}, | |
error: function(xhr, status) { | |
if (parseNumber(status) < 500) { | |
test = false; | |
} | |
} | |
}); | |
} | |
return test; | |
}); | |
$$('#billing\\:postcode').each(function(elem){ | |
elem.addClassName('validate-postmon'); | |
elem.observe('keyup', function(e){ | |
var zip = this.value.replace(/\D/g, ''); | |
if (zip.length == 8 && Validation.validate(this)) { | |
try { | |
Postmon.autofill(); | |
} catch(e) {} | |
if (typeof checkout != 'undefined') { | |
checkout.saveField( | |
this, | |
checkout.urls.billing_address, | |
{}, | |
checkout.setLoadingField.bind(checkout, this, false) | |
); | |
} | |
} | |
}); | |
elem.dispatchEvent(new Event('keyup')); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment