Question

I created a Custom Free Shipping method and want to disable only in front end.

I try using getAreaCode() check here..but not working in Magento 2.4.0.

Please help if you know.

Was it helpful?

Solution

Please try this code. It will help you

etc/di.xml

<?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="Suraj\CustomShipping\Model\Carrier\Customshipping">
        <plugin name="disable_customshipping" type="Suraj\CustomShipping\Plugin\DisableEnableCustomShipping" sortOrder="1" />
    </type>
</config>

Plugin/DisableEnableCustomShipping.php

<?php

    namespace Suraj\CustomShipping\Plugin;
    
    use Magento\Backend\Model\Auth\Session;
    
    class DisableEnableCustomShipping
    {
        /**
         * @var Session
         */
        private $_session;     
    
        public function __construct(
            Session $session
        )
        {
            $this->_session = $session;
        }
    
        public function aroundCollectRates(
            \Suraj\CustomShipping\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;
        }
   }
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top