• Resolved Henry H.

    (@sirhenryhu)


    Hi all. I’m facing a very difficult situation right now and hope someone can give some useful solutions on this one. I’m using a 3rd party plugin developed by the domestic payment flow company which provides visitors to pay by credit cards and common logistics options. The shipping method is named “ecpay_shipping”.

    I’d like to convert the required address related fields to optional and hide them on the condition that shipping_method equals ecpay_shipping.

    The four fields I wish to tweak are:

    1. billing_address_1
    2. billing_city
    3. billing_postcode
    4. billing_state

    I have tried the followed function but it only does the hiding part but causes the order process to drop because the four fields are not filled properly.

    add_filter('woocommerce_checkout_fields', 'xa_remove_billing_checkout_fields');
    
    function xa_remove_billing_checkout_fields($fields) {
        $shipping_method ='ecpay_shipping';
        global $woocommerce;
        $chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
        $chosen_shipping = $chosen_methods[0];
    
        if ($chosen_shipping == $shipping_method) {
            unset($fields['billing']['billing_address_1']);
            unset($fields['billing']['billing_city']);
            unset($fields['billing']['billing_postcode']);
            unset($fields['billing']['billing_state']);
        }
        return $fields;
    }

    Kinda stuck on this. Hopefully, someone can come up with a solution to this. Thank you in advance.

    • This topic was modified 2 years, 7 months ago by Henry H..
Viewing 4 replies - 1 through 4 (of 4 total)
  • Hi @sirhenryhu! Thanks for reaching out to us!

    This is a fairly complex development topic. I’m going to leave it open for a bit to see if anyone can chime in to help you out.

    Just something that you can consider, to make a field optional you can filter/hook into the woocommerce_default_address_fields and make them false instead of unset.

    Something like:

    add_filter( 'woocommerce_default_address_fields' , 'myFunctionName' );
     function myFunctionName( $address_fields ) {
        $address_fields['city']['required'] = false;
     // ...
          return $address_fields;
     }

    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.

    My very best!

    Thread Starter Henry H.

    (@sirhenryhu)

    Hi @chiape. Unfortunately, I am a non-techie and barely understand a little bit of CSS only. However, thank you for sharing the information and will learn from it for sure.

    Hi @sirhenryhu

    Never mind. You may receive useful inputs further on this thread from other members of the community.

    Also, for direct assistance with code customizations, you can consult with the WooCommerce Customizations Partners at https://woocommerce.com/customizations/, apart from the support channels suggested above.

    Thanks

    Seems we’ve not had additional inputs on this thread. Thus, we encourage you to make use of the above resources.

    I’ll go ahead and mark the thread as closed but please feel free to create a new thread if you have other questions.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How to Covert Attributes of Checkout Address Fields based on Shipping Method’ is closed to new replies.