Skip to content

Instantly share code, notes, and snippets.

@Miq3l
Forked from SiR-DanieL/functions.php
Last active June 22, 2021 14:15
Show Gist options
  • Save Miq3l/4870eecc80bb6a1f537c575e848ef2b5 to your computer and use it in GitHub Desktop.
Save Miq3l/4870eecc80bb6a1f537c575e848ef2b5 to your computer and use it in GitHub Desktop.
Adding VAT to WooCommerce
/***************************** FRONTEND ****************************************/
/**************************
Filter to add a VAT field to:
- My Account - Edit Form -- Billing fields
- Checkout - Edit Form - Billing Fields
This function is also reordering the form fields
Source: https://docs.woothemes.com/document/tutorial-customising-checkout-fields-using-actions-and-filters
***************************/
function add_woocommerce_billing_fields($billing_fields){
//reorder woo my billing address form fields
$billing_fields2['billing_first_name'] = $billing_fields['billing_first_name'];
$billing_fields2['billing_last_name'] = $billing_fields['billing_last_name'];
$billing_fields2['billing_vat'] = array(
'type' => 'text',
'label' => __('VAT number', 'keyelp-shop-customization' ),
'class' => array('form-row-wide'),
'required' => false,
'clear' => true
);
//unimos el resto de campos
$merged_billing_fields = $billing_fields2 + $billing_fields;
return $merged_billing_fields;
}
add_filter('woocommerce_billing_fields' , 'add_woocommerce_billing_fields');
/*********
Filters to add VAT when printing billing address on:
- (1) My account
- (2) Checkout - Order Received (after checkout compeltion),
+++ Additional filters to format the printed output.
********/
// (1) Printing the Billing Address on My Account
add_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;
}
// (2) 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->billing_vat;
return $fields;
}
// Creating merger VAT variables for printing formatting
add_filter( 'woocommerce_formatted_address_replacements', 'custom_formatted_address_replacements', 10, 2 );
function custom_formatted_address_replacements( $address, $args ) {
$address['{vat}'] = '';
$address['{vat_upper}']= '';
if ( ! empty( $args['vat'] ) ) {
$address['{vat}'] = $args['vat'];
$address['{vat_upper}'] = strtoupper($args['vat']);
}
return $address;
}
//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 ) {
$formats['ES'] = "{name}\n{company}\n{vat_upper}\n{address_1}\n{address_2}\n{postcode} {city}\n{state}\n{country}";
return $formats;
}
/***************************** ADMIN USER PROFILE PAGE ****************************************/
/***************
Filter to add VAT Customer meta fields (user profile field on the billing address grouping)
*****************/
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', 'keyelp-shop-customization' )
);
return $fields;
}
/*************************** ADMIN ORDER PAGE ****************************************/
/*********
Filter to add VAT to the Edit Form on: Order -- Admin page
*********/
add_filter( 'woocommerce_admin_billing_fields', 'custom_admin_billing_fields' );
function custom_admin_billing_fields( $fields ) {
$fields['vat'] = array(
'label' => __( 'VAT number', 'keyelp-shop-customization' ),
'show' => true
);
return $fields;
}
/****************
Filter to copy the VAT field from User meta fields to the Order Admin form (after clicking dedicated button on admin page)
******************/
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;
}
@lonniept
Copy link

Hi! Thx for the code!
However, I also have the same error:
[15-Feb-2021 17:50:15 UTC] billing_vat was called incorrectly. Order properties should not be accessed directly. Backtrace: require('wp-blog-header.php'), require_once('wp-includes/template-loader.php'), do_action('template_redirect'), WP_Hook->do_action, WP_Hook->apply_filters, WC_AJAX::do_wc_ajax, do_action('wc_ajax_checkout'), WP_Hook->do_action, WP_Hook->apply_filters, WC_AJAX::checkout, WC_Checkout->process_checkout, WC_Checkout->process_order_payment, WC_Gateway_BACS->process_payment, WC_Order->update_status, WC_Order->save, WC_Order->status_transition, do_action('woocommerce_order_status_pending_to_on-hold'), WP_Hook->do_action, WP_Hook->apply_filters, WC_Emails::send_transactional_email, do_action_ref_array('woocommerce_order_status_pending_to_on-hold_notification'), WP_Hook->do_action, WP_Hook->apply_filters, WC_Email_Customer_On_Hold_Order->trigger, WC_Email->get_content, WC_Email_Customer_On_Hold_Order->get_content_html, wc_get_template_html, wc_get_template, include('/plugins/kadence-woocommerce-email-designer/templates/woo/emails/customer-on-hold-order.php'), do_action('woocommerce_email_customer_details'), WP_Hook->do_action, WP_Hook->apply_filters, WC_Emails->email_addresses, wc_get_template, include('/plugins/kadence-woocommerce-email-designer/templates/woo/emails/email-addresses.php'), WC_Order->get_formatted_billing_address, apply_filters('woocommerce_order_formatted_billing_address'), WP_Hook->apply_filters, njengah_add_vat_formatted_billing_address, WC_Abstract_Legacy_Order->__get, wc_doing_it_wrong. This message was added in version 3.0.

Do you have any idea of what it could be? How can I fix this? Thank you so much in advance!

@jicao
Copy link

jicao commented Jun 22, 2021

Hello,

I found the solution here about
billing_vat was called incorrectly

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;
}

Hope this will help others :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment