Sometimes you want to show a field on the checkout just for some users.

In this tutorial I’ll show you how to do this. In the example I’ll add the field for the VAT number only for the user role Wholesalers.

For simplicity, I’ll use Checkout Field Editor to add the field and then with a custom code I’ll remove it from the checkout page for non Wholesalers users.

How to do it….

First of all, install and activate Checkout Field Editor. This is not a required step, if you do not have the plugin and do not want to buy it you can add the field by yourself following this tutorial.

By default WordPress and WooCommerce do not have the role Wholesalers for users, so you need to add it. You can do so using the popular plugin User Role Editor. Obviously you can also use an existing role instead of creating a new one if you prefer.

Then add the field Billing VAT in the checkout:

checkout-field

This field will now show on the checkout page, required for all users. We need to remove it for everybody except for Wholesalers. To do so, add this code to your functions.php file located in wp-content/themes/your-theme-name/:

Now the field will show only for users with the role Wholesalers and it will be required.

Note: be sure to use the Role ID in the code current_user_can( 'wholesalers' ) or it will not work.

28 replies
  1. Cecil
    Cecil says:

    I’ve been exploring for a little bit for any high quality articles or weblog posts in this kind
    of house . Exploring in Yahoo I finally stumbled upon this web site.

    Reading this information So i’m satisfied to express that I’ve a very
    just right uncanny feeling I discovered just what I needed.

    I most certainly will make certain to do not forget this website and give it a look on a constant
    basis.

    Reply
  2. sidharth
    sidharth says:

    Hi Nicola ,

    Can you provide a article/code so that i can add custom checkout fields in WooCommerce per product basis using Checkout Field Editor plugin?

    Thanks

    Reply
  3. Eric
    Eric says:

    This works great on custom fields you create in the Checkout Field Editor, but it crashes my site when I try to hide any of the default fields in WooCommerce (such as company name “billing_company” or city “billing_city”), any idea how I can hide those fields?

    Reply
    • Nicola Mustone
      Nicola Mustone says:

      Hi, in that case you need to also be sure that the fields are not required and remove the checks that WooCommerce has on them.

      You would need to hire an expert WooCommerce developer in order to do this if you don’t know how to do it by yourself.

      Thanks!

      Reply
  4. Dzulhelmi
    Dzulhelmi says:

    How to upload file during checkout at WooCommerce for certain product? I am selling membership and for this one product, we need customer to upload a form and an ID during the checkout. I checked all WooCommerce extension that enable add custom field for WooCommerce checkout but none of them can add file upload feature.

    Reply
    • Nicola Mustone
      Nicola Mustone says:

      Hi,
      this is tricky because WordPress handles the upload itself and you should write code to “ask” him to handle your field on the checkout.

      I don’t know if i’ll write an article about this, i’m sorry.

      If you require assistance with this and would like to explore this further you can seek help from an Affiliated Woo Worker or post a job on Codeable, I have provided links to these below.

      Affiliated Woo Workers – https://www.woothemes.com/affiliated-woo-workers/
      Codeable – https://codeable.io/

      Thanks! Have a good day

      Reply
  5. Andrea
    Andrea says:

    i proved that, but the field “vat” don’t disappear in billing address in my account page? how i can hide it? thanks

    Reply
  6. Kevin
    Kevin says:

    For some reason I’m unable to get the above code to work. Are there any changes needed for WordPress 4.5?

    Reply
    • Nicola Mustone
      Nicola Mustone says:

      Hi Kevin,
      No there’s no change needed for WP 4.5. I’m not sure what could cause it to not work for you.

      Try with a different theme, maybe there’s something that overrides it. Or if you have any other plugin that handles WooCommerce checkout fields try to disable it.

      Reply
      • Sally Lynn MacDonald
        Sally Lynn MacDonald says:

        I found and am loving this article! I was able to add the field using the Woo plugin, but your code to hide it isn’t working for me. I’m a bit of a novice, but I feel like i’ve got it right. Any ideas? Can I share my snippet with you? It’s very generous of you to share in the first place. Just trying to make it work.

        Reply
  7. Liz
    Liz says:

    Hi Nicola, is it possible to add checkout field for specific shipping method, i.e. after the customer has chosen which shipping method he wants?

    Reply
  8. Uschi
    Uschi says:

    Hi,
    if I only want to unset an Array entry – for example BG (Bulgaria) in shipping_Country if user is not in Group Distributor I thought it would be this:

    function custom_override_checkout_fields( $fields ) {
    if ( ! current_user_can( ‘distributor’ ) ) {
    unset($fields[‘shipping’][‘schipping_country’][‘BG’]);
    }
    return $fields;
    }
    add_filter( ‘woocommerce_checkout_fields’ , ‘custom_override_checkout_fields’ );

    But it does not work. I get no error but also Bulgaria is in my dropdown-list of available shipping countries for everybody. Please can you help?

    Reply

Trackbacks & Pingbacks

  1. […] Add a checkout field in WooCommerce only for specific roles […]

  2. […] is off with my fields as they’re still showing up for all users. I followed the tutorial here to hide the fields. Apologies if the formatting of the code is off. The editor didn’t accept […]

  3. […] You ask, i write! This post has been asked by sidharh in my other article about how to “Add a checkout field in WooCommerce only for specific roles“. […]

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

You have to agree to the comment policy.

This site uses Akismet to reduce spam. Learn how your comment data is processed.