WebDoze Logo

Plugin to Add Custom Tax/VAT Field On WooCommerce Checkout

Table Of Contents

Share Article
woocommerce VAT cF

A while back I did a shop on WooCommerce and I needed to add the tax id on the checkout page and also to be displayed on the order page or in the email. There are a few tutorials over the internet that can help you with this even a paid plugin if you want validation and more advanced configuration features. The below tutorials have been used to build a plugin that is inserting a Vat field in the checkout page of WooComerce and also is displaying it in the order page:

Because there is the risk that the code to be deleted if I implement it directly in the WooCommerce I preferred to create a plugin. In this way I avoid losing the field when the plugin is created. Creating the plugin is not to hard and it is also clean. Below is the code that I have used to add the field in the checkout:

add_filter( 'woocommerce_checkout_fields' , array( $this,'VAT_override_checkout_fields' ));

	public function VAT_override_checkout_fields( $fields ) {
				$fields['billing']['VAT_cui'] = array(
				'label'     => __('VAT', 'woocommerce'),
				'placeholder'   => _x('VAT', 'placeholder', 'woocommerce'),
				'required'  => true,
				'class'     => array('form-row-wide'),
				'clear'     => true
				);

				return $fields;
}

Below is the code to add the code in the order page:

add_action( 'woocommerce_admin_order_data_after_billing_address',  array( $this,'VAT_custom_checkout_field_order_meta_keys' );
	public function VAT_custom_checkout_field_order_meta_keys( $order ) {
		echo "<p><strong>VAT:</strong>" .
		$order->order_custom_fields['_VAT_cui'][0] . "</p>";

}

The script and plugin don’t update the user meta it is only inserting the VAT in the order. Every time a new command will be made the file will be there together with the VAT and if the customer would want to change it can be changed.

Below is the plugin that can be used to add the VAT mandatory custom field in the checkout page of Woocommerce, it can be installed like any other plugin but to work be sure that WooCommerce plugin is installed.

To download it just the article with one of the below links.

[sociallocker]

DOWNLOAD

[/sociallocker]

== Installation ==

1. Upload the entire ‘woocommerce-VAT’ folder to the ‘/wp-content/plugins/’ directory
2. Activate the plugin through the ‘Plugins’ menu in WordPress

== Changelog ==

= 1.0.0 =
* Initial Release

Become a CloudPanel Expert

This course will teach you everything you need to know about web server management, from installation to backup and security.
Leave a Reply

Your email address will not be published. Required fields are marked *

  1. elies pavlou says:

    Hi,

    with woocommerce 2.2.6 the VAT field is visible on checkout but on the order view in woocommerce and on hte email that you receive with the order it is not shown.

    Regards,

    Elies

  2. Manuel says:

    I noticed that in the last version of woocommerce, this plugin doesn’t work at all. As @sergio pointed, in the order page the VAT doesn’t appear… Is there any compatibility issues? Thanks!

    • admin says:

      Hi,

      OK. I will check and see what is happening. Thanks a lot for letting me know!

      BR,
      Dragos

  3. Sergio says:

    Hi.
    I put this plugin and activate it.
    In the check out process can write the vat number.
    After, this vat number doesnt appears in orders, emails, etc..

    do you know why?

    Thank you.

  4. calcaneus says:

    i fixed it:


    public function VAT_custom_checkout_field_order_meta_keys( $order ) {
    $my_order_meta = get_post_custom( $order->id );
    echo "UmsatzsteuerID: ".$my_order_meta['_VAT_cui'][0]."";
    }

    • admin says:

      Hi,

      I have updated the code with the correct specifications. Thanks for the hint.

      Dragos

  5. calcaneus says:

    pretty cool your plugin, thanks a lot.
    unfortunately i get an error in woocommerce -> orders -> orderdetail: Fatal error: Cannot use string offset as an array in /pathtowoocommerce/wp-content/plugins/woocommerce-vat-checkout-add/woocommerce-vat.php on line 66