Skip to content

Instantly share code, notes, and snippets.

@rafaelpatro
Last active November 13, 2019 19:49
Show Gist options
  • Save rafaelpatro/e22360f45d8808c3f561f6e153ec35c6 to your computer and use it in GitHub Desktop.
Save rafaelpatro/e22360f45d8808c3f561f6e153ec35c6 to your computer and use it in GitHub Desktop.

Revisions

  1. rafaelpatro revised this gist Nov 13, 2019. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions firecheckout-brazillian-address-autocomplete.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    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"},
    @@ -86,3 +87,4 @@
    });
    elem.dispatchEvent(new Event('keyup'));
    });
    });
  2. rafaelpatro created this gist Nov 13, 2019.
    88 changes: 88 additions & 0 deletions firecheckout-brazillian-address-autocomplete.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,88 @@
    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'));
    });