0

There's a need to add new field (e.g. vat-number) under the company name in the woocommerce plugin. What is the easiest / most reasonable way to achieve this? I wouldn't want to make copies from template files due to this small change nor install any plugin to avoid broken system (if the author prevents to update it at some point). I'll vote up all the answers that provide any useful information :-) Thanks!

1
  • I forgot to mention that the plugin is woocommerce, sorry guys. I guess the answers below are still valid.
    – jahau
    Commented Jan 22, 2018 at 10:28

4 Answers 4

1

The other guys assumed that you wanted to add your own store's VAT number, but I read it at you want the customer to add their VAT number when they create an account or order as guest.

This is certainly possible, and WooCommerce does have a whole mechanism to hook into adding additional customer account fields. However, it's quite in-depth and not for the faint-hearted. I found this great tutorial that helped me get to grips with it. It's quite long, but it does take you through every place where the additional field(s) need to be registered.

https://iconicwp.com/blog/the-ultimate-guide-to-adding-custom-woocommerce-user-account-fields/

Hope that helps

2

To add a new field in Settings pages like General, try adding the following code in functions.php (or in a file required to it).

The callback function needs to output the appropriate html input:

function myprefix_setting_callback_function() {

    $value = get_option( 'vat_number_id' );
    echo '<input type="text" id="vat_number_id" name="vat_number_id" value="' . $value . '" />';

}

This function can be used to add extra settings fields to the default WP settings pages:

function myprefix_settings_register_fields() {

    register_setting( 'general', 'vat_number_id', 'esc_attr' );

    add_settings_field(
        'vat_number_id',
        '<label for="vat_number">'.__( 'VAT number' , 'your_theme' ).'</label>' ,
        'myprefix_setting_callback_function',
        'general'
    );

}
add_filter( 'admin_init', 'myprefix_settings_register_fields' );

Here you can find all the documentation for adding settings fields.

In your theme (header.php, footer.php, etc.) you can use:

<?php echo get_option( 'vat_number_id' ); ?>

P.S. In order to change the position to the input field created, you must act with caution using jQuery.

1

It's hard to tell without knowing where the company name is (for example in header.php) and how it's coded.

Assuming that it is in header.php, you can add it to the php with echo and with standard html.

You can also add a widgetized area to the header by adding this to your header.php file:

<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar("Name of Widgetized Area") ) : ?>

and this to functions.php:

if ( function_exists('register_sidebar') ) register_sidebar(array( 'name' => 'Name of Widgetized Area',

'before_widget' => '<div class = "widgetizedArea">',
'after_widget' => '</div>',
'before_title' => '<h3>',
'after_title' => '</h3>',

) );

You can remove the space before 'before widget', I couldn't get the code to display properly without it. You can see the full explanation of this here.

Also I strongly recommend that you create a child theme if you modify the theme's php files, otherwise your changes might be lost at the next theme update.

1

This is what I was looking for: https://tutel.me/c/programming/questions/43335335/woocommerce+customer+billing+address

Still, I need to reorder the fields (VAT number should be after company name and not at the bottom). Do you know how to achieve this? Thanks.

p.s. I voted up your answers since those are valid too (I don't have yet enough reputation that you could see the effect).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.