• Resolved Woozy Face

    (@shaady4)


    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 to see the link]

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

    (@laceyrod)

    Automattic Happiness Engineer

    Howdy 😛

    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/

    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

    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/

    Hope this helps!

    Thread Starter Woozy Face

    (@shaady4)

    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

    (@fhaps)

    Automattic Happiness Engineer

    Hi @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

    (@shaady4)

    I already got it working

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

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