Skip to content

Instantly share code, notes, and snippets.

@SiR-DanieL
Created September 29, 2014 07:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SiR-DanieL/b87314a6267b45c41302 to your computer and use it in GitHub Desktop.
Save SiR-DanieL/b87314a6267b45c41302 to your computer and use it in GitHub Desktop.
add woocommerce customer vat and ssn 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' )
);
}
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' )
);
}
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_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