skip to Main Content

I have some additional javascript I’m looking to run on the WooCommerce checkout page when a user changes their shipping method. Currently, I’m using the following;

jQuery( document.body ).on( 'updated_checkout', function() {
    // Javascript here...
});

The problem with the above is that this event fires when users also change any of the checkout data including the address. I only want to run my javascript when the shipping method is updated at checkout. I can’t see any event listeners or hooks for this.

2

Answers


  1. Chosen as BEST ANSWER

    Thanks, @CBroe in the comments for pointing me in the right direction, I was looking to overcomplicate the problem. See my solution below;

    This works with the default WooCommerce shipping classes;

    jQuery(document.body).on( 'change', 'input.shipping_method', function() {
        // Javascript here...
    });
    

  2. You can add this action:

    add_action( 'woocommerce_after_checkout_form', 'your_function' );
    function your_function( $available_gateways ) {
        wc_enqueue_js( ";
    $('form.checkout').on('change', 'input[name^="shipping_method"]', function () {
        const val = $(this).val();
        // you code here ...
    });
    " );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search