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:
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.
Leave a Reply