Magento2 Checkout – Update Shipping Methods After Address Change

magento2shipping-methods

I need to trigger the call that updates the shipping methods on the checkout page when the city, address lines, telephone and email fields change. Just like when the shipping methods are updated when the postcode, country and province fields change.

I have tried following the steps in this tutorial form the official docs (https://devdocs.magento.com/guides/v2.3/howdoi/checkout/checkout_carrier.html), but it seems this only validates the fields client-side and does not trigger the shipping methods update call?

Best Answer

After so much investigation I found one solution. I have tried for Offline shipping methods as described follows and its working fine for me:

Namespace/Module/view/frontend/requirejs-config.js

var config = {
    "map": {
        "*": {
            "Magento_OfflineShipping/js/model/shipping-rates-validation-rules/flatrate": "Namespace_Module/js/model/shipping-rates-validation-rules/flatrate",
            "Magento_OfflineShipping/js/model/shipping-rates-validation-rules/freeshipping": "Namespace_Module/js/model/shipping-rates-validation-rules/freeshipping",
            "Magento_OfflineShipping/js/model/shipping-rates-validation-rules/tablerate": "Namespace_Module/js/model/shipping-rates-validation-rules/tablerate"
        }
    }
};

Namespace/Module/view/frontend/web/js/model/shipping-rates-validation-rules/flatrate.js

define([], function () {
    'use strict';

    return {
        /**
         * @return {Object}
         */
        getRules: function () {
            return {
                'country_id': {
                    'required': true
                },
                'city': {
                    'required': true
                },
                'telephone': {
                    'required': true
                }
            };
        }
    };
});

Namespace/Module/view/frontend/web/js/model/shipping-rates-validation-rules/freeshipping.js

define([], function () {
    'use strict';

    return {
        /**
         * @return {Object}
         */
        getRules: function () {
            return {
                'country_id': {
                    'required': true
                },
                'city': {
                    'required': true
                },
                'telephone': {
                    'required': true
                }
            };
        }
    };
});

Namespace/Module/view/frontend/web/js/model/shipping-rates-validation-rules/tablerate.js

define([], function () {
    'use strict';

    return {
        /**
         * @return {Object}
         */
        getRules: function () {
            return {
                'postcode': {
                    'required': true
                },
                'country_id': {
                    'required': true
                },
                'region_id': {
                    'required': true
                },
                'region_id_input': {
                    'required': true
                },
                'city': {
                    'required': true
                },
                'telephone': {
                    'required': true
                }
            };
        }
    };
});

These rules will automatically applied on change event of telephone and city fields.

Note: I have tried for Offline shipping methods only. If you wan to do it for other shipping methods, then you can try in the same way.