Title: Conditional fields?
Last modified: June 12, 2019

---

# Conditional fields?

 *  Resolved [Woozy Face](https://wordpress.org/support/users/shaady4/)
 * (@shaady4)
 * [6 years, 11 months ago](https://wordpress.org/support/topic/conditional-fields-34/)
 * Hi there,
 * I’ve created some extra checkout fields with Checkout field editor, now I want
   some fields to be conditional based on product categories. How can I doe this?
 * for example, if someone orders a internet package there is no need to ask about
   number porting details.
 * Thanks!
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fconditional-fields-34%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

Viewing 4 replies - 1 through 4 (of 4 total)

 *  [laceyrod](https://wordpress.org/support/users/laceyrod/)
 * (@laceyrod)
 * Automattic Happiness Engineer
 * [6 years, 11 months ago](https://wordpress.org/support/topic/conditional-fields-34/#post-11631834)
 * Howdy 😛
 * [Checkout Field Editor](https://woocommerce.com/products/woocommerce-checkout-field-editor/)
   does not support conditional logic like this right out of the box, but it is 
   possible with some extensive customizations. I would recommend taking a look 
   at this article to see if it’s something you’d be comfortable doing yourself:
   [https://calebburks.com/conditionally-showhide-woocommerce-checkout-fields/](https://calebburks.com/conditionally-showhide-woocommerce-checkout-fields/)
 * If you do choose to go this route, I would recommend making sure that your site
   is backed up first, since it looks like this snippet was created a few years 
   ago.
 * The author of this article, a colleague of mine, has also turned this code into
   a mini plugin, if you’d like to test that out: [https://gist.github.com/WPprodigy/b0479625b14b4b7ee789](https://gist.github.com/WPprodigy/b0479625b14b4b7ee789)
 * If these options don’t work out, then I would recommend contacting Codeable or
   one of our WooExperts for paid assistance: [https://woocommerce.com/customizations/](https://woocommerce.com/customizations/)
 * Hope this helps!
 *  Thread Starter [Woozy Face](https://wordpress.org/support/users/shaady4/)
 * (@shaady4)
 * [6 years, 11 months ago](https://wordpress.org/support/topic/conditional-fields-34/#post-11631956)
 * Hi there, Thanks for the quickest reply ever!
 * I’ve tried something but if I activate the plugin it gets an fatal error Could
   you please give me a hand and help me to get this working?
 *     ```
       <?php
       /**
        * Plugin Name: Conditional WooCommerce Checkout Fields
        * Description: Adds the abilitiy to conditionally show / hide checkout fields.
        * Version: 1.0
        * Author: EasyComp Zeeland
        * Author URI: http://easycompzeeland.nl
        */
   
       /**
        * Check if a specific product category is in the cart
        */
       function wc_ninja_category_is_in_the_cart( $categories ) {
       	// Products currently in the cart
       	$cart_ids = array();
   
       	// Categories currently in the cart
       	$cart_categories = array();
   
       	// Find each product in the cart and add it to the $cart_ids array
       	foreach( WC()->cart->get_cart() as $cart_item_key => $values ) {
       		$cart_product = $values['data'];
       		$cart_ids[]   = $cart_product->id;
       	}
   
       	// Connect the products in the cart w/ their categories
       	foreach( $cart_ids as $id ) {
       		$products_categories = get_the_terms( $id, 'product_cat' );
   
       		// Loop through each product category and add it to our $cart_categories array
       		foreach ( $products_categories as $products_category ) {
       			$cart_categories[] = $products_category->slug;
       		}
       	}
   
       	// If one of the special categories are in the cart, return true.
       	if ( ! empty( array_intersect( $categories, $cart_categories ) ) ) {
       		return true;
       	} else {
       		return false;
       	}
       }
   
       /**
        * Conditionally remove checkout fields
        */
       function wc_ninja_remove_checkout_field( $fields ) {
       		$categories  = array( 'mobiel', 'online' );
   
       	// If mobile is in cart
       	if ( wc_ninja_category_is_in_the_cart( $categories ) ) {
       		unset( $fields['additional']['vaststart'] );
       	}
       	if ( wc_ninja_category_is_in_the_cart( $categories ) ) {
       		unset( $fields['additional']['providervast'] );
       	}
       	if ( wc_ninja_category_is_in_the_cart( $categories ) ) {
       		unset( $fields['additional']['klantnummervast'] );
       	}
       	if ( wc_ninja_category_is_in_the_cart( $categories ) ) {
       		unset( $fields['additional']['aansluitingvast'] );
       	}
       	if ( wc_ninja_category_is_in_the_cart( $categories ) ) {
       		unset( $fields['additional']['einddatumvast'] );
       	}
   
       	return $fields;
       }
       add_filter( 'woocommerce_checkout_fields' , 'wc_ninja_remove_checkout_field' );
   
       /**
        * Conditionally remove checkout fields vast
        */
       function wc_ninja_remove_checkout_field( $fields ) {
       	$categories  = array( 'voip', 'online' );
   
       	// If mobile is in cart
       	if ( wc_ninja_category_is_in_the_cart( $categories ) ) {
       		unset( $fields['additional']['nummerbehoudmobielstart'] );
       	}
       	if ( wc_ninja_category_is_in_the_cart( $categories ) ) {
       		unset( $fields['additional']['behoudennrmobiel'] );
       	}
       	if ( wc_ninja_category_is_in_the_cart( $categories ) ) {
       		unset( $fields['additional']['providermobiel'] );
       	}
       	if ( wc_ninja_category_is_in_the_cart( $categories ) ) {
       		unset( $fields['additional']['klantnummermobiel'] );
       	}
       	if ( wc_ninja_category_is_in_the_cart( $categories ) ) {
       		unset( $fields['additional']['aansluitingmobiel'] );
       	}
       }
       add_filter( 'woocommerce_checkout_fields' , 'wc_ninja_remove_checkout_field' );
       ```
   
 * If you get this working I would like to buy you a cup of coffee or tea! Just 
   hit me up with a donation link!
 *  Plugin Support [Fernando a11n](https://wordpress.org/support/users/fhaps/)
 * (@fhaps)
 * Automattic Happiness Engineer
 * [6 years, 11 months ago](https://wordpress.org/support/topic/conditional-fields-34/#post-11641735)
 * Hi [@shaady4](https://wordpress.org/support/users/shaady4/),
 * Coding or reviewing code goes beyond the scope of support we provide in this 
   forum, so I’m afraid we can’t offer much help on this front.
 * If you do need further coding help, our suggestion would be to get in touch with
   a developer who can help you review this. The customizations page suggested in
   the reply above is a good place to start.
 *  Thread Starter [Woozy Face](https://wordpress.org/support/users/shaady4/)
 * (@shaady4)
 * [6 years, 11 months ago](https://wordpress.org/support/topic/conditional-fields-34/#post-11642217)
 * I already got it working

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Conditional fields?’ is closed to new replies.

 * ![](https://ps.w.org/woocommerce/assets/icon.svg?rev=3234504)
 * [WooCommerce](https://wordpress.org/plugins/woocommerce/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/woocommerce/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/woocommerce/)
 * [Active Topics](https://wordpress.org/support/plugin/woocommerce/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/woocommerce/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/woocommerce/reviews/)

 * 4 replies
 * 3 participants
 * Last reply from: [Woozy Face](https://wordpress.org/support/users/shaady4/)
 * Last activity: [6 years, 11 months ago](https://wordpress.org/support/topic/conditional-fields-34/#post-11642217)
 * Status: resolved