2

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.

1

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;
        }
   }
2
  • Ok I am checking this code – Vikram Kumar Feb 15 at 10:45
  • Thanks! Its working for me. – Vikram Kumar Feb 15 at 10:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.