2

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.

1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.