• First, I’m continually amazed at how well WooCommerce has been designed – simply amazing. Well done!

    I’d like to hide/show checkout fields based on the selected payment gateway (e.g. if paying by check, show/hide certain checkout fields and if paying by PayPal, show/hide certain checkout fields). I need checkout field visibility to change dynamically when the user toggles selected payment methods at checkout.

    I understand the basic mechanics, since for other purposes, I am already using
    add_filter( 'woocommerce_checkout_fields' , ... );
    add_action( 'woocommerce_cart_calculate_fees', ... );
    and I am already triggering ‘update_checkout’ via a script when the payment_method changes, so that cart fees are recalculated when the payment method changes.

    Do I modify the ‘woocommerce_checkout_fields’ filter (based on payment method) and refresh the checkout fields with another script? A few suggestions or an example would be greatly appreciated.

    Thank you!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter deeveedee

    (@deeveedee)

    I found that I can force a “refresh” of the billing checkout fields when the payment method is toggled by adding the following to my child theme’s filters.php. Now, when I toggle the payment method at checkout, the woocommerce_checkout_fields are reloaded which re-applies my custom checkout fields filter (to conditionally hide unwanted billing checkout fields based on the payment method). If anyone has a better way or a reason not to do it this way, I’d be interested. Thank you.

    
    function filter_woocommerce_update_order_review_fragments( $array ) { 
       ob_start();
       wc_get_template( 'checkout/form-billing.php', array( 'checkout' => WC()->checkout() ) );
       $array[".woocommerce-billing-fields"] = ob_get_clean();
       return $array; 
    }; 
    add_filter( 'woocommerce_update_order_review_fragments', 'filter_woocommerce_update_order_review_fragments', 10, 1 );

    Would you be able/willing to add the full function to make this work?

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Change checkout fields based on payment gateway’ is closed to new replies.