0

I have the shipping method "Flatrate" enabled in backend. So, as expected, it is available in frontend, too. But this shipping method should only be available in backend.

Of course, I already googled for this: How to hide a shipping method in front end and visible on admin section Magento 2?

But this doesn't work for me. I have my own module and namespace already and created the di.xml file. This file is for dependency injection, ok. But what other files do I have to create and what's the content of the files and the di.xml?

That's what I have:

/app/code/MyNameSpace/MyModule/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">
    <preference for="Magento\OfflineShipping\Model\Carrier\Flatrate" type="MyNameSpace\MyModule\Model\Carrier\Flatrate" />
</config>

/app/code/MyNameSpace/MyModule/Model/Carrier/Flatrate.php

// no content - I just don't know what to do here...
  • Hi alex can u please let me know why you would like to hide Flatrate at frontend? – Nagaraju K Dec 4 '18 at 17:37
  • Hi. This is only for telephonic orders. – DaFunkyAlex Dec 5 '18 at 7:09
4

You can disable/hide Flatrate shipping method at frontend in magento 2 using plugin concept. please follow the below steps

  1. Create custom module Hello_World
  2. Create plugin and configure in di.xml file, app/code/Hello/World/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="Magento\OfflineShipping\Model\Carrier\Flatrate">
          <plugin name="disable-Flatrate" type="Hello\World\Model\Carrier\Flatrate" sortOrder="1" />  

        </type>      
 </config>
  1. Create model file app/code/Hello/World/Model/Carrier/Flatrate.php

    <?php
    
    namespace Hello\World\Model\Carrier;    
    
    class Flatrate{
    
    protected $_checkoutSession;        
    
    protected $_scopeConfig;
    
    protected $_customerSession;
    
    public function __construct(
        \Magento\Checkout\Model\Session $checkoutSession,
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Customer\Model\Session $customerSession
    ) {
        $this->_storeManager = $storeManager;
        $this->_checkoutSession = $checkoutSession;
        $this->_scopeConfig = $scopeConfig;
        $this->_customerSession = $customerSession;
    }
    
    public function afterCollectRates(\Magento\OfflineShipping\Model\Carrier\Flatrate $Flatrate, $result)
    {   
        //Magento-2 Log Here
        $writer = new \Zend\Log\Writer\Stream(BP.'/var/log/magento2.log');
        $logger = new \Zend\Log\Logger();
        $logger->addWriter($writer);     
        $logger->info("Flatrate shipping has been calling");                
        //keep your condition if you want
    
        return false;       
    
        return $result;
    }  
    
    }
    
  • are you sure this should be app/code/Hello/World/di.xml, not app/code/Hello/World/etc/di.xml? – DaFunkyAlex Dec 5 '18 at 7:09
  • it should app/code/Hello/World/etc/di.xml – Nagaraju K Dec 5 '18 at 8:45
  • i have updated post now thanks for found a mistake – Nagaraju K Dec 5 '18 at 8:46
  • Please try in your local instance and let me know if u face any issue & please don't forget upvote and accept anser when its working fine. so that it will helpful to other developers like us. – Nagaraju K Dec 5 '18 at 9:00
  • @NagarajuKasa, How to hide specific shipping method option, in my case there is a store pickup option, I need to hide it – jafar pinjar Apr 26 '19 at 12:24
1

Your carrier class should extend the core flatrate carrier, and override the isActive() function to check which area it's being requested from.

namespace MyNameSpace\MyModule\Model\Carrier;

class Flatrate extends \Magento\OfflineShipping\Model\Carrier\Flatrate
{
    public function isActive() {

        /* Get area code (disclaimer - please inject this properly in the constructor) */
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $appState = $objectManager->get('Magento\Framework\App\State');
        $areaCode = $appState->getAreaCode();

        /* Always inactive if not in admin, otherwise fall back to default behaviour */
        if ($areaCode != 'admin') {
            return false 
        } else {
            return parent::isActive();
        }
    }
}

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.