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“.
This time instead, we will hide checkout fields based on the products contained into your cart. you asked to add them instead to hide them, but it’s a lot quicker to add them all, and then hide them when they are not needed.
Let’s start!
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 purchase it you can add the field by yourself following this tutorial.
Create all the new fields you need for the shipping for or the billing for or also additional fields. Also, be sure to have a list of all the products ID you need to use in your code to hide specific fields. If you don’t know how to get the product ID, read this tutorial.
Now you are ready to write some code. Open your functions.php file located in wp-content/themes/your-theme-name/ and add this code at the end of the file:
In this code, i hide the billing and shipping company field only if the product ID 70 is in the customer cart.
This is a very simple rule, obviously you can create very complex rules too, like hide fields only if there’s a product, but not another one, or hide a field if there are 2 or more products into the cart or anything else you can think about.
Leave a Reply