Update Woocommerce cart shipping methods on country change

Viewed 2881

Inspired by the question of: update cart shipping woocommerce when change country with ajax, (the answer does not work and is incorrect).

In my case I have 2 shipping Zones configured:

  • Colombia - 3 shipping methods (Free, Pick up at Destination and Local Delivery)
  • Rest of the World, I have a unique shipping method which is (Pick up at destination).

My intention is to hide the update button of the cart shipping calculator and that when selecting the shipping country the methods are automatically displayed.

Note: In the shipping calculator I only have the country activated, the other fields have been deactivated. I have no knowledge of ajax or jquery, but I think this is done with ajax / jquery. Please I appreciate any help in this.

1 Answers

You can try to use the following:

add_action('wp_footer', 'cart_country_update_shipping_methods', 50);
function cart_country_update_shipping_methods() {
    if ( ! is_cart() ) return; // On cart
    ?>
    <script type='text/javascript'>
        jQuery(function($){
            $(document.body).on('change', 'select[name="calc_shipping_country"]', function(){
                $(this).submit();
            });
        });
    </script>
    <?php
}

Code goes in functions.php file of your active child theme (or active theme). Tested and works.

Related: Shipping calculator state field change trigger update in WooCommerce cart

Related