Add module.xml file in app/code/Magemonkeys/ShippingHide/etc and copy the following code in it:
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd"> <module name="Magemonkeys_ShippingHide" setup_version="2.0.10"> <sequence> <module name="Magento_Shipping"/> </sequence> </module> </config>
Add registration.php in app/code/Magemonkeys/ShippingHide and copy the following code in it:
<?php MagentoFrameworkComponentComponentRegistrar::register( MagentoFrameworkComponentComponentRegistrar::MODULE, 'Magemonkeys_ShippingHide', __DIR__ );
Add Uninstall.php file in app/code/Magemonkeys/ShippingHide/Setup and copy the following code in it:
<?php namespace MagemonkeysShippingHideSetup; use MagentoFrameworkSetupUninstallInterface; use MagentoFrameworkSetupModuleContextInterface; use MagentoFrameworkSetupSchemaSetupInterface; use MagentoConfigModelResourceModelConfigDataCollectionFactory as ConfigCollectionFactory; /** * ShippingHide Uninstall */ class Uninstall implements UninstallInterface { /** * Config Collection Factory * * @var MagentoConfigModelResourceModelConfigDataCollectionFactory */ private $_configCollectionFactory; /** * Initialize Setup * * @param ConfigCollectionFactory $configCollectionFactory */ public function __construct( ConfigCollectionFactory $configCollectionFactory ) { $this->_configCollectionFactory = $configCollectionFactory; } /** * Uninstall DB Schema * * @param SchemaSetupInterface $setup * @param ModuleContextInterface $context * @return void */ public function uninstall(SchemaSetupInterface $setup, ModuleContextInterface $context) { $setup->startSetup(); $this->removeConfig(); $setup->endSetup(); } /** * Remove Config * * @return void */ private function removeConfig() { $path = 'shipping/behavior'; /** @var MagentoConfigModelResourceModelConfigDataCollection $collection */ $collection = $this->_configCollectionFactory->create(); $collection->addPathFilter($path); foreach ($collection as $config) { $config->delete(); } } }
Add config.xml file in app/code/Magemonkeys/ShippingHide/etc and copy the following code in it:
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd"> <default> <shipping> <behavior> <hide>1</hide> </behavior> </shipping> </default> </config>
Add di.xml file in app/code/Magemonkeys/ShippingHide/etc and copy the following code in it:
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd"> <!-- plugin --> <type name="MagentoShippingModelRateResult"> <plugin name="Magemonkeys_ShippingHide" type="MagemonkeysShippingHidePluginShippingModelRateResult" /> </type> </config>
Add system.xml file in app/code/Magemonkeys/ShippingHide/etc/adminhtml and copy the following code in it:
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd"> <system> <section id="shipping"> <group id="behavior" translate="label" showInDefault="1" showInWebsite="1" showInStore="1" sortOrder="310"> <label>Behavior of Methods</label> <field id="hide" translate="label comment" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1"> <label>Hide Mode</label> <comment>Hides any other shipping methods if free shipping is available.</comment> <source_model>MagentoConfigModelConfigSourceYesno</source_model> </field> </group> </section> </system> </config>
Add Data.php file in app/code/Magemonkeys/ShippingHide/Helper and copy the following code in it:
<?php namespace MagemonkeysShippingHideHelper; use MagentoStoreModelScopeInterface; use MagentoFrameworkAppHelperAbstractHelper; /** * Shipping Hide Helper */ class Data extends AbstractHelper { /** * Enabled Config Path */ const XML_CONFIG_ENABLED = 'shipping/behavior/hide'; /** * Check Hide mode Functionality Should be Enabled * * @return bool */ public function isEnabled() { return $this->_getConfig(self::XML_CONFIG_ENABLED); } /** * Retrieve Store Configuration Data * * @param string $path * @return string|null */ protected function _getConfig($path) { return $this->scopeConfig->getValue($path, ScopeInterface::SCOPE_STORE); } }
Add Result.php file in app/code/Magemonkeys/ShippingHide/Plugin/Shipping/Model/Rate and copy the following code in it:
<?php namespace MagemonkeysShippingHidePluginShippingModelRate; use MagemonkeysShippingHideHelperData as ShippingHideHelper; /** * Shipping Result Plugin */ class Result { /** * Helper * * @var MagemonkeysShippingHideHelperData */ protected $_helper; /** * Initialize Plugin * * @param ShippingHideHelper $helper */ public function __construct( ShippingHideHelper $helper ) { $this->_helper = $helper; } /** * Return all Rates in the Result * * @param Result $subject * @param Method[] $result * @return Method[] */ public function afterGetAllRates($subject, $result) { if (!$this->_helper->isEnabled()) { return $result; } $rates = $this->getAllFreeRates($result); return (count($rates) > 0) ? $rates : $result; } /** * Return all free Rates in the Result * * @param Method[] $result * @return Method[] */ public function getAllFreeRates($result) { $rates = []; foreach ($result ?: [] as $rate) { if ($rate->getPrice() < 0.0001) { $rates[] = $rate; } } return $rates; } }
Run CLI Commands
rm -rf var/di/* var/generation/* var/cache/* var/log/* var/page_cache/* var/session/* var/view_preprocessed/* pub/static/* php bin/magento setup:upgrade php bin/magento setup:upgrade php bin/magento setup:static-content:deploy -f php bin/magento cache:clean php bin/magento cache:flush
Result
No method of free shipping
There is a method of free shipping