How To

Magento 2: Hide Other Shipping Methods if Free Shipping is Available

Hello, Magento Folks,

We have already learned How you can use input masks at checkout in the telephone field of Magento 2. Today, I am going to explain to you how you can hide all other shipping methods in your Magento 2 store when a free shipping method is available.

When you have enabled the free shipping method in your store, it will show with other shipping methods as well. It does not harm you in any way, but you might want to hide other methods when free shipping in your store is enabled.

While developing a Magento site for one of our clients, we came across similar requirements to hide other shipping methods when free shipping is available. We thought about why not write a blog on the topic. So, in this article, we are going to explain to you how to do that programmatically. Using the below codes, it is possible to hide other shipping methods.

Step 1: Let’s create a “Registration.php ” file at the below-given path.
app\code\Vendor\Extension

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Vendor_Extension',
    __DIR__
);

Step 2: Now, Let’s create the “Module.xml” file at given below path,
app\code\Vendor\Extension\etc

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Vendor_Extension" setup_version="1.0.0" schema_version="1.0.0" />
</config>

Step 3: Also, let’s create a “di.xml” file at the given below path,
app\code\Vendor\Extension\etc

<?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\Quote\Model\ShippingMethodManagement">
        <plugin name="shipping_method_management" type="Vendor\Extension\Plugin\Model\ShippingMethodManagement" disabled="false"/>
    </type>
</config>

Step 4: Now, the last step is to create a “ShippingMethodManagement.php” file at the give below path,
app\code\Vendor\Extension\Plugin\Model

<?php
namespace Vendor\Extension\Plugin\Model;

class ShippingMethodManagement {

    public function afterEstimateByExtendedAddress($shippingMethodManagement, $output)
    {
        return $this->filterOutput($output);
    }
    private function filterOutput($output)
    {
        $free = [];
        foreach ($output as $shippingMethod) {
            if ($shippingMethod->getCarrierCode() == 'freeshipping' && $shippingMethod->getMethodCode() == 'freeshipping') {
                $free[] = $shippingMethod;
            }
        }
        if ($free) {
            return $free;
        }
        return $output;
    }
}

 

That’s it for today! Now you are able to successfully hide other shipping methods. After implementing these codes, it will only show a free shipping method. You can customize these codes according to your requirements. Also, check out Shipping and Payment Method per Customer Group Extension which allows the store owner to manage shipping and payment methods for specific customer groups.

At last, it will make us feel good if you give it a thumbs up and share it with others as well. If you need help with implementing these codes or with anything else, you can always contact our support team.

Happy Coding!

Click to rate this post!
[Total: 16 Average: 4.6]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

View Comments

Recent Posts

Mastering Tailwind CSS in Laravel: A Comprehensive Guide

Tailwind CSS has emerged as a powerful utility-first CSS framework, offering developers a unique approach…

2 days ago

React Native or Flutter in 2024

The mobile app development field has witnessed a rapid revolution over the past few years.…

3 days ago

Magento 2: How To Call JS on the Checkout Page?

Hello Magento mates, Today we will learn to add a call JS on the checkout…

6 days ago

Boost Your SEM Game: Unveiling the Top 10 Tools for Marketers in 2024

Business survival in today’s digital world has become extremely difficult. Using traditional marketing techniques is…

1 week ago

Five Essential Payroll Compliance Tips for eCommerce Startups

Are you setting up a payroll system for your eCommerce startup? Ensuring compliance with myriad…

1 week ago

Optimizing Laravel Blade: Unlocking Advanced Fetcher Techniques

In the expansive universe of Laravel development, Blade serves as the stellar templating engine, propelling…

1 week ago