How To

How to Remove/Disable Custom Free Shipping Method from Front-end only in Magento 2?

Hello Magento Friends,

Today I will be discussing on How to Remove/Disable Custom Free Shipping Method from Front-end only in Magento 2?

Many times, you do not want to provide a free shipping method to your customers. But the orders placed from the admin grid should have a free shipping method. At that time, Magento store merchants come across the requirement to disable or remove the free shipping method from the store frontend only. Also, check out Shipping and Payment Method per Customer Group Extension which allows the store owner to manage shipping and payment method for the specific customer groups.

So, let’s find out How to Remove/Disable Custom Free Shipping Method from Front-end only in Magento 2?

Steps to Remove/Disable Custom Free Shipping Method from Front-end only in Magento 2:

Step 1: Go to the below path,

app\code\Vendor\Extension\etc\di.xml

And add this 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="Vendor\Extension\Model\Carrier\Customshipping">
        <plugin name="disable_customshipping" type="Vendor\Extension\Plugin\DisableEnableCustomShipping" sortOrder="1" />
    </type>
</config>

Step 2: Now, go to the below path,

app\code\Vendor\Extension\Plugin\DisableEnableCustomShipping.php

And finally, add the code as mentioned below:

<?php

namespace Vendor\Extension\Plugin;
    
use Magento\Backend\Model\Auth\Session;
    
class DisableEnableCustomShipping
{
     /**
     * @var Session
     */     private $_session;     
    
     public function __construct(Session $session)
     {
          $this->_session = $session;
     }
    
     public function aroundCollectRates(
     \Vendor\Extension\Model\Carrier\Customshipping $subject,
     \Closure $proceed,
      $request)
      {
           if (!$this->_session->isLoggedIn())
           {
                return false;   // Only allow this to be used from the admin system
           }
    
           $result = $proceed($request);
           return $result;
      }
}

Conclusion:

Hence, with the help of the above code, you can easily Remove/Disable Custom Free Shipping Method from Front-end only in Magento 2. Also, check out Shipping and Payment Method per Customer Group Extension which allows the store owner to manage shipping and payment method for the specific customer groups. If you have any difficulty, mention it in the comment section. Do share the article further. See you soon till then stay in touch!

Happy Coding!

Click to rate this post!
[Total: 3 Average: 5]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

View Comments

Recent Posts

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

2 days ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

2 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

3 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

4 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

6 days ago

How Upcoming Cookie Changes Will Affect Your E-commerce Website?

The e-commerce world is constantly in flux. New tech and strategies emerge daily to help…

6 days ago