Disable a shipping method based on a condition

Here we’re considering the ordertotal of the cart.

di.xml file

<?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\Shipping">
        <plugin name="restrict_shippingmethod" type="JJ\CartDiscount\Plugin\ShippingMethod" sortOrder="500" />
    </type>
</config>
<?php
namespace JJ\CartDiscount\Plugin;

use Magento\Checkout\Model\Session as CheckoutSession;

class ShippingMethod
{
    protected $checkoutSession;

    public function __construct(
        CheckoutSession $checkoutSession
    ) {
        $this->checkoutSession = $checkoutSession;
    }
    public function aroundCollectCarrierRates(
        \Magento\Shipping\Model\Shipping $subject,
        \Closure $proceed,
        $carrierCode,
        $request
    ) {
        $quote = $this->checkoutSession->getQuote();
        $orderTotal = $quote->getBaseGrandTotal();
        \Magento\Framework\App\ObjectManager::getInstance()->get('Psr\Log\LoggerInterface')->info($orderTotal);
        if ($carrierCode == 'shiphawk') {
            return false;
        }
        return $proceed($carrierCode, $request);
    }
}

Leave a comment

Your email address will not be published. Required fields are marked *