August 16, 2022

Pankaj Rajpurohit 0

Today we are going to see how to hide Shipping Method in Magento2. In the previous article, we learned how to hide payment methods. Magento2 provides a number of online and offline shipping methods for delivery.

Sometimes merchants are required to hide shipping methods based on some condition, let’s take one example Flat rate shipping method should not be displayed if the order subtotal amount is less than equal to 100 then we have to customize the code to hide the Flat rate shipping method. There are many more scenarios like maximum order amount to hide specific shipping payment method, specific customer group, front-end/admin side, or specific customer address, in those cases we have to customize code to make working according to it. This is basic code that must be required to hide the shipping method for all those cases plus some additional code required to make it complete.

Magento2 natively does not provide to hide shipping methods for specific conditions. In order to hide the shipping method on the front-end checkout page and the admin to create the order page, let’s follow the below steps.

Method to hide a Shipping Method in front-end and admin side Magento 2

1) Create the di.xml file at app/code/Vendor/Extension/etc directory

<?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="hide_flatrate_shipping_method" sortOrder="10"
                type="Cogentcommerce\RestrictShippingMethod\Plugin\HideFlatRateShippingPlugin"/>
    </type>
</config>

2) Create HideFlatRateShippingPlugin.php file at Vendor\Extension\Plugin directory

<?php
namespace Cogentcommerce\RestrictShippingMethod\Plugin;

use Closure;
use Magento\Checkout\Model\Session as checkoutSession;
use Magento\Shipping\Model\Shipping;

class HideFlatRateShippingPlugin
{
    /**
     * @var checkoutSession
     */
    protected $_checkoutSession;

    public function __construct(
        checkoutSession $checkoutSession
    )
    {
        $this->_checkoutSession = $checkoutSession;
    }

    public function aroundCollectCarrierRates(
        Shipping $subject,
        Closure  $proceed,
                 $carrierCode,
                 $request
    )
    {
        $quote = $this->_checkoutSession->getQuote();
        if (!$quote) {
            return;
        }
        $subtotal = $quote->getSubtotal();
        // Hide Flat Rate shipping method if order subtotal is less than 100
        if ($subtotal <= 100 && $carrierCode == 'flatrate') {
            return false;
        }

        $result = $proceed($carrierCode, $request);
        return $result;
    }
}

Using the above-mentioned code, you can hide a shipping method on the front-end and admin side.

If you are required to hide the shipping method in some other conditional base then feel free to ask us in a comment or contact us at info@cogentcommerce.com

Thank you for reading…We hope it helps you out. For any assistance contact us.

Follow us on Linkedin

Related Post

Leave a Reply

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