🔥 Just Launched! Werra Premium Template for HyväSee it in Action

How to Enable/Disable Shipping Method Programmatically in Magento 2

By Sanjay JethvaUpdated on May 22, 2025 2 min read

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 system strategically so that neither the customers are disappointed nor the business has to incur any loss.

In the default Magento 2, the admin can enable or disable shipping method from the backend.

However, if one wants to enable/disable shipping method programmatically in Magento 2 based on specific conditions, follow the below solution.

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.

If the customer’s orders fulfil such condition, then only the shipping method should be enabled or disabled.

It can be done with the programmatic solution in this post. Or, you may prefer the Magento 2 Shipping Restrictions extension to restrict shipping methods based on the cart, customer attributes, zip codes, and days of the week!

Steps to Enable/Disable Shipping Method Programmatically in Magento 2

1. Create a plugin in di.xml file and use this below code.

<type name="Magento\Shipping\Model\Shipping">
    <plugin disabled="false" name="Vendor_Extension_Model_Shipping" sortOrder="10"
            type="Vendor\Extension\Plugin\ApplyShipping"/>
</type>

2. Create a plugin file in Vendor/Extension/Plugin.

<?php

namespace Vendor\Extension\Plugin;

class ApplyShipping
{

    public function __construct()
    {

    }

    public function aroundCollectCarrierRates(
        \Magento\Shipping\Model\Shipping $subject,
        \Closure $proceed,
        $carrierCode,
        $request
    )
    {
            // Enter Shipping Code here instead of 'freeshipping'
        if ($carrierCode == 'freeshipping') {
           // To disable the shipping method return false
            return false;
        } 
           // To enable the shipping method
            return $proceed($carrierCode, $request);
    }
}

The shipping method will be disabled if free shipping is selected else enables.

That’s it!

Feel free to share the solution with Magento 2 community via social media.

Thank You. 

Sanjay Jethva Full Image
Article bySanjay Jethva

Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe. His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.