{"id":1531,"date":"2021-01-20T09:16:27","date_gmt":"2021-01-20T09:16:27","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/enable-disable-shipping-method-programmatically-in-magento-2\/"},"modified":"2025-05-22T11:12:12","modified_gmt":"2025-05-22T05:42:12","slug":"enable-disable-shipping-method-programmatically-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/enable-disable-shipping-method-programmatically-in-magento-2\/","title":{"rendered":"How to Enable\/Disable Shipping Method Programmatically in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Online shoppers look forward to shipping charges and it often becomes the decision point for purchase.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Hence, E-commerce store owners need to define the shipping system strategically so that neither the customers are disappointed nor the business has to incur any loss.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the default Magento 2, the admin can enable or disable shipping method from the backend.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, if one wants to&nbsp;<em><strong>enable\/disable shipping method programmatically in Magento 2<\/strong><\/em>&nbsp;based on specific conditions, follow the below solution.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For instance, you want to enable the free shipping method for a minimum cart total and above. Or, you want to enable a certain shipping service for fixed regions or states only.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If the customer\u2019s orders fulfil such condition, then only the shipping method should be enabled or disabled.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It can be done with the programmatic solution in this post. Or, you may prefer the&nbsp;<a href=\"https:\/\/meetanshi.com\/magento-2-shipping-restrictions.html\" target=\"_blank\" rel=\"noreferrer noopener\">Magento 2 Shipping Restrictions<\/a>&nbsp;extension to restrict shipping methods based on the cart, customer attributes, zip codes, and days of the week!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Steps to Enable\/Disable Shipping Method Programmatically in Magento 2<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">1. Create a plugin in&nbsp;<strong>di.xml<\/strong>&nbsp;file and use this 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=\"\">&lt;type name=\"Magento\\Shipping\\Model\\Shipping\">\n    &lt;plugin disabled=\"false\" name=\"Vendor_Extension_Model_Shipping\" sortOrder=\"10\"\n            type=\"Vendor\\Extension\\Plugin\\ApplyShipping\"\/>\n&lt;\/type><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">2. Create a plugin file in&nbsp;<strong>Vendor\/Extension\/Plugin<\/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;?php\n\nnamespace Vendor\\Extension\\Plugin;\n\nclass ApplyShipping\n{\n\n    public function __construct()\n    {\n\n    }\n\n    public function aroundCollectCarrierRates(\n        \\Magento\\Shipping\\Model\\Shipping $subject,\n        \\Closure $proceed,\n        $carrierCode,\n        $request\n    )\n    {\n            \/\/ Enter Shipping Code here instead of 'freeshipping'\n        if ($carrierCode == 'freeshipping') {\n           \/\/ To disable the shipping method return false\n            return false;\n        } \n           \/\/ To enable the shipping method\n            return $proceed($carrierCode, $request);\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The shipping method will be disabled if free shipping is selected else enables.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s it!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Feel free to share the solution with Magento 2 community via social media.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank You.<strong>&nbsp;<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Online shoppers look forward to shipping charges and it often becomes the decision point for purchase. Hence, E-commerce store owners need to define the shipping&#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-1531","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1531","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=1531"}],"version-history":[{"count":3,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1531\/revisions"}],"predecessor-version":[{"id":14545,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1531\/revisions\/14545"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=1531"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=1531"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=1531"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}