1

I have the shipping method "Flatrate" enabled in the backend. So, as expected, it is available in the frontend, too. But this shipping method should only be available in the 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...
2
  • Hi alex can u please let me know why you would like to hide Flatrate at frontend? Commented Dec 4, 2018 at 17:37
  • Hi. This is only for telephonic orders. Commented Dec 5, 2018 at 7:09

3 Answers 3

8

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;
    }  
    
    }
    
7
  • are you sure this should be app/code/Hello/World/di.xml, not app/code/Hello/World/etc/di.xml? Commented Dec 5, 2018 at 7:09
  • it should app/code/Hello/World/etc/di.xml Commented Dec 5, 2018 at 8:45
  • i have updated post now thanks for found a mistake Commented Dec 5, 2018 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. Commented Dec 5, 2018 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 Commented Apr 26, 2019 at 12:24
2

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();
        }
    }
}
1

You can HIDE any SHIPPING method by given code for login customer as well guest customer

app/code/Vendor/Extension/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\Quote\Model\ShippingMethodManagement">
        <plugin name="hide_shippingmethod" type="Vendor\Extension\Plugin\Model\ShippingMethodManagement"/>
    </type>
</config>

app/code/Vendor/Extension/Plugin/Model/ShippingMethodManagement.php

<?php
namespace Vendor\Extension\Plugin\Model;
 
class ShippingMethodManagement {
 
    public function afterEstimateByExtendedAddress($shippingMethodManagement, $output)
    {
        return $this->filterOutput($output);
    }
    public function afterEstimateByAddressId($shippingMethodManagement, $output)
    {
        return $this->filterOutput($output);
    }
    private function filterOutput($output)
    { 
        $result = []; 
        foreach ($output as $shippingMethod) {
            if ($shippingMethod->getMethodCode() == 'free') {
              continue;
            }
            $result[] = $shippingMethod;
        }
        
        return $result;  
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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