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

Magento 2: How to Save Custom Field Value to quote_address for Multi-Shipping Orders

Hello Magento Friends, In Magento 2, the checkout process allows customers to choose multiple shipping…

21 mins ago

Best Beginners Guide to Shopify Balance Account

If you are a Shopify admin, using a Shopify Balance Account for your business revenue…

23 mins ago

8 Best Social Login Apps for Shopify Store in 2024

Running an eCommerce business can be incredibly demanding, leaving entrepreneurs little time to focus on…

23 mins ago

Generating Thumbnails with Spatie Media Library in Laravel 11: A Step-by-Step Guide

Generating image thumbnails is a common requirement in web applications, especially when handling media-heavy content.…

1 day ago

Enhancing Web Application Security with Laravel’s Built-In Features

In today’s digital landscape, web application security is paramount. As a powerful PHP framework, Laravel…

2 days ago

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

October was an exciting month for MageComp! From significant updates across our Magento 2 extension…

2 days ago