Custom VAT field issue in Woocommerce Custom VAT field issue in Woocommerce wordpress wordpress

Custom VAT field issue in Woocommerce


This code is a bit outdated since 2017 WooCommerce 3 version… Also some other things have changed in between.

Here is that revisited code:

add_filter('woocommerce_billing_fields' , 'display_billing_vat_fields');function display_billing_vat_fields($billing_fields){    $billing_fields['billing_vat'] = array(        'type' => 'text',        'label' =>  __('VAT number',  'woocommerce' ),        'class' => array('form-row-wide'),        'required' => false,        'clear' => true,        'priority' => 35, // To change the field location increase or decrease this value    );    return $billing_fields;}// Printing the Billing Address on My Accountadd_filter( 'woocommerce_my_account_my_address_formatted_address', 'custom_my_account_my_address_formatted_address', 10, 3 );function custom_my_account_my_address_formatted_address( $fields, $customer_id, $type ) {    if ( $type == 'billing' ) {        $fields['vat'] = get_user_meta( $customer_id, 'billing_vat', true );    }    return $fields;}// Checkout -- Order Received (printed after having completed checkout)add_filter( 'woocommerce_order_formatted_billing_address', 'custom_add_vat_formatted_billing_address', 10, 2 );function custom_add_vat_formatted_billing_address( $fields, $order ) {    $fields['vat'] = $order->get_meta('billing_vat');    return $fields;}// Creating merger VAT variables for printing formattingadd_filter( 'woocommerce_formatted_address_replacements', 'custom_formatted_address_replacements', 10, 2 );function custom_formatted_address_replacements( $replacements, $args  ) {    $replacements['{vat}'] = ! empty($args['vat']) ? $args['vat'] : '';    $replacements['{vat_upper}'] = ! empty($args['vat']) ? strtoupper($args['vat']) : '';    return $replacements;}//Defining the Spanish formatting to print the address, including VAT.add_filter( 'woocommerce_localisation_address_formats', 'custom_localisation_address_format' );function custom_localisation_address_format( $formats ) {    foreach($formats as $country => $string_address ) {        $formats[$country] = str_replace('{company}\n', '{company}\n{vat_upper}\n', $string_address);    }    return $formats;}add_filter( 'woocommerce_customer_meta_fields', 'custom_customer_meta_fields' );function custom_customer_meta_fields( $fields ) {    $fields['billing']['fields']['billing_vat'] = array(        'label'       => __( 'VAT number', 'woocommerce' )    );    return $fields;}add_filter( 'woocommerce_admin_billing_fields', 'custom_admin_billing_fields' );function custom_admin_billing_fields( $fields ) {    $fields['vat'] = array(        'label' => __( 'VAT number', 'woocommerce' ),        'show'  => true    );    return $fields;}add_filter( 'woocommerce_found_customer_details', 'custom_found_customer_details' );function custom_found_customer_details( $customer_data ) {    $customer_data['billing_vat'] = get_user_meta( $_POST['user_id'], 'billing_vat', true );    return $customer_data;}

Code goes in function.php file of your active child theme (or active theme). Tested and work.


Related answers: