Magento, Magento 2

How to Hide Other Shipping Methods if Free Shipping Enable in Magento 2

In this tutorial, Today I will explain to how to hide other shipping methods if the free shipping method enables in Magento 2. There are many shipping methods by default provided by Magento 2. It may be possible that in your store there are multiple shipping methods enabled and for some products you want to do like there are only a free shipping methods display when other shipping methods should be hidden.

At that time, you need to perform the below steps. Let’s see the steps.

You may also like this :

1) First of all, Let’s assume that you have created a simple module. Now, create di.xml file for create a plugin to hide shipping methods at app/code/RH/Helloworld/etc/ and paste the below code :

<?xml version="1.0"?>
<!--
/**
 * Created By : Rohan Hapani
 */
-->
<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 name="hide_shipping_methods" type="RH\Helloworld\Plugin\HideShippingPlugin"/>
    </type>
</config>

2) Now, Create HideShippingPlugin.php file at app/code/RH/Helloworld/Plugin/ and paste the below code to show only Free Shipping Method :

<?php
/**
 * Created By : Rohan Hapani
 */
namespace RH\Helloworld\Plugin;

class HideShippingPlugin {

    public function aroundCollectCarrierRates(
        \Magento\Shipping\Model\Shipping $subject,
        \Closure $proceed,
        $carrierCode,
        $request
    ) {

        // Hide all shipping methods except free shipping method
        if ($carrierCode != 'freeshipping') {
            return false;;
        } 

        $result = $proceed($carrierCode, $request);
        return $result;
    }
}

Output :

Hide Shipping Rohan Hapani

That’s it !!!

I hope this blog is easy to understand about how to hide other shipping methods if the free shipping method enables in Magento 2. In case, I missed anything or need to add some information, always feel free to leave a comment in this blog, I’ll get back with a proper solution.

Stay Safe and Stay Connected !!

Tagged , ,