skip to Main Content

Hi have a wocommerce plugin, While changing the payment method. the cost for shipping my function calculate_shipping is not being called. and therefore shipping methods are not updating with appropriate cost

here are some samples from my code.

    public function __construct() {
                add_action('woocommerce_review_order_before_payment', array($this, 'update_shipping_charges'), 1);
}

public function  update_shipping_charges() {
        // jQuery code
        ?>
        <script type="text/javascript">
            (function ($) {
                $('form.checkout').on('change', 'input[name^="payment_method"]', function () {
                    // Set the select value in a variable
                   
                    $('body').trigger('update_checkout');

                     //testing
                    $('body').on('updated_checkout', function() {
                        console.log('updated');
                    });
                });
            })(jQuery);
        </script>
        <?php
    }

2

Answers


  1. Try wrapping everything inside the init hook. Maybe the code gets executed too early:

    public function __construct() {
        add_action( 'init', [ $this, 'init_action' ] );
    }
    
    public function init_action(): void {
        add_action( 'woocommerce_review_order_before_payment', [ $this, 'update_shipping_charges' ], 1 );
    }
    
    public function update_shipping_charges(): void {
        // jQuery code
        ?>
        <script type="text/javascript">
            (function ( $ ) {
                $( 'form.checkout' ).on( 'change', 'input[name^="payment_method"]', function () {
                    // Set the select value in a variable
                    let body = $( 'body' );
    
                    body.trigger( 'update_checkout' );
    
                    //testing
                    body.on( 'updated_checkout', function () {
                        console.log( 'updated' );
                    } );
                } );
            })( jQuery );
        </script>
        <?php
    }
    

    You could try multiple hooks if init don’t works:

    woocommerce_loaded
    woocommerce_init
    plugins_loaded <- I use this one in my plugins for example
    
    Login or Signup to reply.
  2. Please try the following (Add to functions.php or via Code Snippets plugin)

    function kia_checkout_on_payment_change() {
        wp_add_inline_script( 'wc-checkout', '
            jQuery( document.body ).on( "payment_method_selected", function() {
                jQuery( "form.checkout" ).trigger( "update_checkout", { update_shipping_method: true } );
                console.log("payment selected");
                } );
            '
        );
    }
    add_action( 'wp_enqueue_scripts', 'kia_checkout_on_payment_change' );
    

    A couple notes….

    1. We’re adding the script right after the wc-checkout script is loaded.
    2. We’re listening for the WooCommerce checkout script’s payment_method_selected (but yes, it’s a core Woo trigger in the checkout) trigger which should fire when the payment method is changed
    3. We’re explicitly passing some arguments to the update_checkout function.

    Don’t have any shipping methods set up locally to test it properly, but I hope it points you in the right direction.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search