Magento2 provides many shipping methods but many times store owners require to hide all other shipping methods for those products that are available for free shipping. By default, Magento doesn’t provide this functionality to hide all shipping methods excluding free shipping when the free shipping method is enabled that’s why in this post, we will explain how to hide other shipping methods if the free shipping method is enabled.
Let’s see the steps and follow the below steps to remove other shipping methods.
Step 1: Firstly, need to create Registration.php file path at the following
app\code\Webiators\DisableShippingMethods
1 2 3 4 5 6 |
<?php use Magento\Framework\Component\ComponentRegistrar; ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Webiators_DisableShippingMethods', __DIR__); |
Step 2: Now you need to create Module.xml
file path at the following
app\code\Webiators\DisableShippingMethods\etc
Code
1 2 3 4 5 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="Webiators_DisableShippingMethods" setup_version="1.0.0" schema_version="1.0.0" /> </config> |
Step 3: Create di.xml file path at the following
app\code\Webiators\DisableShippingMethods\etc
Code
1 2 3 4 5 6 7 |
<?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="shipping_method_management" type="Webiators\DisableShippingMethods\Plugin\Model\HideAllShippingMethod" disabled="false"/> </type> </config> |
Step 4: Create HideAllShippingMethod.php file path at the following
app\code\Webiators\DisableShippingMethods\Plugin\Model
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?php namespace Webiators\DisableShippingMethods\Plugin\Model; class HideAllShippingMethod { public function afterEstimateByExtendedAddress($shippingMethodManagement, $output) { return $this->filterOutput($output); } private function filterOutput($output) { $free = []; foreach ($output as $shippingMethod) { if ($shippingMethod->getCarrierCode() == 'freeshipping' && $shippingMethod->getMethodCode() == 'freeshipping') { $free[] = $shippingMethod; } } if ($free) { return $free; } return $output; } } |
We hope this post is very helpful to understand about how to hide other shipping methods if the free shipping method is enabled in Magento 2. If you have any query related to this post or your Magento 2 store then you can contact us!
Thank You!