Luuk Van Dongen Luuk Van Dongen -4 years ago 82
PHP Question

Add field to wordpress billing fields

I'd like to add 2 custom fields to the user billing information in WooCommerce, one for capturing their VAT number and one for the Chamber of Commerce number. These fields need to be displayed during checkout, on the my account > billing address page and also on the WP Admin on the user page so the admin of the website/webshop can check for these values.

I prefer not to use a plugin but to use the child theme's functions.php.

Can anyone please help me with this problem? I looked around on the Wordpress Stack exchange but couldn't find a specific and up-to-date solution for my problem. Also I read the Woocommerce documentation but there it's not explained how to show the custom billing fields on the user admin page in the backend.

Thank you very much in advance!

Answer Source

You can achieve this by using Woocommerce filter woocommerce_checkout_fields.

Here is the code example.

add_filter( 'woocommerce_checkout_fields','checkout_extra_fields');
function checkout_extra_fields($fields){
    $fields['billing']['vat_number'] = array(       
        'label'       => __('VAT number', 'my-slug'),
        'placeholder' => __('VAT number', 'my-slug'),
        'required'    => false,
        'clear'       => false,
        'type'        => 'text',

    $fields['billing']['commerce_number'] = array(      
        'label'       => __('Commerce number', 'my-slug'),
        'placeholder' => __('Commerce number', 'my-slug'),
        'required'    => false,
        'clear'       => false,
        'type'        => 'text'

    return $fields;

Also, You can get the saved values from backend by this code

$extra_fileds_vat_number    =   get_post_meta( wf_get_order_id($order),'_vat_number',1);
$extra_fileds_commerce_number   =   get_post_meta( wf_get_order_id($order),'_commerce_number',1);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download