Luuk Van Dongen
Luuk Van Dongen

Reputation: 2501

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!

Upvotes: 1

Views: 2831

Answers (2)

Luuk Van Dongen
Luuk Van Dongen

Reputation: 2501

Altough I didn't want to use a plugin and Nishad's answer is absolutely correct I found a free plugin (after a really long search) that does exactly what I want. It's called Flexible Checkout Fields and you can create custom fields that show up on Checkout, Woocommerce my account page and on the WP admin user profile page. I really love it!

So for anyone who encounters the same problem, you might want to try this one!

Upvotes: 1

Nishad Up
Nishad Up

Reputation: 3615

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);

Upvotes: 2

Related Questions