Skip to content

Instantly share code, notes, and snippets.

@aatronco
Last active January 22, 2018 15:17
Show Gist options
  • Save aatronco/83afdd10a2971e040476717ad61316a6 to your computer and use it in GitHub Desktop.
Save aatronco/83afdd10a2971e040476717ad61316a6 to your computer and use it in GitHub Desktop.
<script type="text/javascript">
// In admin panel: Settings > Checkout should be like: https://www.evernote.com/l/AJqFD-JxfUtEoI5LVNy_tRocu30aPTBBVuk
// Boleta o Factura Should be like: https://www.evernote.com/l/AJpcG43vJa1GLbQi2OgbRN_T4iUvJcIS4VQ
$(document).ready(function(){
setTimeout(function(){ $("#other").prependTo("#shipping_address_same_as_shipping"); $("#order_billing_address_country").val('CL'); $("#order_billing_address_country").change(); }, 1000); // set billing address for chile.
var Fn = {
// Valida el rut con su cadena completa "XXXXXXXX-X"
validaRut : function (rutCompleto) {
if (!/^[0-9]+[-|‐]{1}[0-9kK]{1}$/.test( rutCompleto ))
return false;
var tmp = rutCompleto.split('-');
var digv = tmp[1];
var rut = tmp[0];
if ( digv == 'K' ) digv = 'k' ;
return (Fn.dv(rut) == digv );
},
dv : function(T){
var M=0,S=1;
for(;T;T=Math.floor(T/10))
S=(S+T%10*(9-M++%6))%11;
return S?S-1:'k';
}
}
$("#checkout").on("submit", function(){
var option = $("#order_other_boleta_o_factura option:selected").val();
if(option == 'Factura Electronica'){
//check if RUT is valid.
var tax_id = $("#order_billing_address_taxid").val().trim();
if(!tax_id){
tax_id = $("#order_billing_address_taxid").val().trim();
}
   if(!Fn.validaRut(tax_id)){
alert('RUT Inválido.');
return false;
}
// check if all data exists.
if(!$('#order_other_razon_social').val() || !$('#order_other_giro').val() ){
alert('RUT, Razón Social o Giro no presente.');
return false;
}
}
})
$('#other_razon_social, #other_giro').hide(); // hidden by default
$('#order_other_boleta_o_factura option[value=""]').remove(); // remove default value.
$('#shipping_address_taxid').hide();
$('#order_other_boleta_o_factura').on('change', function() {
var option = $("#order_other_boleta_o_factura option:selected").val();
if(option == 'Factura Electronica'){
$('#other_razon_social, #other_giro').show();
$('#shipping_address_taxid').show();
$("#billing_address_taxid").appendTo("#other");
$('#billing_address_taxid').show();
}else if(option == 'Boleta Electronica'){
$('#other_razon_social, #other_giro').hide();
$('#order_shipping_address_taxid').removeAttr('required');
$('#shipping_address_taxid').hide();
$('#billing_address_taxid').hide();
}else{
alert('Select list should contain the options: "Factura Electronica" or "Boleta Electronica"');
}
});
$('#order_other_boleta_o_factura').val("Boleta Electronica");
$('#shipping_address_taxid').appendTo('#order_other_boleta_o_factura');
$("#shipping_address_name,#shipping_address_surname").insertBefore("#contacts_email");
$("#order_billing_address_name").val($("#order_shipping_address_name").val());
$("#order_billing_address_surname").val($("#order_shipping_address_surname").val());
$("#billing_address_name,#billing_address_surname").hide();
$('#order_other_boleta_o_factura').change();
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment