Skip to content

Instantly share code, notes, and snippets.

@fabiograsso
Created February 22, 2021 10:50
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 fabiograsso/57a9696d0e7e30399a4f2ef40455a387 to your computer and use it in GitHub Desktop.
Save fabiograsso/57a9696d0e7e30399a4f2ef40455a387 to your computer and use it in GitHub Desktop.
<?php
// Aggiunta campi per la richiesta fattura
add_filter( 'woocommerce_checkout_fields' , 'custom_additional_fields' );
function custom_additional_fields( $fields ) {
// Richiesta fattura
$fields['billing']['fattura'] = array(
'label' => __('Richiedo la fattura', 'woocommerce'),
'type' => 'checkbox',
'placeholder' => _x('Fattura', 'placeholder', 'woocommerce'),
'required' => false,
'class' => array('form-row-last'),
'clear' => true,
'priority' => 5
);
// Codice Fiscale
$fields['billing']['codice_fiscale'] = array(
'label' => __('Codice Fiscale', 'woocommerce'),
'placeholder' => _x('Codice Fiscale', 'placeholder', 'woocommerce'),
'required' => false,
'class' => array('form-row-last'),
'clear' => true,
'priority' => 180
);
// Partita IVA
$fields['billing']['partita_iva'] = array(
'label' => __('Partita IVA', 'woocommerce'),
'placeholder' => _x('Partita IVA', 'placeholder', 'woocommerce'),
'required' => false,
'class' => array('form-row-last'),
'clear' => true,
'priority' => 190
);
return $fields;
}
// Gestione dei metadati
add_action( 'woocommerce_checkout_update_order_meta', 'custom_order_meta' );
function custom_order_meta( $order_id ) {
if ( ! empty( $_POST['fattura'] ) ) {
update_post_meta( $order_id, 'fattura', strtoupper( sanitize_text_field( $_POST['fattura'] ) ) );
}
if ( ! empty( $_POST['codice_fiscale'] ) ) {
update_post_meta( $order_id, 'codice_fiscale', strtoupper( sanitize_text_field( $_POST['codice_fiscale'] ) ) );
}
if ( ! empty( $_POST['partita_iva'] ) ) {
update_post_meta( $order_id, 'partita_iva', sanitize_text_field( $_POST['partita_iva'] ) );
}
}
// Inserisco i campi nel backend di Wordpress
add_action( 'woocommerce_admin_order_data_after_billing_address', 'custom_order_meta_admin', 10, 1 );
function custom_order_meta_admin($order){
if (get_post_meta( $order->id, 'fattura', true )) {
$fattura = "Sì";
} else {
$fattura = "No";
}
echo '<h3>Dati di fatturazione</h3>';
echo '<div class="address">';
echo '<p><strong>'.__('Richiesta Fattura').':</strong> ' . $fattura . '</p>';
echo '<p><strong>'.__('Codice Fiscale').':</strong> ' . get_post_meta( $order->id, 'codice_fiscale', true ) . '</p>';
echo '<p><strong>'.__('Partita IVA').':</strong> ' . get_post_meta( $order->id, 'partita_iva', true ) . '</p>';
echo '</div>';
}
// Inserisco i campi nella mail dell'ordine
add_filter('woocommerce_email_order_meta', 'custom_order_meta_keys', 30, 3);
function custom_order_meta_keys( $order, $sent_to_admin, $plain_text ) {
$fattura = get_post_meta( $order->get_order_number(), 'fattura', true );
if( empty( $fattura ) ) // se non è stata richiesta fattura esco senza aggiungere nulla
return;
$codice_fiscale = get_post_meta( $order->get_order_number(), 'codice_fiscale', true );
$partita_iva = get_post_meta( $order->get_order_number(), 'partita_iva', true );
if ( $plain_text === false ) { // versione HTML
echo '
<table cellspacing="0" cellpadding="0" border="0" style="width:100%;vertical-align:top;margin-bottom:40px;padding:0">
<tbody><tr><td valign="top" width="50%" style="text-align:left;font-family:\'Helvetica Neue\',Helvetica,Roboto,Arial,sans-serif;border:0;padding:0">
<h2 style="color:#53a04c;display:block;font-family:&quot;Helvetica Neue&quot;,Helvetica,Roboto,Arial,sans-serif;font-size:18px;font-weight:bold;line-height:130%;margin:0 0 18px;text-align:left">Dati di fatturazione</h2>
<address style="padding:12px;color:#636363;border:1px solid #e5e5e5">
Richiesta fattura? Sì<br/>
Codice Fiscale: ' . $codice_fiscale . '<br/>
Partita IVA: ' . $partita_iva .
'</address>
</td>
</tr></tbody></table>';
} else { // versione plain text
echo "DATI FATTURAZIONE\n
Richiesta fattura: Sì
Codice Fiscale: $codice_fiscale
Partita IVA: $partita_iva";
}
}
// Imposto i vari attributi in modo che vengano salvati nel profilo utente
add_action( 'woocommerce_checkout_update_user_meta', 'custom_checkout_update_user_meta', 10, 2 );
function custom_checkout_update_user_meta( $customer_id, $posted ) {
if (isset($posted['partita_iva'])) {
$partita_iva = sanitize_text_field( $posted['partita_iva'] );
update_user_meta( $customer_id, 'partita_iva', $partita_iva);
}
if (isset($posted['codice_fiscale'])) {
$codice_fiscale = sanitize_text_field( $posted['codice_fiscale'] );
update_user_meta( $customer_id, 'codice_fiscale', $codice_fiscale);
}
if (isset($posted['fattura'])) {
$codice_fiscale = sanitize_text_field( $posted['fattura'] );
update_user_meta( $customer_id, 'fattura', $codice_fiscale);
}
}
// Javascript per la gestione della fatturazione (faccio apparire i campi solo se c'è il flag nella fattura)
add_action( 'woocommerce_after_checkout_form', 'custom_add_jscript_checkout');
function custom_add_jscript_checkout() {
echo <<<EOT
<style type="text/css">
#billing_phone_field,
#billing_country_field,
#billing_company_field,
#billing_address_1_field,
#billing_address_2_field,
#billing_postcode_field,
#billing_city_field,
#billing_state_field,
#codice_fiscale_field,
#partita_iva_field { display : none!important; }
</style>
<script type="text/javascript">
jQuery(function(t) {
var checkbox = "#fattura"
// campi da mostrare/nascondere
var fields = ["#billing_country_field",
"#billing_company_field",
"#billing_address_1_field",
"#billing_address_2_field",
"#billing_postcode_field",
"#billing_city_field",
"#billing_state_field",
"#codice_fiscale_field",
"#partita_iva_field"];
function check(){
if( t(checkbox).is(":checked") ) {
fields.forEach(function (item) {
t(item).attr("style", "display: block!important");
});
} else {
fields.forEach(function (item) {
t(item).attr("style", "display: none!important");
});
}
// workaround per far renderizzare correttamente il selettore di country
t("#billing_country").trigger("change");
}
// al cambio della checkbox mostro o nascondo i campi
t(checkbox).change(function() {
check();
})
check(); // eseguo al caricamento della pagina
})
</script>
EOT;
}
//se è richiesta la fattura controllo che il codice fiscale e/o la partita iva siano compilate
add_action('woocommerce_checkout_process', 'required_cf_checkout_field_process');
function required_cf_checkout_field_process() {
if ($_POST['fattura'] == true) {
if ($_POST['codice_fiscale'] == null || $_POST['partita_iva'])
wc_add_notice( __( '<b>Per ricevere la fattura devi inserire una Partita IVA e/o un Codice Fiscale valido.</b>' ), 'error' );
if ($_POST['billing_country'] == null || $_POST['billing_address_1'] == null || $_POST['billing_city'] == null )
wc_add_notice( __( '<b>Per ricevere la fattura devi inserire un indirizzo valido.</b>'), 'error' );
if (($_POST['billing_country'] == 'IT' || $_POST['billing_country'] == 'Italia') && $_POST['billing_company'] == '' ) // verifico se è un utente italiano e privato (ovvero senza billing_company)
if (!validateCodiceFiscale($_POST['codice_fiscale'])) // in questo caso valido il codice fiscale
wc_add_notice( __( '<b>Se sei residente in Italia e desideri la fattura come utente privato, devi inserire un Codice Fiscale valido. In alternativa rimuovi la spunta dalla richiesta fattura oppure compila il nome della Società (Ragione Sociale).</b>' . $_POST['billing_company'] ), 'error' );
}
}
// code by Manuel Marangoni - http://www.manuelmarangoni.it
/** controllo del codice fiscale **/
function validateCodiceFiscale($cf){
if($cf=='')
return false;
if(strlen($cf)!= 16)
return false;
$cf=strtoupper($cf);
if(!preg_match("/[A-Z0-9]+$/", $cf))
return false;
$s = 0;
for($i=1; $i<=13; $i+=2){
$c=$cf[$i];
if('0'<=$c and $c<='9')
$s+=ord($c)-ord('0');
else
$s+=ord($c)-ord('A');
}
for($i=0; $i<=14; $i+=2){
$c=$cf[$i];
switch($c){
case '0': $s += 1; break;
case '1': $s += 0; break;
case '2': $s += 5; break;
case '3': $s += 7; break;
case '4': $s += 9; break;
case '5': $s += 13; break;
case '6': $s += 15; break;
case '7': $s += 17; break;
case '8': $s += 19; break;
case '9': $s += 21; break;
case 'A': $s += 1; break;
case 'B': $s += 0; break;
case 'C': $s += 5; break;
case 'D': $s += 7; break;
case 'E': $s += 9; break;
case 'F': $s += 13; break;
case 'G': $s += 15; break;
case 'H': $s += 17; break;
case 'I': $s += 19; break;
case 'J': $s += 21; break;
case 'K': $s += 2; break;
case 'L': $s += 4; break;
case 'M': $s += 18; break;
case 'N': $s += 20; break;
case 'O': $s += 11; break;
case 'P': $s += 3; break;
case 'Q': $s += 6; break;
case 'R': $s += 8; break;
case 'S': $s += 12; break;
case 'T': $s += 14; break;
case 'U': $s += 16; break;
case 'V': $s += 10; break;
case 'W': $s += 22; break;
case 'X': $s += 25; break;
case 'Y': $s += 24; break;
case 'Z': $s += 23; break;
}
}
if( chr($s%26+ord('A'))!=$cf[15] )
return false;
return true;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment