Dodatkowe pole Chcę otrzymać fakturę

W tym przypadku dodajemy pole checkbox Chcę otrzymać fakturę. Po zaznaczeniu pojawia się pole number NIP oraz textarea Dane do faktury. Walidacja odbywa się zarówno w functions.php jak i w global.js.

functions.php:


function custom_checkout_customer_field( $checkout ) {
woocommerce_form_field( 'faktura', array(
'type'            => 'checkbox',
'label' => __('Chcę otrzymać fakturę'),
'required'        => false,
'class'           => array('faktura-field', 'form-row-wide'),
), get_user_meta( get_current_user_id(), 'faktura' , true  ) );
woocommerce_form_field( 'nip', array(
'type'            => 'number',
'label' => '',
'placeholder'         => __('Numer NIP') . ' *',
'required'        => true,
'class'           => array('nip-field', 'form-row-wide'),
), get_user_meta( get_current_user_id(), 'nip' , true  ) );
woocommerce_form_field( 'dane_do_faktury', array(
'type'            => 'textarea',
'label' => '',
'placeholder'         => __('Dane do faktury') . ' *',
'required'        => true,
'class'           => array('dane-do-faktury-field', 'form-row-wide'),
), get_user_meta( get_current_user_id(), 'dane_do_faktury' , true  ) );
}
add_action( 'woocommerce_before_checkout_billing_form', 'custom_checkout_customer_field' );
function custom_checkout_customer_get_field_values() {
$fields = [
'faktura'	=> '',
'nip'			=> '',
'dane_do_faktury' => '',
];
foreach( $fields as $field_name => $value ) {
if( !empty( $_POST[ $field_name ] ) ) {
$fields[ $field_name ] = sanitize_text_field( $_POST[ $field_name ] );
} else {
unset( $fields[ $field_name ] );
}
}
return $fields;
}
function custom_checkout_customer_field_save( $order_id ) {
$order = wc_get_order( $order_id );
$field_values = custom_checkout_customer_get_field_values();
foreach( $field_values as $field_name => $value ) {
if( !empty( $field_values[ $field_name ] ) ) {
update_post_meta( $order_id, $field_name, $value );
if( $order->get_user_id() > 0 ) {
update_user_meta( $order->get_user_id(), $field_name, $value );
}
}
}
}
add_action( 'woocommerce_checkout_update_order_meta', 'custom_checkout_customer_field_save' );
function custom_checkout_customer_field_validate() {
$field_values = custom_checkout_customer_get_field_values();
$faktura = filter_input(INPUT_POST, 'faktura');
if($faktura == true) {
if ( empty( $field_values['nip'] ) ) {
wc_add_notice( __('Uzupełnij numer NIP.'), 'error' );
}
if ( !empty( $field_values['nip'] ) ) {
$billing_nip = filter_input(INPUT_POST, 'nip');
$valid = true;
$customer_country = WC()->customer->get_billing_country();
if ( in_array( $customer_country, WC()->countries->get_european_union_countries() ) ) {
$valid = is_eu_vat_number_valid( $billing_nip, $customer_country );
}
if ( false === $valid ) { 
wc_add_notice( sprintf( __( 'Niepoprawny numer NIP. Proszę podaj poprawny.' )), 'error', array('id'=>'nip') );
}
}
}
}
add_action( 'woocommerce_checkout_process', 'custom_checkout_customer_field_validate' );
function is_eu_vat_number_valid( $id, $customer_country ) {
$id = strtoupper( $id );
$id = preg_replace( '/[ -,.]/', '', $id );
if ( strlen( $id ) < 8 ) {
return false;
}
$country                     = substr( $id, 0, 2 );
$woocommerce_default_country = get_option( 'woocommerce_default_country', 0 );
if ( ! in_array( $customer_country, WC()->countries->get_european_union_countries() ) ) {
$id      = $customer_country . $id;
$country = $customer_country;
} else {
if ( $woocommerce_default_country === $customer_country ) {
if ( $country != $customer_country ) {
$id      = $customer_country . $id;
$country = $customer_country;
}
}
}
if ( $country !== $customer_country ) {
return false;
}
switch ( $country ) {
case 'AT': // AUSTRIA
$isValid = (bool) preg_match( '/^(AT)U(\d{8})$/', $id );
break;
case 'BE': // BELGIUM
$isValid = (bool) preg_match( '/(BE)(0?\d{9})$/', $id );
break;
case 'BG': // BULGARIA
$isValid = (bool) preg_match( '/(BG)(\d{9,10})$/', $id );
break;
case 'CHE': // Switzerland
$isValid = (bool) preg_match( '/(CHE)(\d{9})(MWST)?$/', $id );
break;
case 'CY': // CYPRUS
$isValid = (bool) preg_match( '/^(CY)([0-5|9]\d{7}[A-Z])$/', $id );
break;
case 'CZ': // CZECH REPUBLIC
$isValid = (bool) preg_match( '/^(CZ)(\d{8,10})(\d{3})?$/', $id );
break;
case 'DE': // GERMANY
$isValid = (bool) preg_match( '/^(DE)([1-9]\d{8})/', $id );
break;
case 'DK': // DENMARK
$isValid = (bool) preg_match( '/^(DK)(\d{8})$/', $id );
break;
case 'EE': // ESTONIA
$isValid = (bool) preg_match( '/^(EE)(10\d{7})$/', $id );
break;
case 'EL': // GREECE
$isValid = (bool) preg_match( '/^(EL)(\d{9})$/', $id );
break;
case 'ES': // SPAIN
$isValid = (bool) preg_match( '/^(ES)([A-Z]\d{8})$/', $id )
|| preg_match( '/^(ES)([A-H|N-S|W]\d{7}[A-J])$/', $id )
|| preg_match( '/^(ES)([0-9|Y|Z]\d{7}[A-Z])$/', $id )
|| preg_match( '/^(ES)([K|L|M|X]\d{7}[A-Z])$/', $id );
break;
case 'EU': // EU type
$isValid = (bool) preg_match( '/^(EU)(\d{9})$/', $id );
break;
case 'FI': // FINLAND
$isValid = (bool) preg_match( '/^(FI)(\d{8})$/', $id );
break;
case 'FR': // FRANCE
$isValid = (bool) preg_match( '/^(FR)(\d{11})$/', $id )
|| preg_match( '/^(FR)([(A-H)|(J-N)|(P-Z)]\d{10})$/', $id )
|| preg_match( '/^(FR)(\d[(A-H)|(J-N)|(P-Z)]\d{9})$/', $id )
|| preg_match( '/^(FR)([(A-H)|(J-N)|(P-Z)]{2}\d{9})$/', $id );
break;
case 'GB': // GREAT BRITAIN
$isValid = (bool) preg_match( '/^(GB)?(\d{9})$/', $id )
|| preg_match( '/^(GB)?(\d{12})$/', $id )
|| preg_match( '/^(GB)?(GD\d{3})$/', $id )
|| preg_match( '/^(GB)?(HA\d{3})$/', $id );
break;
case 'GR': // GREECE
$isValid = (bool) preg_match( '/^(GR)(\d{8,9})$/', $id );
break;
case 'HR': // CROATIA
$isValid = (bool) preg_match( '/^(HR)(\d{11})$/', $id );
break;
case 'HU': // HUNGARY
$isValid = (bool) preg_match( '/^(HU)(\d{8})$/', $id );
break;
case 'IE': // IRELAND
$isValid = (bool) preg_match( '/^(IE)(\d{7}[A-W])$/', $id )
|| preg_match( '/^(IE)([7-9][A-Z\*\+)]\d{5}[A-W])$/', $id )
|| preg_match( '/^(IE)(\d{7}[A-W][AH])$/', $id );
break;
case 'IT': // ITALY
$isValid = (bool) preg_match( '/^(IT)(\d{11})$/', $id );
break;
case 'LV': // LATVIA
$isValid = (bool) preg_match( '/^(LV)(\d{11})$/', $id );
break;
case 'LT': // LITHUNIA
$isValid = (bool) preg_match( '/^(LT)(\d{9}|\d{12})$/', $id );
break;
case 'LU': // LUXEMBOURG
$isValid = (bool) preg_match( '/^(LU)(\d{8})$/', $id );
break;
case 'MT': // MALTA
$isValid = (bool) preg_match( '/^(MT)([1-9]\d{7})$/', $id );
break;
case 'NL': // NETHERLAND
$isValid = (bool) preg_match( '/^(NL)(\d{9})B\d{2}$/', $id );
break;
case 'NO': // NORWAY
$isValid = (bool) preg_match( '/^(NO)(\d{9})$/', $id );
break;
case 'PL': // POLAND
$isValid = (bool) preg_match( '/^(PL)(\d{10})$/', $id );
break;
case 'PT': // PORTUGAL
$isValid = (bool) preg_match( '/^(PT)(\d{9})$/', $id );
break;
case 'RO': // ROMANIA
$isValid = (bool) preg_match( '/^(RO)([1-9]\d{1,9})$/', $id );
break;
case 'RS': // SERBIA
$isValid = (bool) preg_match( '/^(RS)(\d{9})$/', $id );
break;
case 'SI': // SLOVENIA
$isValid = (bool) preg_match( '/^(SI)([1-9]\d{7})$/', $id );
break;
case 'SK': // SLOVAK REPUBLIC
$isValid = (bool) preg_match( '/^(SK)([1-9]\d[(2-4)|(6-9)]\d{7})$/', $id );
break;
case 'SE': // SWEDEN
$isValid = (bool) preg_match( '/^(SE)(\d{10}01)$/', $id );
break;
default:
$isValid = false;
}
return $isValid;
}

global.js:

// CHECKOUT FIELDS VALIDATION
$(document).ready(function() {
var customerField       		= $('#faktura'),
billingCompanyField			= $('#dane_do_faktury_field');
billingCompanyFieldInput	= $('#dane_do_faktury');
billingNIPField				= $('#nip_field');
billingNIPFieldInput		= $('#nip');
// Check that all fields exist
if( !customerField.length || !billingCompanyField.length || !billingNIPField.length ) { return; }
function toggleVisibleFields() {
var selectedAnswer = customerField;
if(!selectedAnswer.is(':checked')) {
billingCompanyField.hide();
billingCompanyFieldInput.prop( "disabled", true );
billingNIPField.hide();
billingNIPFieldInput.prop( "disabled", true );
} else {
billingCompanyField.show();
billingCompanyFieldInput.prop( "disabled", false );
billingNIPField.show();
billingNIPFieldInput.prop( "disabled", false );
}
}
$(document).on('change', 'input[name=faktura]', toggleVisibleFields);
$('body').on('updated_checkout', toggleVisibleFields);
toggleVisibleFields();
});
function validateVAT() {
if($('.woocommerce-error li[data-id="vat-number"]').length > 0) {
$('#nip_field').addClass('woocommerce-invalid-vat');
} else {
$('#nip_field').removeClass('woocommerce-invalid-vat');
}
}