Learn in this tutorial how to hide any field on the checkout page in WooCommerce for a specific product category with a simple code snippet.
In a previous tutorial you learn how you can hide any field on the checkout page in WooCommerce for a specific product. This is fine for one-off products, but if you want it to apply to multiple products it may be more interesting to hide them for one (or more) product category(ies).
Thanks to the woocommerce_checkout_fields
filter of WooCommerce you can achieve this easily.
Here’s how to do it.
Snippet to hide a WooCommerce checkout field in a specific product category
Add the following code at the end of functions.php or in your functionality plugin:
To see this and another 919 code snippets of this website, login or subscribe here.
In this case you use the woocommerce_checkout_fields
filter together with a conditional where you indicate that when the product category is “books” remove the field in the second line of the address (billing_user_type
).
This field is not a native field of WooCommerce but it is added by the Spanish Autónomos plugin, which allows you to choose between individual or professional to apply the Spanish IRPF tax or not. So with this example you can see that if you know the name of the field you can apply this tutorial to any of it.
I leave you the list of WooCommerce default fields and how to remove them so you can customize the snippet above to your liking:
// Billing fields
unset( $fields['billing']['billing_company'] );
unset( $fields['billing']['billing_email'] );
unset( $fields['billing']['billing_phone'] );
unset( $fields['billing']['billing_state'] );
unset( $fields['billing']['billing_first_name'] );
unset( $fields['billing']['billing_last_name'] );
unset( $fields['billing']['billing_address_1'] );
unset( $fields['billing']['billing_address_2'] );
unset( $fields['billing']['billing_city'] );
unset( $fields['billing']['billing_postcode'] );
// Shipping fields
unset( $fields['shipping']]['shipping_company'] );
unset( $fields['shipping']]['shipping_phone'] );
unset( $fields['shipping']['shipping_state'] );
unset( $fields['shipping']['shipping_first_name'] );
unset( $fields['shipping']['shipping_last_name'] );
unset( $fields['shipping']['shipping_address_1'] );
unset( $fields['shipping']['shipping_address_2'] );
unset( $fields['shipping']]['shipping_city'] );
unset( $fields['shipping']['shipping_postcode'] );
// Other
unset( $fields['order']['order_comments'] );
Obviously you can also change the product category, even add several by separating them with a comma.
Conclusion
As you can see, with a few lines of code you can hide the field you want on the WooCommerce checkout page only for a specific product category (or several if you prefer).
If you have any question, please leave it in the comments. And if you want to give me a suggestion for future snippets, please send it through the contact form.
Benefits of being a subscriber. 🙂