Skip to content

Instantly share code, notes, and snippets.

@danielcharrua
Forked from Miq3l/functions.php
Last active September 11, 2020 06:23
Show Gist options
  • Save danielcharrua/050dabc806d5ea702ea0d74cf2876699 to your computer and use it in GitHub Desktop.
Save danielcharrua/050dabc806d5ea702ea0d74cf2876699 to your computer and use it in GitHub Desktop.
Adding VAT to WooCommerce
<?php
/**
* Plugin Name: WooCommerce NIF
* Description: Adds NIF fields to WooCommerce
* Version: 1.0
* Author: Daniel Pereyra Costas
* Author URI: https://charrua.es
* License: GPL v2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: woocommerce-nif
*/
/*
* Add VAT field to WooCommerce billing fields
* Show after Company Name field (priority 30)
*/
add_filter('woocommerce_billing_fields', 'charrua_add_vat_to_billing_fields');
function charrua_add_vat_to_billing_fields($billing_fields)
{
$billing_fields['billing_vat'] = array(
'type' => 'text',
'label' => __('NIF/CIF', 'woocommerce-nif'),
'class' => array('form-row-wide'),
'required' => false,
'clear' => true,
'priority' => 31,
);
return $billing_fields;
}
/*
* Add VAT field value under addresses tab on My Account
* Frontend -> My Account -> Addresses
*/
add_filter('woocommerce_my_account_my_address_formatted_address', 'charrua_my_account_my_address_formatted_address', 10, 3);
function charrua_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;
}
/*
* Add VAT field value when printing billing address on:
* Emails, order details (backend) and customer order details (after purchasing, frontend)
*/
add_filter('woocommerce_order_formatted_billing_address', 'charrua_add_vat_formatted_billing_address', 10, 2);
function charrua_add_vat_formatted_billing_address($fields, $order)
{
$fields['vat'] = $order->billing_vat;
return $fields;
}
/*
* Creating merger VAT variables for printing formatting
* We will use this later when formatting the address on Spain
*/
add_filter('woocommerce_formatted_address_replacements', 'charrua_formatted_address_replacements', 10, 2);
function charrua_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 (Spain) formatting to print the address plus including VAT value.
*/
add_filter('woocommerce_localisation_address_formats', 'charrua_localisation_address_format');
function charrua_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;
}
/*
* Add VAT form meta field on Wordpress Admin -> User Profile -> Billing
*/
add_filter('woocommerce_customer_meta_fields', 'charrua_customer_meta_fields');
function charrua_customer_meta_fields($fields)
{
$fields['billing']['fields']['billing_vat'] = array(
'label' => __('VAT', 'woocommerce'),
);
return $fields;
}
/*
* Copy VAT user meta field value stored, to the order when creating a new order and selecting a client (ajax request)
* Or when using the pencil on the Order Edit Page "Use billing address" (ajax request)
* Both cases, creating a new order and selecting a client or copying billing data from stored client data use the same filter.
*
* On Wordpress Admin -> Order -> New Order or Order Edit Page (same page)
*/
add_filter('woocommerce_ajax_get_customer_details', 'charrua_found_customer_details');
function charrua_found_customer_details($customer_data)
{
$customer_data['billing']['vat'] = get_user_meta($_POST['user_id'], 'billing_vat', true);
return $customer_data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment