Magento – Magento 2: Recalculate shipping methods when State/Province changes

checkoutjavascriptmagento-2.1

On step 1 (Shipping) of the checkout process, some of the fields – such as Zip/Postal Code – trigger re-evaluation of the list of available shipping methods.

I have a custom plugin in place that disables Free Shipping if the user chooses a non mainland-US state. Unfortunately, the "State/Province" select box does not trigger the AJAX call to "estimate-shipping-methods" which is needed to rebuild the list of shipping methods.

Does anyone know how to attach this event to the select box? I DON'T want a hacky answer like using jQuery to trigger onkeyup/onchange on one of the fields that already has the event attached to it.

Best Answer

After so much investigation I found one solution. I have tried as described follows and it's working fine for me:

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

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

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

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

    return {
        /**
         * @return {Object}
         */
        getRules: function () {
            return {
                'region_id': {
                    'required': true
                }
            };
        }
    };
});