Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/**
* Hides checkout fields based on the products in the cart
*
* @param array $fields
* @return array
*/
function conditional_checkout_fields_products( $fields ) {
$cart = WC()->cart->get_cart();
$has_tag = true;
foreach ( $cart as $item_key => $values ) {
$product = $values['data'];
if ( ! has_term( 'your-tag', 'product_tag', $product->id ) ) {
$has_term = false;
break;
}
}
if ( $has_term ) {
unset( $fields['billing']['billing_company'] );
}
return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'conditional_checkout_fields_products' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment