Skip to content

Instantly share code, notes, and snippets.

@SiR-DanieL
Last active August 17, 2017 13:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save SiR-DanieL/2147d4b9d69d5fae9a2f to your computer and use it in GitHub Desktop.
Save SiR-DanieL/2147d4b9d69d5fae9a2f to your computer and use it in GitHub Desktop.
Adds VAT and SSN fields in WooCommerce workflow
// Add VAT and SSN fields in billing address display
add_filter( 'woocommerce_order_formatted_billing_address', 'custom_add_vat_ssn_formatted_billing_address', 10, 2 );
function custom_add_vat_ssn_formatted_billing_address( $fields, $order ) {
$fields['vat'] = $order->billing_vat;
$fields['ssn'] = $order->billing_ssn;
return $fields;
}
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 );
$fields['ssn'] = get_user_meta( $customer_id, 'billing_ssn', true );
}
return $fields;
}
add_filter( 'woocommerce_address_to_edit', 'custom_address_to_edit' );
function custom_address_to_edit( $address ) {
global $wp_query;
if ( isset( $wp_query->query_vars['edit-address'] ) && $wp_query->query_vars['edit-address'] != 'billing' ) {
return $address;
}
if ( ! isset( $address['billing_vat'] ) ) {
$address['billing_vat'] = array(
'label' => __( 'VAT', 'your-domain' ),
'placeholder' => _x( 'VAT', 'placeholder', 'your-domain' ),
'required' => true, //change to false if you do not need this field to be required
'class' => array( 'form-row-first' ),
'value' => get_user_meta( get_current_user_id(), 'billing_vat', true )
);
}
if ( ! isset( $address['billing_ssn'] ) ) {
$address['billing_ssn'] = array(
'label' => __( 'SSN', 'your-domain' ),
'placeholder' => _x( 'SSN', 'placeholder', 'your-domain' ),
'required' => true, //change to false if you do not need this field to be required
'class' => array( 'form-row-first' ),
'value' => get_user_meta( get_current_user_id(), 'billing_ssn', true )
);
}
return $address;
}
add_filter( 'woocommerce_formatted_address_replacements', 'custom_formatted_address_replacements', 10, 2 );
function custom_formatted_address_replacements( $address, $args ) {
$address['{vat}'] = '';
$address['{ssn}'] = '';
if ( ! empty( $args['vat'] ) ) {
$address['{vat}'] = __( 'VAT', 'your-domain' ) . ' ' . $args['vat'];
}
if ( ! empty( $args['ssn'] ) ) {
$address['{ssn}'] = __( 'SSN', 'your-domain' ) . ' ' . strtoupper( $args['ssn'] );
}
return $address;
}
add_filter( 'woocommerce_localisation_address_formats', 'custom_localisation_address_format' );
function custom_localisation_address_format( $formats ) {
$formats['IT'] .= "\n\n{vat}\n{ssn}";
return $formats;
}
add_filter( 'woocommerce_admin_billing_fields', 'custom_admin_billing_fields' );
function custom_admin_billing_fields( $fields ) {
$fields['vat'] = array(
'label' => __( 'VAT', 'your-domain' ),
'show' => true
);
$fields['ssn'] = array(
'label' => __( 'SSN', 'your-domain' ),
'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 );
$customer_data['billing_ssn'] = get_user_meta( $_POST['user_id'], 'billing_ssn', true );
return $customer_data;
}
add_filter( 'woocommerce_customer_meta_fields', 'custom_customer_meta_fields' );
function custom_customer_meta_fields( $fields ) {
$fields['billing']['fields']['billing_vat'] = array(
'label' => __( 'VAT', 'your-domain' )
);
$fields['billing']['fields']['billing_ssn'] = array(
'label' => __( 'SSN', 'your-domain' )
);
return $fields;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment