Skip to content

Instantly share code, notes, and snippets.

@SiR-DanieL
Created April 27, 2015 06:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SiR-DanieL/fdc05fb78a7ec7c74745 to your computer and use it in GitHub Desktop.
Save SiR-DanieL/fdc05fb78a7ec7c74745 to your computer and use it in GitHub Desktop.
/**
* 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