Magento 2: How to Disable Shipping Method Programmatically

How to Disable Shipping Method

We have disabled the payment method. However, we must manage shipping methods in various situations like payment. So, we’ll be able to disable the shipping method in this post.

We often get the situation to handle shipments on several conditions, like customer groups and region-based shipping methods. Of course, Magento contains built-in shipping methods, and we can create our own shipping methods. But the question is that when do we need to show it on the front end?

Magento contains interceptors, which make us easy to handle such things.

It is easy to play with shipping and payment methods. We just need to use the appropriate plugin.

Let’s check How to Disable Shipping Method in Magento 2. Sometimes we use different shipping methods in other cases; for example, we require shipping methods based on the customer’s shipping address, cart total, etc.

We can achieve this by intercepting Magento\Shipping\Model\Shipping class using the aroundCollectCarrierRate method.

We need to use the around method to handle shipping.

Create di.xml in order to define the plugin.

<?xml version="1.0"?>


<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Shipping\Model\Shipping">
        <plugin disabled="false" name="sbdevblog_disable_shipping_method" sortOrder="10" type="SbDevBlog\Shipping\Plugin\Model\HandleShippingMethods"/>
    </type>
</config>

Create The Plugin Class defined in di.xml

In the around method, we will return false if the carrier code matches.

Create HandleShippingMethods Class inside VendorName/ModuleName/Plugin/Model as defined in the di.xml

<?php
/**
 * @copyright Copyright (c) sbdevblog (http://www.sbdevblog.com)
 */
namespace SbDevBlog\Shipping\Plugin\Model;

class HandleShippingMethods
{
    public function aroundCollectCarrierRates(
        \Magento\Shipping\Model\Shipping $subject,
        \Closure $proceed,
         $carrierCode,
         $request
    ) {
        if ($carrierCode == 'shipping-carrier-code') { // add relevant shipping code
            return false;
        }
        return $proceed($carrierCode, $request);
    }
}

I have disabled free shipping method in the example given below:

Disable Free Shipping Method Programmatically
<?php
/**
 * @copyright Copyright (c) sbdevblog (http://www.sbdevblog.com)
 */
namespace SbDevBlog\Shipping\Plugin\Model;

class HandleShippingMethods
{
    public function aroundCollectCarrierRates(
        \Magento\Shipping\Model\Shipping $subject,
        \Closure $proceed,
         $carrierCode,
         $request
    ) {
        if ($carrierCode == 'freeshipping') {
            return false;
        }
        return $proceed($carrierCode, $request);
    }
}

That’s it. It is easy to restrict shipping methods based on conditions.

Thanks for reading this post; please subscribe and share with your connections. Also, please use the comment box for your valuable feedback.

You can visit the post to refresh the shipping methods and rates using JS.

Download Source Code

Note: Please verify the code of this blog and the relevant git repository before using it in production.
sb dev blog adobe commece Magento 2

🙂 HAPPY CODING 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *