Magento 2 hide shipping method in frontend

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 :

Magento 2 hide shipping method in frontend

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 how-to, magento2, shipping

In most cases, it happens when you do not want to offer a free shipping method to your customers. But the orders are placed with a free shipping method by default from the admin grid.

In that case, as a site owner, you will want to remove or disable a free shipping method from the front end of your Magento 2 store. Well, to resolve this problem, we have come up with the ultimate solution to remove/disable custom free shipping in Magento2 from the front-end.

Let’s get started on how to remove/disable the custom free shipping method from the front-end in Magento 2.

Steps to remove/disable custom free shipping method from the front-end in Magento 2:

  • Go to the path

    app\code\MageSpark\RemoveShipping\etc\di.xml,

    and add the below code:

    <?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="MageSpark\CustomShipping\Model\Carrier\CustomShipping"> <plugin name="disable_customshipping" type="MageSpark\RemoveShipping\Plugin\DisableCustomShipping" sortOrder="1"/> </type> </config>
  • Go to the path

    app\code\MageSpark\RemoveShipping\Plugin\DisableCustomShipping.php

    and use the code below:

    <?php namespace MageSpark\RemoveShipping\Plugin; use Magento\Backend\Model\Auth\Session; use MageSpark\CustomShipping\Model\Carrier\CustomShipping; class DisableCustomShipping { /** * @var Session */ private $_session; public function __construct( Session $session ) { $this->_session = $session; } /** * @param CustomShipping $subject * @param \Closure $proceed * @param $request * @return false|mixed */ public function aroundCollectRates( CustomShipping $subject, \Closure $proceed, $request ) { if (!$this->_session->isLoggedIn()) { return false; // Only allow this to be used from the admin system } return $proceed($request); } }

Thus, following the above steps, you can easily Remove/Disable Custom Free Shipping Method from Front-end in Magento 2. If you have any questions or face any kind of problem, please contact us.

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>

    <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); } }

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    <?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!

    Any doubts about the solution can be mentioned in the Comments section below. I’d be happy to help.

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

    Thank You. 

Magento 2 hide shipping method in frontend

Shipping Restrictions

Allows restricting shipping methods based on the cart, customer attributes, zip codes, and days of the week for efficient shipping management.