Skip to content

Instantly share code, notes, and snippets.

@aatronco
Last active November 29, 2018 18:20
Show Gist options
  • Save aatronco/1237af2f65942f9d26a184d0a0c451b6 to your computer and use it in GitHub Desktop.
Save aatronco/1237af2f65942f9d26a184d0a0c451b6 to your computer and use it in GitHub Desktop.
<script>
// Municipalities and region codes are here: https://docs.google.com/spreadsheets/d/1X4A4gBMHehHSFWVDF1e17XPOc6dY4f6byqkl7MM7ENo/edit?usp=sharing
$("#order_shipping_address_region").change(function(){
if( $('#order_shipping_method_100969').is(':checked')){ // cambiar el numero 100969 por el codigo del medio de envio de retiro en tienda se obtiene acá http://prntscr.com/lf7v08
var interval = setInterval(function(){
$('#order_shipping_address_municipality').val(8261417); // cambiar código de comuna por el que corresponda
if($('#order_shipping_address_municipality').val()){
$('#order_shipping_address_municipality').parent().hide();
clearInterval(interval)
}
}, 1000)
}
});
$(document).ready(function(){
setTimeout(updateInfos, 1500)
});
$('.radiobox').change(updateInfos)
autoFilledInfos = {
order_shipping_address_address: 'Retiro en Tienda: Patronato 406',
order_shipping_address_city: 'Santiago', // Ciudad de Entrega
order_shipping_address_region : "12", //Codigo de región de entrega, Ohiggins is región "08"
order_shipping_address_postal: '8420360' //Codigo postal solo si es obligatorio
};
function updateInfos(){
selected = $('#order_shipping_method_100969').is(':checked') //reemplazar 81328 por el código del envío correspondiente a retiro en tienda
fieldTitle = selected ? 'Datos para el retiro' : 'Datos de envio'
$("#shipping_address h2").text(fieldTitle);
$.each(autoFilledInfos, function(fieldId, fieldValue){
value = selected ? fieldValue : ''
$('#' + fieldId).val(value)
if(fieldId == "order_shipping_address_region"){
// fire native change event on select element -- START
element = document.getElementById('order_shipping_address_region')
var evt = document.createEvent("HTMLEvents");
evt.initEvent("change", false, true);
element.dispatchEvent(evt);
// fire native change event on select element -- END
}
if(selected){
$('#' + fieldId).parent().hide()
}
else {
$('#' + fieldId).parent().show()
}
})
}
</script>
@aatronco
Copy link
Author

// something like this.... (To improve the regions or municipalities loading)

$( document ).ajaxSuccess(function( event, xhr, settings ) {
if ( settings.url == "checkout/regions" ) {
console.log("regions loaded!");
} else if (settings.url == "checkout/municipalities") {
console.log("municipalities loaded!");
}
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment