checkout

How to Hide Fields on Checkout Page for specific Product in the Cart in Woocommerce

checkout

Today one of our client wanted to hide the specific field on the checkout page for a specific product contained into the cart in Woocommerce. So here I show you the snippet that I used to remove field on checkout page based on the product.

Open your functions.php file located in wp-content/themes/your-theme-name/ and add the following code at the end of the file or you can create your own plugin to add this snippet.

/**
* Hides checkout fields based on the products in the cart
*
* @param  array $fields
* @return array
*/
function remove_fields_forspecific_products( $fields ) {
$cart = WC()->cart->get_cart();
foreach ( $cart as $item_key => $values ) {
$product = $values['data'];
if ( $product->id == 144 ) {
unset( $fields['billing']['billing_company'] );
unset( $fields['shipping']['shipping_company'] );
}
}
return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'remove_fields_forspecific_products' );

In above snippet, I removed the billing and shipping company field only if the product ID 144 is in the customer cart.

if you want to do this for multiple products then use all products IDs, see example below

if ( $product->id == 144 || $product->id == 155 )

This snippet will remove the field for the product id 144 and product id 155.

The following two tabs change content below.

Wasim Sama

Wasim Sama is a passionate blogger and experienced web developer. Founder of wasimsama.com and co-founder of TechnicalMail.com.

One thought on “How to Hide Fields on Checkout Page for specific Product in the Cart in Woocommerce

  1. Hi,

    Please I have used WooCommerce Checkout Manager plugin in my WordPress website to remove some of checkout fields and to add other fields, now I want to make the new added fields appear/hide based on product id. I used your code snippet in functions.php, but it doesn’t work. How could I solve this problem??

Leave a Reply

Your email address will not be published. Required fields are marked *