• Resolved Kindfamily

    (@kindfamily)


    Hello,

    I am trying to add checkout fields based on what variation of product the customer has in their cart. I have three types of products.

    For the first (Personal Use File) I would like to display First Name, Last Name, and Email fields (all required)

    For the second (Commercial Use File) I would like to display the same as above, but with a required “Company Name” field.

    For the third (Art Print) I would like to display all of the usual billing and shipping details.

    I currently have this snippet of code:

    add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
    
    function custom_override_checkout_fields( $fields ) {
         unset($fields['billing']['billing_city']);
         unset($fields['billing']['billing_country']);
    	 unset($fields['billing']['billing_address_1']);
    	 unset($fields['billing']['billing_postcode']);	
    	 unset($fields['billing']['billing_state']);	
    	
         return $fields;
    }

    in my functions.php file to remove most of the fields, but this is clunky and doesn’t change based on the customer’s cart. I was hoping to change this so that it’s more adaptive.

    Any help is most appreciated!

    The page I need help with: [log in to see the link]

Viewing 8 replies - 1 through 8 (of 8 total)
  • Hi @kindfamily – if you’d like to continue using custom code like you’ve mentioned above, you can just add a condition for the product, similar to what’s outlined here: https://www.businessbloomer.com/woocommerce-easily-check-product-id-cart/
    Keep in mind that you’ll have to account for a scenario where more than one product is in the cart.

    The easiest route would likely be a plugin, though: https://woocommerce.com/products/conditional-checkout-fields-for-woocommerce/
    I’d suggest looking at that as well.

    Thread Starter Kindfamily

    (@kindfamily)

    Hi @jricketts4 , apologies for the late reply.

    I tried both of the methods outline in the first article you linked. The “old” code was the only one that worked. What exactly would I have to change to the snippet to display different fields? Right now it only displays the placeholder message used in the article.

    As for the official plugin, I’m trying to avoid it due to the high cost haha.

    Hi @kindfamily – no problem! I’m not sure I’ll be a ton of help, but feel free to paste the code you currently have here and I’ll take a look 😁

    Thread Starter Kindfamily

    (@kindfamily)

    Hi Joey, thanks for your willingness to help! The code I am using is the same as what I have in the original post. I’m not sure if that helps at all, but hopefully it will!

    Thanks

    hi @kindfamily – Sorry for the confusion, but I was referring to the altered code that you tried using the articles I mentioned in my first reply. I won’t be able to write this for you, but I can look at what you’ve come up with since then.

    Thread Starter Kindfamily

    (@kindfamily)

    Of course, apologies. Currently, I am using this snippet of code to remove the majority of billing details, which I don’t need for the vast majority of my products.

    add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
    
    function custom_override_checkout_fields( $fields ) {
         unset($fields['billing']['billing_city']);
         unset($fields['billing']['billing_country']);
    	 unset($fields['billing']['billing_address_1']);
    	 unset($fields['billing']['billing_postcode']);	
    	 unset($fields['billing']['billing_state']);	
    	
         return $fields;
    } 

    Then, directly after that, I have this snippet of code that attempts to to reinstate the fields for a specific product (789)

    add_action( 'woocommerce_before_cart', 'bbloomer_find_product_in_cart_alt' );
        
    function bbloomer_find_product_in_cart_alt() {
      
       $product_id = 789;
       $in_cart = false;
      
       foreach( WC()->cart->get_cart() as $cart_item ) {
          $product_in_cart = $cart_item['product_id'];
          if ( $product_in_cart === $product_id ) $in_cart = true;
       }
      
       if ( $in_cart ) {
    	   
         set($fields['billing']['billing_city']);
         set($fields['billing']['billing_country']);
    	 set($fields['billing']['billing_address_1']);
    	 set($fields['billing']['billing_postcode']);	
    	 set($fields['billing']['billing_state']);	
      
       }
    }  

    However, this causes a critical error on the checkout so that I cannot access it

    • This reply was modified 3 years, 3 months ago by Kindfamily.
    Niall a11n

    (@wpniall)

    Hi @kindfamily,

    I’m sorry we missed your last post. Have you been able to resolve this issue?

    If not, this thread involves a fairly complex development topic. I’m going to leave it open for a bit longer to see if anyone is able to chime in to help you out.

    I can also recommend the WooCommerce Developer Resources Portal for resources on developing for WooCommerce.

    You can also visit the WooCommerce Facebook group or the #developers channel of the WooCommerce Community Slack. We’re lucky to have a great community of open-source developers for WooCommerce, and many of our developers hang out there, as well.

    Niall a11n

    (@wpniall)

    Hi again,

    I hope you managed to get the code working. We haven’t heard back from you for a while, so I’m going to mark this post as resolved. If you have any further questions or need additional help with the Storefront theme, please start a new thread and we’ll be able to help you out there. Thanks!

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Conditional Checkout Fields by Variation’ is closed to new replies.