The question:
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="MagentoOfflineShippingModelCarrierFlatrate" type="MyNameSpaceMyModuleModelCarrierFlatrate" />
</config>
/app/code/MyNameSpace/MyModule/Model/Carrier/Flatrate.php
// no content - I just don't know what to do here...
The Solutions:
Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.
Method 1
You can disable/hide Flatrate shipping method at frontend in magento 2 using plugin concept. please follow the below steps
- Create custom module
Hello_World
- 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="MagentoOfflineShippingModelCarrierFlatrate"> <plugin name="disable-Flatrate" type="HelloWorldModelCarrierFlatrate" sortOrder="1" /> </type> </config>
-
Create model file
app/code/Hello/World/Model/Carrier/Flatrate.php
<?php namespace HelloWorldModelCarrier; class Flatrate{ protected $_checkoutSession; protected $_scopeConfig; protected $_customerSession; public function __construct( MagentoCheckoutModelSession $checkoutSession, MagentoFrameworkAppConfigScopeConfigInterface $scopeConfig, MagentoStoreModelStoreManagerInterface $storeManager, MagentoCustomerModelSession $customerSession ) { $this->_storeManager = $storeManager; $this->_checkoutSession = $checkoutSession; $this->_scopeConfig = $scopeConfig; $this->_customerSession = $customerSession; } public function afterCollectRates(MagentoOfflineShippingModelCarrierFlatrate $Flatrate, $result) { //Magento-2 Log Here $writer = new ZendLogWriterStream(BP.'/var/log/magento2.log'); $logger = new ZendLogLogger(); $logger->addWriter($writer); $logger->info("Flatrate shipping has been calling"); //keep your condition if you want return false; return $result; } }
Method 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 MyNameSpaceMyModuleModelCarrier;
class Flatrate extends MagentoOfflineShippingModelCarrierFlatrate
{
public function isActive() {
/* Get area code (disclaimer - please inject this properly in the constructor) */
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$appState = $objectManager->get('MagentoFrameworkAppState');
$areaCode = $appState->getAreaCode();
/* Always inactive if not in admin, otherwise fall back to default behaviour */
if ($areaCode != 'admin') {
return false
} else {
return parent::isActive();
}
}
}
Method 3
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="MagentoQuoteModelShippingMethodManagement">
<plugin name="hide_shippingmethod" type="VendorExtensionPluginModelShippingMethodManagement"/>
</type>
</config>
app/code/Vendor/Extension/Plugin/Model/ShippingMethodManagement.php
<?php
namespace VendorExtensionPluginModel;
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;
}
}
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0