Skip to content

Instantly share code, notes, and snippets.

@Ratne
Created March 31, 2022 10:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ratne/fd46b12a61fbd2aafc3be94e9ea2122b to your computer and use it in GitHub Desktop.
Save Ratne/fd46b12a61fbd2aafc3be94e9ea2122b to your computer and use it in GitHub Desktop.
function p_iva_update_checkout() {
if ( ! is_checkout() ) return;
?>
<style>
.nascondi{display:none};
</style>
<?php
}
add_action('wp_footer', 'p_iva_update_checkout', 50);
//COMBINAZIONE DI DEFAULT DELLE SELECT : AZIENDA; ITALIA unset
add_filter( 'default_checkout_billing_state', 'change_default_checkout_state' );
add_filter( 'default_checkout_shipping_state', 'change_default_checkout_state' );
function change_default_checkout_state() {
return '';
}
add_filter( 'default_checkout_billing_country', 'change_default_billing_country' );
function change_default_billing_country() {
return '';
}
add_filter( 'default_checkout_select_user_type', 'change_default_select_user_type' );
function change_default_select_user_type() {
return '';
}
add_action( 'woocommerce_after_order_notes', 'custom_checkout_scripts_and_fields', 10, 1 );
function custom_checkout_scripts_and_fields( $checkout ) {
$required = esc_attr__( 'required', 'woocommerce' );
$countries = array( 'IT' );
$tipologia= array( 'azienda' );
echo '<input type="hidden" name="customertaxid_check" id="customertaxid_check" value="0">';
$countries_str = "'".implode( "', '", $countries )."'"; // Formatting countries for jQuery
?>
<script type="text/javascript">
(function($){
var required = '<abbr class="required" title="<?php echo $required; ?>">*</abbr>',
countries = [<?php echo $countries_str; ?>],
location = $('#billing_country option:selected').val(),
tipologia = [<?php echo $tipologia_str; ?>],
azienda = $('#select_user_type option:selected').val(),
customercompany ='',
customertaxid = '',
customervat = '',
customersdi ='',
customerpec ='';
function actionRequire( actionToDo='yes', selector='' ){
if ( actionToDo == 'yes' ) {
$(selector).addClass("validate-required");
$(selector+' label').append(required);
} else {
$(selector).removeClass("validate-required");
$(selector+' label > .required').remove();
}
$(selector).removeClass("woocommerce-validated");
$(selector).removeClass("woocommerce-invalid woocommerce-invalid-required-field");
}
// Default loading
// actionRequire( 'no','#customertaxid_field' );
// if( $.inArray( location, countries, tipologia, azienda) >= 0 && $(phoneCheck).val() == '0' ){
// actionRequire( 'no','#customertaxid_field' );
$(customercompany).val('0');
$(customertaxid).val('0');
$(customervat).val('0');
$(customersdi).val('0');
$(customerpec).val('0');
actionRequire( 'yes','#customercompany_field' );
actionRequire( 'yes','#customerpec_field' );
actionRequire( 'yes','#customertaxid_field' );
actionRequire( 'yes','#customervat_field' );
actionRequire( 'yes','#customersdi_field' );
// }
//COMBINAZIONE DI DEFAULT DELLE SELECT : AZIENDA ITALIA, NASCONDO QUINDI IL CF
$( document ).ready(function() {
$( "#customertaxid_field" ).addClass( "nascondi" );
});
// cambia i form live al change di billing country
$( 'form.checkout' ).on( 'change', '#billing_country' , function(aggiornacampi){
var location = $('#billing_country option:selected').val();
var tipologia = $('#select_user_type option:selected').val();
console.log(tipologia); console.log(location);
// AZIENDA
// if ( $.inArray( location, countries ) >= 0 && tipologia == 'azienda' ) {
if ( tipologia == 'azienda' ) {
//NASCONDI
$( "#customertaxid_field" ).addClass( "nascondi" );
} else {
//MOSTRA
$( "#customertaxid_field" ).removeClass( "nascondi" );
}
//P.IVA, pec, sdi, nome azienda nascosto se PRIVATO
if ( tipologia == 'privato' ) {
//NASCONDI
$( "#customervat_field" ).addClass( "nascondi" );
$( "#customercompany_field" ).addClass( "nascondi" );
$( "#customersdi_field" ).addClass( "nascondi" );
$( "#customerpec_field" ).addClass( "nascondi" );
} else {
//MOSTRA
$( "#customervat_field" ).removeClass( "nascondi" );
$( "#customercompany_field" ).removeClass( "nascondi" );
$( "#customersdi_field" ).removeClass( "nascondi" );
$( "#customerpec_field" ).removeClass( "nascondi" );
}
//Privato svizzera
if (location == 'CH' && tipologia == 'privato' ) {
//NASCONDI
$( "#customertaxid_field" ).addClass( "nascondi" );
$( "#customervat_field" ).addClass( "nascondi" );
$( "#customercompany_field" ).addClass( "nascondi" );
$( "#customersdi_field" ).addClass( "nascondi" );
$( "#customerpec_field" ).addClass( "nascondi" );
} else {
}
// AZIENDA NON italiana
if ( tipologia == 'azienda' && location !== 'IT' ) {
//NASCONDI
$( "#customertaxid_field" ).addClass( "nascondi" );
$( "#customerpec_field" ).addClass( "nascondi" );
$( "#customersdi_field" ).addClass( "nascondi" );
} else {
//MOSTRA
}
});
// AL CAMBIO DI TIPOLOGIA
// unisci sopra in multiple on mouseleave
$( 'form.checkout' ).on( 'change', '#select_user_type', function(){
var location = $('#billing_country option:selected').val();
var tipologia = $('#select_user_type option:selected').val();
// AZIENDA
if ( tipologia == 'azienda' ) {
//NASCONDI
$( "#customertaxid_field" ).addClass( "nascondi" );
} else {
//MOSTRA
$( "#customertaxid_field" ).removeClass( "nascondi" );
}
//P.IVA, pec, sdi, nome azienda nascosto se PRIVATO
if ( tipologia == 'privato' ) {
actionRequire( 'yes','#customertaxid_field' );
$(customertaxid).val('1');
//NASCONDI
$( "#customervat_field" ).addClass( "nascondi" );
$( "#customercompany_field" ).addClass( "nascondi" );
$( "#customersdi_field" ).addClass( "nascondi" );
$( "#customerpec_field" ).addClass( "nascondi" );
} else {
//MOSTRA
$( "#customervat_field" ).removeClass( "nascondi" );
$( "#customercompany_field" ).removeClass( "nascondi" );
$( "#customersdi_field" ).removeClass( "nascondi" );
$( "#customerpec_field" ).removeClass( "nascondi" );
}
//Privato svizzera
if (location == 'CH' && tipologia == 'privato' ) {
//NASCONDI
$( "#customertaxid_field" ).addClass( "nascondi" );
$( "#customervat_field" ).addClass( "nascondi" );
$( "#customercompany_field" ).addClass( "nascondi" );
$( "#customersdi_field" ).addClass( "nascondi" );
$( "#customerpec_field" ).addClass( "nascondi" );
} else {
}
// AZIENDA NON italiana
if ( tipologia == 'azienda' && location !== 'IT' ) {
//NASCONDI
$( "#customertaxid_field" ).addClass( "nascondi" );
$( "#customerpec_field" ).addClass( "nascondi" );
$( "#customersdi_field" ).addClass( "nascondi" );
} else {
//MOSTRA
}
});
})(jQuery);
</script>
<?php
}
add_action('woocommerce_checkout_process', 'billing_phone_field_process');
function billing_phone_field_process() {
if ( ! $_POST['customertaxid'] && $_POST['customertaxid'] == '1' )
wc_add_notice( __( 'Inserisci un codice fiscale valido' ), 'error' );
}
// VALIDAZIONI INVIO CHECKOUT
add_action('woocommerce_after_checkout_validation', 'custom_checkout_field_validation_process', 20, 2 );
function custom_checkout_field_validation_process( $data, $errors ) {
//PRIVATO ITA
// if( empty($_POST['customertaxid']) && $_POST['billing_country'] == 'IT' && $_POST['select_user_type'] == 'privato' )
// $errors->add( 'requirements', __( "Per favore inserisci il codice fiscale", "woocommerce" ) );
//PRIVATO OVUNQUE eccetto svizzera
if( empty($_POST['customertaxid']) && $_POST['select_user_type'] == 'privato' && $_POST['billing_country'] != 'CH' )
$errors->add( 'requirements', __( "<strong>Codice fiscale</strong> è un campo obbligatorio.", "woocommerce" ) );
//AZIENDA IT
if( empty($_POST['customercompany']) && $_POST['billing_country'] == 'IT' && $_POST['select_user_type'] == 'azienda' )
$errors->add( 'requirements', __( "<strong>Nome Azienda</strong> è un campo obbligatorio.", "woocommerce" ) );
if( empty($_POST['customervat']) && $_POST['billing_country'] == 'IT' && $_POST['select_user_type'] == 'azienda' )
$errors->add( 'requirements', __( "<strong>Partita Iva</strong> è un campo obbligatorio.", "woocommerce" ) );
if( empty($_POST['customersdi']) && $_POST['billing_country'] == 'IT' && $_POST['select_user_type'] == 'azienda' )
$errors->add( 'requirements', __( "<strong>Codice SDI</strong> è un campo obbligatorio.", "woocommerce" ) );
if( empty($_POST['customerpec']) && $_POST['billing_country'] == 'IT' && $_POST['select_user_type'] == 'azienda' )
$errors->add( 'requirements', __( "<strong>Pec</strong> è un campo obbligatorio.", "woocommerce" ) );
//AZIENDA DIVERSA DA IT
if( empty($_POST['customercompany']) && $_POST['billing_country'] !== 'IT' && $_POST['select_user_type'] == 'azienda' )
$errors->add( 'requirements', __( "<strong>Nome Azienda</strong> è un campo obbligatorio.", "woocommerce" ) );
if( empty($_POST['customervat']) && $_POST['billing_country'] !== 'IT' && $_POST['select_user_type'] == 'azienda' )
$errors->add( 'requirements', __( "<strong>Partita Iva</strong> è un campo obbligatorio.", "woocommerce" ) );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment