Created
March 15, 2022 12:53
-
-
Save danielcharrua/41939ec31fee57e7a4fa9f137715500e to your computer and use it in GitHub Desktop.
Add NIF/CIF to WooCommerce (only as example)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Only used for example purposes, for also incluidng VAT validation and calculation | |
* I recommend using the plugin https://wordpress.org/plugins/wc-apg-nifcifnie-field/ | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; // Exit if accessed directly. | |
} | |
/* | |
* Add VAT field to | |
* My Account - Edit Form - Billing fields | |
* Checkout - Edit Form - Billing Fields | |
* | |
* https://gist.github.com/Miq3l/4870eecc80bb6a1f537c575e848ef2b5 | |
* Source: https://docs.woothemes.com/document/tutorial-customising-checkout-fields-using-actions-and-filters | |
*/ | |
function add_vat_to_billing_fields($billing_fields){ | |
$billing_fields['billing_vat'] = array( | |
'type' => 'text', | |
'label' => __('NIF/CIF', 'woocommerce' ), | |
'class' => array('form-row-wide'), | |
'required' => false, | |
'clear' => true, | |
'priority' => 31 | |
); | |
return $billing_fields; | |
} | |
add_filter('woocommerce_billing_fields' , 'add_vat_to_billing_fields'); | |
/* | |
* Add VAT when printing billing address on: | |
* (1) My account | |
* (2) Checkout - Order Received (printed after having completed checkout and in other parts where this function is used) | |
*/ | |
// (1) 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 | |
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; | |
} | |
/* | |
* Add VAT form meta field on Wordpress Admin -> User Profile -> Billing | |
*/ | |
add_filter( 'woocommerce_customer_meta_fields', 'custom_customer_meta_fields' ); | |
function custom_customer_meta_fields( $fields ) { | |
$fields['billing']['fields']['billing_vat'] = array( | |
'label' => __( 'NIF/CIF', 'woocommerce' ) | |
); | |
return $fields; | |
} | |
/* | |
* Add VAT form meta field on Wordpress Admin -> Order -> Billing Edit Form | |
*/ | |
add_filter( 'woocommerce_admin_billing_fields', 'custom_admin_billing_fields' ); | |
function custom_admin_billing_fields( $fields ) { | |
$fields['vat'] = array( | |
'label' => __( 'NIF/CIF', 'woocommerce' ), | |
'show' => true | |
); | |
return $fields; | |
} | |
/* | |
* Copy VAT user meta field 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 "Usar la dirección de facturación" (ajax request) | |
* Both cases, selecting a new order and 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', '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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment