Add a checkout field in WooCommerce only for specific roles

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.


More Posts That You Might Like…


32 responses to “Add a checkout field in WooCommerce only for specific roles”

  1. 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.

    1. Thanks Cecil! Really appreciated!

  2. 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

    1. Hi sidharh, sorry for the late reply.

      I’ll write an article about that this week! Thanks for suggesting.

  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“. […]

  4. 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?

    1. 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!

  5. 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.

    1. 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

  6. What about removing all of the address fields for a specific user role?

    1. The snippet is the same, you need only to edit the indexes. You can find the list of all the available indexes in this article.

  7. sorry but i dont see any php code for adding to function.php/ Where is?

    1. Hi Alex, it’s right after the second image in the article, here is the directly link: https://gist.github.com/woogist/4fb6c1e6e817dc874fc3#file-functions-php

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

    1. How did you add the field Andrea?
      The article is about checkout fields, I’m not sure what code you are using for billing address fields in the user account page.

      1. yes, i add the field. how i can desappear the field “vat” also in my account page —> billing address?

        1. Yes, I asked how did you add the field? Did you use any other code except the one from this article?

  9. Nice post about WooCommerce Plugin. I would like to share one Woo-commerce Plugins named WC Checkout Fields Editor.

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

    1. 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.

      1. Sally Lynn MacDonald Avatar
        Sally Lynn MacDonald

        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.

        1. Hello Sally,
          yes, of course you can! Please paste your code in a pastebin or gist file and write its URL in your next comment.

          I’ll have a look!

  11. 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?

    1. Hi Liz,
      Yes you can. I found this post on StackOverflow that explains how to get the chosen shipping method: http://stackoverflow.com/questions/22383621/woocommerce-2-1-detect-chosen-shipping-method

      It could work, but only for returning customers because they already have a chosen shipping method stored in their data.

      For new customers, it would be possible via jQuery/Javascript.

  12. […] 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 […]

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

  14. 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?

  15. Hi Nicola,

    great snippet.
    I’m have one sugestion, because don’t find in any place these snippet for woo

    Hide $field based on $payment_method chosen

    Thank you

  16. hello, I am trying to use this snippet, but after i add it to my my functions.php, i get that blank screen you get when something is working right.

    these are the details:
    using User Role Editor, I created “Customer Success Team”, it shows up as “customer_success_team”
    using Checkout Field Editor, I created “Ship to PCC Canadian Office”, it shows up as “ship-to-pcc-canadian-office”
    this is an Additional Checkout Field, which shows up as ‘additional’

    here is how i amended the snippet:
    function custom_override_checkout_fields( $fields ) {

    if ( ! current_user_can( 'customer_success_team' ) && isset( $fields['additional']['ship-to-pcc-canadian-office'] ) ) {
    unset( $fields['additional']['ship-to-pcc-canadian-office'] );
    }

    return $fields;

    }
    add_filter( ‘woocommerce_checkout_fields’ , ‘custom_override_checkout_fields’ );

    I can’t figure out what could be wrong, does anyone see anything amiss?

  17. how this code works with more than only one ‘wholesalers’ user? i want to display this checkout field for 3 users. thanks for reply!

    1. Hello signal,
      The code affects all users with the role wholesaler.

Leave a Reply

Categories

Newsletter

Receive new articles from this blog directly in your inbox!

No spam guaranteed!

Blog at WordPress.com.

%d bloggers like this: