3

Tell me how you can hide the rest of the delivery methods and leave only free if the order amount is more than 99

I tried the implementation that is on the stack for lower versions, it either does not work or is deprecated, I need it for version 2.3

Thanks

2 Answers 2

2

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!

2
  • 1
    Thanks, you very help me Nov 8, 2019 at 10:55
  • 1
    Can you please upvote for me :) Nov 8, 2019 at 11:02
1

Disable the free shipping delivery method. Add the free shipping functionality (more than 99) through a cart price rule.

Your Answer

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

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