Last active
May 30, 2023 09:01
-
-
Save FrancoStino/87125a57fac999d5e930aed01abea7c6 to your computer and use it in GitHub Desktop.
Custom Field (Codice fiscale - Partita IVA- SDI/PEC) at checkout with conditional checkbox - Checkout - Woocommerce
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Custom Field (Codice fiscale & Partita IVA) at checkout with conditional checkbox - Checkout - Woocommerce | |
*/ | |
add_filter( 'woocommerce_checkout_fields' , 'cbi_cf_chkbox' ); | |
function cbi_cf_chkbox ( $fields ) { | |
//if ( ICL_LANGUAGE_CODE !='it' ) return $fields; // Only for Italy | |
$fields['billing']['checkbox_cf'] = array( | |
'type' => 'checkbox', | |
'label' => __('Vuoi ricevere fattura?', 'cbi-custom-parts'), | |
'class' => array('form-row-wide'), | |
'clear' => true | |
); | |
$fields['billing']['cf_in'] = array( | |
'label' => __('Inserisci il Codice fiscale/Partita IVA', 'cbi-custom-parts'), | |
'placeholder' => _x('RSSMRA85T10A562S', 'placeholder', 'cbi-custom-parts'), | |
'class' => array('form-row-wide'), | |
'clear' => true | |
); | |
$fields['billing']['sdi_in'] = array( | |
'label' => __('Inserisci il Codice SDI', 'cbi-custom-parts'), | |
'placeholder' => _x('XXXXXXX', 'placeholder', 'cbi-custom-parts'), | |
'class' => array('form-row-wide'), | |
'clear' => true | |
); | |
return $fields; | |
} | |
add_action( 'woocommerce_after_checkout_form', 'cbi_cf_conditionally_hide_show', 6); | |
function cbi_cf_conditionally_hide_show() { | |
//if ( ICL_LANGUAGE_CODE !='it' ) return; // Only for Italy | |
$required = esc_attr__( 'required', 'woocommerce' ); | |
?> | |
<script type="text/javascript"> | |
(function($){ | |
var required = '<abbr class="required" title="<?php echo $required; ?>">*</abbr>'; // Required html | |
$('#cf_in_field > #cf_in').prop('pattern', "^[a-zA-Z]{6}[0-9]{2}[a-zA-Z][0-9]{2}[a-zA-Z][0-9]{3}[a-zA-Z]$"); // Doesn't seem to do something | |
$('#cf_in_field').hide(); | |
$('#sdi_in_field').hide(); | |
$('input#checkbox_cf').change(function(){ | |
if (this.checked) { | |
$('#cf_in_field').fadeIn("fast", function(){ | |
$(this).addClass("validate-required"); | |
$('#cf_in_field > label').append(required); | |
}); | |
$('#sdi_in_field').fadeIn("fast", function(){ | |
$(this).addClass("validate-required"); | |
$('#sdi_in_field > label').append(required); | |
}); | |
} else { | |
$('#cf_in_field').fadeOut("fast", function(){ | |
$(this).removeClass("validate-required"); | |
$('#cf_in_field > label > .required').remove(); | |
}); | |
$('#sdi_in_field').fadeOut("fast", function(){ | |
$(this).removeClass("validate-required"); | |
$('#sdi_in_field > label > .required').remove(); | |
}); | |
} | |
$('#cf_in_field').val(''); | |
$('#cf_in_field').removeClass("woocommerce-validated"); | |
$('#cf_in_field').removeClass("woocommerce-invalid woocommerce-invalid-required-field"); | |
$('#sdi_in_field').val(''); | |
$('#sdi_in_field').removeClass("woocommerce-validated"); | |
$('#sdi_in_field').removeClass("woocommerce-invalid woocommerce-invalid-required-field"); | |
}); | |
})(jQuery); | |
</script> | |
<?php | |
} | |
// Check custom fields value "codice fiscale" when submit and return error notices (if needed) | |
add_action('woocommerce_checkout_process', 'cbi_cf_process'); | |
function cbi_cf_process() { | |
if ( isset($_POST['checkbox_cf']) && $_POST['checkbox_cf'] == 1 ) { | |
if( empty( $_POST['cf_in'] ) ) { | |
wc_add_notice( __( "Inserisci il tuo Codice Fiscale/Partita Iva", "cbi-custom-parts" ), "error" ); | |
} | |
if( empty( $_POST['sdi_in'] ) ) { | |
wc_add_notice( __( "Inserisci il tuo Codice destinatario", "cbi-custom-parts" ), "error" ); | |
} | |
} | |
} | |
// Save the custom field value "codice fiscale" in order meta and in user meta | |
add_action( 'woocommerce_checkout_update_order_meta', 'cbi_cf_in_update_order_meta' ); | |
function cbi_cf_in_update_order_meta ( $order_id ) { | |
if ( empty( $_POST['cf_in'] ) ) return; | |
if ( empty( $_POST['sdi_in'] ) ) return; | |
$customer_id = get_post_meta( $order_id, '_customer_user', true ); | |
$user_codice_fiscale = get_user_meta( $order_id, 'codice_fiscale', true ); | |
$user_sdi = get_user_meta( $order_id, 'sdi', true ); | |
if( ! empty( $user_codice_fiscale ) ) | |
update_user_meta($order->user_id, 'codice_fiscale', sanitize_text_field( $_POST['cf_in'] ) ); | |
if( ! empty( $user_sdi ) ) | |
update_user_meta($order->user_id, 'sdi', sanitize_text_field( $_POST['sdi_in'] ) ); | |
update_post_meta( $order_id, '_codice_fiscale', sanitize_text_field( $_POST['cf_in'] ) ); | |
update_post_meta( $order_id, '_sdi', sanitize_text_field( $_POST['sdi_in'] ) ); | |
} | |
// Backend : Display in Order edit pages, after billing address, the custom field value "codice fiscale" | |
add_action( 'woocommerce_admin_order_data_after_billing_address', 'cbi_cf_admin_order_data_after_billing_address', 10, 1 ); | |
function cbi_cf_admin_order_data_after_billing_address( $order ){ | |
$codice_fiscale = get_post_meta( $order->get_id(), '_codice_fiscale', true ); | |
$sdi = get_post_meta( $order->get_id(), '_sdi', true ); | |
if( ! empty( $codice_fiscale ) ) | |
echo '<p><strong>'.__('Codice fiscale/Partita IVA', 'cbi-cf-invoice').':</strong> ' . $codice_fiscale . '</p>'; | |
if( ! empty( $sdi ) ) | |
echo '<p><strong>'.__('Codice destinatario', 'cbi-sdi-invoice').':</strong> ' . $sdi . '</p>'; | |
} | |
// Backend: Display and edit user profile custom field value "codice fiscale" Only for Italy | |
add_action( 'show_user_profile', 'add_extra_user_codice_fiscale', 1, 1 ); | |
add_action( 'edit_user_profile', 'add_extra_user_codice_fiscale', 1, 1 ); | |
function add_extra_user_codice_fiscale( $user ) | |
{ | |
//if( get_user_meta( $user->ID, 'billing_country', true ) != 'IT' ) return; // Only for Italy | |
$codice_fiscale = get_user_meta( $user->ID, 'codice_fiscale', true ); | |
$sdi = get_user_meta( $user->ID, 'sdi', true ); | |
if( empty( $codice_fiscale ) ) $codice_fiscale = ''; | |
if( empty( $sdi ) ) $sdi = ''; | |
?> | |
<h3><?php _e( "Codice fiscale/Partita IVA", "cbi-custom-parts" ); ?></h3> | |
<table class="form-table"><tr> | |
<th><label for="codice_fiscale"><?php _e( "Codice fiscale/Partita IVA", "cbi-custom-parts" ); ?></label></th> | |
<td><input type="text" name="codice_fiscale" value="<?php echo esc_attr($codice_fiscale); ?>" class="regular-text" /></td> | |
</tr> | |
</table> | |
<br /> | |
<h3><?php _e( "Codice destinatario", "cbi-custom-parts" ); ?></h3> | |
<table class="form-table"><tr> | |
<th><label for="sdi"><?php _e( "Codice destinatario", "cbi-custom-parts" ); ?></label></th> | |
<td><input type="text" name="sdi" value="<?php echo esc_attr($sdi); ?>" class="regular-text" /></td> | |
</tr> | |
</table> | |
<br /> | |
<?php | |
} | |
// Backend: Save edited user profile custom field value "codice fiscale" Only for Italy | |
add_action( 'personal_options_update', 'save_extra_user_codice_fiscale' ); | |
add_action( 'edit_user_profile_update', 'save_extra_user_codice_fiscale' ); | |
function save_extra_user_codice_fiscale( $user_id ) | |
{ | |
if( ! empty( $_POST['codice_fiscale'] ) ) | |
update_user_meta( $user_id, 'codice_fiscale', sanitize_text_field( $_POST['codice_fiscale'] ) ); | |
if( ! empty( $_POST['sdi'] ) ) | |
update_user_meta( $user_id, 'sdi', sanitize_text_field( $_POST['sdi'] ) ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment