/**
* Snippet Name: Add electronic billing fields (Italian customers)
* Snippet Author: coding-bunny.com
* Description: Adds fields for electronic invoicing on the checkout page for Italian customers.
*/
add_filter('woocommerce_checkout_fields', 'dmmfe_checkout_fields');
function dmmfe_checkout_fields($fields) {
unset($fields['order']['order_comments']);
$email_priority = isset($fields['billing']['billing_email']['priority']) ? $fields['billing']['billing_email']['priority'] : 110;
$fields['billing']['billing_need_e_invoice'] = array(
'label' => __('Ho bisogno della fattura elettronica', 'dmmfe'),
'type' => 'checkbox',
'class' => array('form-row-wide'),
'clear' => true,
'priority' => $email_priority + 1,
);
$fields['billing']['billing_company_name'] = array(
'label' => __('Ragione Sociale', 'dmmfe'),
'required' => false,
'class' => array('form-row-wide', 'dmmfe-hidden-field'),
'clear' => true,
'priority' => $email_priority + 2,
);
$fields['billing']['billing_vat_code'] = array(
'label' => __('Partita IVA', 'dmmfe'),
'required' => false,
'class' => array('form-row-first', 'dmmfe-hidden-field'),
'clear' => false,
'priority' => $email_priority + 3,
);
$fields['billing']['billing_tax_code'] = array(
'label' => __('Codice Fiscale', 'dmmfe'),
'required' => false,
'class' => array('form-row-last', 'dmmfe-hidden-field'),
'clear' => true,
'priority' => $email_priority + 4,
);
$fields['billing']['billing_pec'] = array(
'label' => __('Indirizzo PEC', 'dmmfe'),
'type' => 'email',
'required' => false,
'class' => array('form-row-first', 'dmmfe-hidden-field'),
'clear' => false,
'priority' => $email_priority + 5,
);
$fields['billing']['billing_sdi_code'] = array(
'label' => __('Codice Univoco SDI', 'dmmfe'),
'required' => false,
'class' => array('form-row-last', 'dmmfe-hidden-field'),
'clear' => true,
'priority' => $email_priority + 6,
);
return $fields;
}
add_action('wp_enqueue_scripts', 'dmmfe_enqueue_scripts');
function dmmfe_enqueue_scripts() {
if (is_checkout()) {
wp_add_inline_script('jquery', "
jQuery(document).ready(function($){
function dmmfe_toggle_fields() {
if ($('#billing_need_e_invoice').is(':checked')) {
$('.dmmfe-hidden-field').show();
} else {
$('.dmmfe-hidden-field').hide();
}
}
dmmfe_toggle_fields();
$('#billing_need_e_invoice').on('change', function(){
dmmfe_toggle_fields();
});
$(document.body).on('updated_checkout', function(){
dmmfe_toggle_fields();
});
});
");
}
}
add_action('woocommerce_checkout_update_order_meta', 'dmmfe_save_order_meta');
function dmmfe_save_order_meta($order_id) {
if (isset($_POST['billing_need_e_invoice'])) {
update_post_meta($order_id, '_billing_need_e_invoice', wc_clean(wp_unslash($_POST['billing_need_e_invoice'])));
}
if (isset($_POST['billing_company_name'])) {
update_post_meta($order_id, '_billing_company_name', wc_clean(wp_unslash($_POST['billing_company_name'])));
}
if (isset($_POST['billing_vat_code'])) {
update_post_meta($order_id, '_billing_vat_code', wc_clean(wp_unslash($_POST['billing_vat_code'])));
}
if (isset($_POST['billing_tax_code'])) {
update_post_meta($order_id, '_billing_tax_code', wc_clean(wp_unslash($_POST['billing_tax_code'])));
}
if (isset($_POST['billing_pec'])) {
update_post_meta($order_id, '_billing_pec', sanitize_email(wp_unslash($_POST['billing_pec'])));
}
if (isset($_POST['billing_sdi_code'])) {
update_post_meta($order_id, '_billing_sdi_code', wc_clean(wp_unslash($_POST['billing_sdi_code'])));
}
}
add_action('woocommerce_admin_order_data_after_billing_address', 'dmmfe_admin_order_display');
function dmmfe_admin_order_display($order) {
$need_e_invoice = get_post_meta($order->get_id(), '_billing_need_e_invoice', true);
$company_name = get_post_meta($order->get_id(), '_billing_company_name', true);
$vat_code = get_post_meta($order->get_id(), '_billing_vat_code', true);
$tax_code = get_post_meta($order->get_id(), '_billing_tax_code', true);
$pec = get_post_meta($order->get_id(), '_billing_pec', true);
$sdi_code = get_post_meta($order->get_id(), '_billing_sdi_code', true);
if ($need_e_invoice == '1') {
echo '<h3>' . esc_html__('Dettagli Fattura Elettronica', 'dmmfe') . '</h3>';
echo '<p><strong>' . esc_html__('Fattura Elettronica:', 'dmmfe') . '</strong> ' . esc_html__('Sì', 'dmmfe') . '</p>';
if ($company_name) {
echo '<p><strong>' . esc_html__('Ragione Sociale:', 'dmmfe') . '</strong> ' . esc_html($company_name) . '</p>';
}
if ($vat_code) {
echo '<p><strong>' . esc_html__('Partita IVA:', 'dmmfe') . '</strong> ' . esc_html($vat_code) . '</p>';
}
if ($tax_code) {
echo '<p><strong>' . esc_html__('Codice Fiscale:', 'dmmfe') . '</strong> ' . esc_html($tax_code) . '</p>';
}
if ($pec) {
echo '<p><strong>' . esc_html__('Indirizzo PEC:', 'dmmfe') . '</strong> ' . esc_html($pec) . '</p>';
}
if ($sdi_code) {
echo '<p><strong>' . esc_html__('Codice Univoco SDI:', 'dmmfe') . '</strong> ' . esc_html($sdi_code) . '</p>';
}
}
}
add_filter('woocommerce_email_order_meta_fields', 'dmmfe_email_order_meta_fields', 10, 3);
function dmmfe_email_order_meta_fields($fields, $sent_to_admin, $order) {
$need_e_invoice = get_post_meta($order->get_id(), '_billing_need_e_invoice', true);
if ($need_e_invoice == '1') {
$fields['billing_need_e_invoice'] = array(
'label' => __('Fattura Elettronica', 'dmmfe'),
'value' => __('Sì', 'dmmfe'),
);
$company_name = get_post_meta($order->get_id(), '_billing_company_name', true);
if ($company_name) {
$fields['billing_company_name'] = array(
'label' => __('Ragione Sociale', 'dmmfe'),
'value' => $company_name,
);
}
$vat_code = get_post_meta($order->get_id(), '_billing_vat_code', true);
if ($vat_code) {
$fields['billing_vat_code'] = array(
'label' => __('Partita IVA', 'dmmfe'),
'value' => $vat_code,
);
}
$tax_code = get_post_meta($order->get_id(), '_billing_tax_code', true);
if ($tax_code) {
$fields['billing_tax_code'] = array(
'label' => __('Codice Fiscale', 'dmmfe'),
'value' => $tax_code,
);
}
$pec = get_post_meta($order->get_id(), '_billing_pec', true);
if ($pec) {
$fields['billing_pec'] = array(
'label' => __('Indirizzo PEC', 'dmmfe'),
'value' => $pec,
);
}
$sdi_code = get_post_meta($order->get_id(), '_billing_sdi_code', true);
if ($sdi_code) {
$fields['billing_sdi_code'] = array(
'label' => __('Codice Univoco SDI', 'dmmfe'),
'value' => $sdi_code,
);
}
}
return $fields;
}
add_action('wp_head', 'dmmfe_custom_styles');
function dmmfe_custom_styles() {
if (is_checkout()) {
echo '<style>
.optional { display:none !important; }
.dmmfe-hidden-field { display:none; }
.woocommerce-additional-fields { display:none !important; }
</style>';
}
}Add electronic billing fields (Italian customers)
Adds fields for electronic invoicing on the checkout page for Italian customers.


