You can create di.xml
file in your custom module here
app/code/Vendor/Module/etc/di.xml
Content for this file is..
<?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\Shipping\Model\CarrierFactory">
<plugin name="free_shipping_above_99" type="Vendor\Module\Plugin\Model\CarrierFactory" sortOrder="10"/>
</type>
</config>
Now you need to create one Plugin
file here in your custom module
app/code/Vendor/Module/Plugin/Model/CarrierFactory.php
Content for this file is..
<?php
namespace Vendor\Module\Plugin\Model;
class CarrierFactory
{
/**
* Core store config
*
* @var \Magento\Framework\App\Config\ScopeConfigInterface
*/
protected $_scopeConfig;
/**
* @var \Magento\Framework\ObjectManagerInterface
*/
protected $_objectManager;
protected $_checkoutSession;
/**
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
* @param \Magento\Framework\ObjectManagerInterface $objectManager
*/
public function __construct(
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
\Magento\Framework\ObjectManagerInterface $objectManager,
\Magento\Checkout\Model\Session $_checkoutSession
) {
$this->_scopeConfig = $scopeConfig;
$this->_objectManager = $objectManager;
$this->_checkoutSession = $_checkoutSession;
}
public function aroundGet(
\Magento\Shipping\Model\CarrierFactory $subject,
$Closure,
$carrierCode
) {
/*Change your logic here based on your requirement.*/
$grandTotal = $this->_checkoutSession->getQuote()->getGrandTotal();
if($grandTotal > 99 && $carrierCode != 'freeshipping'){
return false;
}
/*Change your logic here based on your requirement.*/
$className = $this->_scopeConfig->getValue(
'carriers/' . $carrierCode . '/model',
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
);
if (!$className) {
return false;
}
$carrier = $this->_objectManager->get($className);
$carrier->setId($carrierCode);
return $carrier;
}
public function aroundCreate(
\Magento\Shipping\Model\CarrierFactory $subject,
$Closure,
$carrierCode,
$storeId = null
) {
/*Change your logic here based on your requirement.*/
$grandTotal = $this->_checkoutSession->getQuote()->getGrandTotal();
if($grandTotal > 99 && $carrierCode != 'freeshipping'){
return false;
}
/*Change your logic here based on your requirement.*/
$className = $this->_scopeConfig->getValue(
'carriers/' . $carrierCode . '/model',
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
$storeId
);
if (!$className) {
return false;
}
$carrier = $this->_objectManager->create($className);
$carrier->setId($carrierCode);
if ($storeId) {
$carrier->setStore($storeId);
}
return $carrier;
}
}
You can change your logic in Plugin based on your requirements, this will work when order's grand total is more than 99 then it will only display FREE SHIPPING and if order total is below 99 then it will display all other shipping methods and not display FREE SHIPPING there.
Hope this will help you!