{"id":1661,"date":"2021-03-24T04:30:53","date_gmt":"2021-03-24T04:30:53","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/change-shipping-price-on-address-field-change-in-magento-2-custom-shipping-method\/"},"modified":"2025-07-17T09:34:11","modified_gmt":"2025-07-17T04:04:11","slug":"change-shipping-price-on-address-field-change-in-magento-2-custom-shipping-method","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/change-shipping-price-on-address-field-change-in-magento-2-custom-shipping-method\/","title":{"rendered":"How to Change Shipping Price on Address Field Change in Magento 2 Custom Shipping Method"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Customer convenience is the essential aspect of any successful online store. Offering a convenient method of purchasing as well as shipping is part and parcel of improving the customer experience in an online store.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">By default, there are 7 different shipping methods in Magento 2. However, if these default methods are not enough for your business, you can&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/magento-2-create-shipping-method\/\" target=\"_blank\" rel=\"noreferrer noopener\">create shipping method in Magento 2<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For custom shipping methods, you need to also look out for other behaviour that needs to be smoothly functioning with customization in the same way as with the default feature. This includes accommodating features such as&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/configure-multiple-shipping-addresses-in-magento-2\/\">multiple shipping addresses<\/a>, which may be essential for some customers. Multiple shipping addresses allow customers to easily manage and choose between different shipping destinations for their orders.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, the change in shipping price on the address field change when a custom shipping method is selected. Also enhance the user experience by providing real time cost calculation, transparency and essential shipping information by&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/update-checkout-summary-on-selecting-shipping-method-in-magento-2\/\">updating checkout summary on selecting shipping method in Magento 2<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The customer has to refresh the page after changing the value of the address field in order to see the updated shipping rate in default\u00a0Magento 2.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It\u2019s annoying when you have to reload the page to see the effect on shipping rate after changing country, state, and postcode on the checkout page, isn\u2019t it? That\u2019s why I\u2019ve come up with the solution to&nbsp;<em><strong>change shipping price on address field change in Magento 2 custom shipping method.<\/strong><\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The below solution saves your customer from reloading the page and offers a customer convenience checkout using ajax.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method to Change Shipping Price on Address Field Change in Magento 2 Custom Shipping Method<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">1. Use the below code in&nbsp;<strong>checkout_cart_index.xml&nbsp;<\/strong>at&nbsp;<strong>Vendor\/Module\/view\/frontend\/layout.<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\"?>\n&lt;page xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" layout=\"1column\"\n      xsi:noNamespaceSchemaLocation=\"urn:magento:framework:View\/Layout\/etc\/page_configuration.xsd\">\n    &lt;body>\n        &lt;referenceBlock name=\"checkout.cart.shipping\">\n            &lt;arguments>\n                &lt;argument name=\"jsLayout\" xsi:type=\"array\">\n                    &lt;item name=\"components\" xsi:type=\"array\">\n                        &lt;item name=\"summary-block-config\" xsi:type=\"array\">\n                            &lt;item name=\"children\" xsi:type=\"array\">\n                                &lt;item name=\"shipping-rates-validation\" xsi:type=\"array\">\n                                    &lt;item name=\"children\" xsi:type=\"array\">\n                                        &lt;item name=\"&lt;carrier-name>-rates-validation\" xsi:type=\"array\">\n                                            &lt;item name=\"component\" xsi:type=\"string\">\n                                                Vendor_Module\/js\/view\/shipping-rates-validation\/\n                                                &lt;carrier-name>\n                                            &lt;\/item>\n                                        &lt;\/item>\n                                    &lt;\/item>\n                                &lt;\/item>\n                            &lt;\/item>\n                        &lt;\/item>\n                    &lt;\/item>\n                &lt;\/argument>\n            &lt;\/arguments>\n        &lt;\/referenceBlock>\n    &lt;\/body>\n&lt;\/page><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">2. Add the below code in&nbsp;<strong>checkout_index_index.xml&nbsp;<\/strong>at&nbsp;<strong>Vendor\/Module\/view\/frontend\/layout<\/strong>.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\"?>\n&lt;page xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\n      xsi:noNamespaceSchemaLocation=\"urn:magento:framework:View\/Layout\/etc\/page_configuration.xsd\">\n    &lt;body>\n        &lt;referenceBlock name=\"checkout.root\">\n            &lt;arguments>\n                &lt;argument name=\"jsLayout\" xsi:type=\"array\">\n                    &lt;item name=\"components\" xsi:type=\"array\">\n                        &lt;item name=\"checkout\" xsi:type=\"array\">\n                            &lt;item name=\"children\" xsi:type=\"array\">\n                                &lt;item name=\"steps\" xsi:type=\"array\">\n                                    &lt;item name=\"children\" xsi:type=\"array\">\n                                        &lt;item name=\"shipping-step\" xsi:type=\"array\">\n                                            &lt;item name=\"children\" xsi:type=\"array\">\n                                                &lt;item name=\"step-config\" xsi:type=\"array\">\n                                                    &lt;item name=\"children\" xsi:type=\"array\">\n                                                        &lt;item name=\"shipping-rates-validation\" xsi:type=\"array\">\n                                                            &lt;item name=\"children\" xsi:type=\"array\">\n                                                                &lt;item name=\"&lt;carrier-name>-rates-validation\"\n                                                                      xsi:type=\"array\">\n                                                                    &lt;item name=\"component\" xsi:type=\"string\">\n                                                                        Vendor_Module\/js\/view\/shipping-rates-validation\/\n                                                                        &lt;carrier-name>\n                                                                    &lt;\/item>\n                                                                &lt;\/item>\n                                                            &lt;\/item>\n                                                        &lt;\/item>\n                                                    &lt;\/item>\n                                                &lt;\/item>\n                                            &lt;\/item>\n                                        &lt;\/item>\n                                    &lt;\/item>\n                                &lt;\/item>\n                            &lt;\/item>\n                        &lt;\/item>\n                    &lt;\/item>\n                &lt;\/argument>\n            &lt;\/arguments>\n        &lt;\/referenceBlock>\n    &lt;\/body>\n&lt;\/page><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">3. Create the file&nbsp;<strong>&lt;carrier-name&gt;.js&nbsp;<\/strong>at&nbsp;<strong><strong>Vendor\/Module\/view\/frontend\/web\/js\/model\/shipping-rates-validation-rules.<\/strong><\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">define([], function () {\n    'use strict';\n\n    return {\n        \/**\n         * @return {Object}\n         *\/\n        getRules: function () {\n            return {\n                'postcode': {\n                    'required': true\n                },\n                'country_id': {\n                    'required': true\n                },\n                'region_id': {\n                    'required': true\n                },\n                'region_id_input': {\n                    'required': true\n                }\n            };\n        }\n    };\n});<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Here postcode, country_id, region_id and region_id_input are the checkout field name. Thus you can use other fields also on which you need required:true.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">4. Create another&nbsp;<strong>&lt;carrier-name&gt;.js&nbsp;<\/strong>at&nbsp;<strong>Vendor\/Module\/view\/frontend\/web\/js\/model\/shipping-rates-validator<\/strong>&nbsp;and use the below code.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">define([\n    'jquery',\n    'mageUtils',\n    '..\/shipping-rates-validation-rules\/&lt;carrier-name>',\n    'mage\/translate'\n], function ($, utils, validationRules, $t) {\n    'use strict';\n\n    return {\n        validationErrors: [],\n\n        \/**\n         * @param {Object} address\n         * @return {Boolean}\n         *\/\n        validate: function (address) {\n            var self = this;\n\n            this.validationErrors = [];\n            $.each(validationRules.getRules(), function (field, rule) {\n                var message, regionFields;\n\n                if (rule.required &amp;&amp; utils.isEmpty(address[field])) {\n                    message = $t('Field ') + field + $t(' is required.');\n                    regionFields = ['region', 'region_id', 'region_id_input'];\n\n                    if (\n                        $.inArray(field, regionFields) === -1 ||\n                        utils.isEmpty(address.region) &amp;&amp; utils.isEmpty(address['region_id'])\n                    ) {\n                        self.validationErrors.push(message);\n                    }\n                }\n            });\n\n            return !this.validationErrors.length;\n        }\n    };\n});<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">5. Use the below code in&nbsp;<strong>&lt;carrier-name&gt;.js&nbsp;<\/strong>at&nbsp;<strong><strong>Vendor\/Module\/view\/frontend\/web\/js\/view\/shipping-rates-validation.<\/strong><\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">define([\n    'uiComponent',\n    'Magento_Checkout\/js\/model\/shipping-rates-validator',\n    'Magento_Checkout\/js\/model\/shipping-rates-validation-rules',\n    '..\/..\/model\/shipping-rates-validator\/&lt;carrier-name>',\n    '..\/..\/model\/shipping-rates-validation-rules\/&lt;carrier-name>'\n], function (Component,\n             defaultShippingRatesValidator,\n             defaultShippingRatesValidationRules,\n             customShippingRatesValidator,\n             customShippingRatesValidationRules) {\n    'use strict';\n\n    defaultShippingRatesValidator.registerValidator('&lt;carrier-name>', customShippingRatesValidator);\n    defaultShippingRatesValidationRules.registerRules('&lt;carrier-name>', customShippingRatesValidationRules);\n\n    return Component;\n});<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Note : Replace&nbsp;<strong>&lt;carrier-name&gt;<\/strong>&nbsp;with your carrier name which you can find in class specified in<strong>&nbsp;&lt;mode&gt;<\/strong>&nbsp;tag of&nbsp;<strong>app\/code\/Vendor\/Module\/etc\/config.xml<\/strong>&nbsp;file in which carrier name is specified as&nbsp;<strong>protected $_code = \u2018&lt;carrier-name&gt;\u2019;<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\"?>\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\n        xsi:noNamespaceSchemaLocation=\"urn:magento:module:Magento_Store:etc\/config.xsd\">\n    &lt;default>\n        &lt;carriers>\n            &lt;carrier-name>\n                &lt;model>Vendor\\Module\\Model\\Carrier\\ClassName&lt;\/model>\n            &lt;\/carrier-name>\n        &lt;\/carriers>\n    &lt;\/default>\n&lt;\/config><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s it. Just being informative you can also&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/get-selected-shipping-rate-in-magento-2-checkout-page\/\">get selected shipping rate in Magento 2<\/a>&nbsp;to know which shipping rate is chosen by customers at checkout page for customizable and information purpose.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Feel free to share the solution with Magento Community via social media.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank You.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Customer convenience is the essential aspect of any successful online store. Offering a convenient method of purchasing as well as shipping is part and parcel&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-1661","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1661","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=1661"}],"version-history":[{"count":4,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1661\/revisions"}],"predecessor-version":[{"id":18164,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1661\/revisions\/18164"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=1661"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=1661"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=1661"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}