Woocommerce Conditional Options from Payment Method Selection

Viewed 399

I am wondering if someone can help me with the following code. I am trying to set some conditional html dependent on which payment method the user selects on checkout. On the payment method page I am using the following function to update the payment method information:

function cart_update_script() {
if (is_checkout()) :
?>
<script>
    jQuery( function( $ ) {


        // woocommerce_params is required to continue, ensure the object exists
        if ( typeof woocommerce_params === 'undefined' ) {
            return false;
        }

        $checkout_form = $( 'form.checkout' );

        $checkout_form.on( 'change', 'input[name="payment_method"]', function() {
                $checkout_form.trigger( 'update' );
        });

    });

</script>
<?php
endif;
}
add_action( 'wp_footer', 'cart_update_script', 999 );

On the final checkout page I am then getting that selected payment method with this bit of code:

<?php $chosen_gateway = WC()->session->chosen_payment_method; ?>

Then I am using conditional statements like the following:

<?php if ( $chosen_gateway == 'stripe' ) { ?>
     //do this
<?php } ?>

My problem is that I am not getting dynamic information when the user changes payment method. Sometimes the code works but sometimes it still thinks a different payment method was selected. Any help would be great!

0 Answers
Related