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.
Contents
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Vendor_Extension', __DIR__ );
<?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>
<?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>
<?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!
Hello Magento Friends, In today’s blog, I will explain How to Add Tooltip in Checkout…
MongoDB is a popular NoSQL database that offers flexibility and scalability when handling modern web…
In NodeJS, callbacks empower developers to execute asynchronous operations like reading files, handling requests, and…
Hello Magento Friends, In today’s blog, we will learn How to Show SKU in Order…
The "Buy Now" and "Add to Cart" buttons serve as the primary call-to-action (CTA) elements…
Hello Magento Friends, In Magento 2, the checkout process allows customers to choose multiple shipping…
View Comments
public function afterEstimateByAddress($shippingMethodManagement, $output)
{
return $this->filterOutput($output);
}
public function afterEstimateByExtendedAddress($shippingMethodManagement, $output)
{
return $this->filterOutput($output);
}
public function afterEstimateByAddressId($shippingMethodManagement, $output)
{
return $this->filterOutput($output);
}
add above code in your plugin other wise for logged in user it will not work
Hello, This code is working fine for a guest checkout but not for logged in customers. Please check this.
Not Working for registered users, Only working for guest checkout. Anything i need to update?
I'm using magento 2.3.5p1
Due to a lot of demand, we developed this free extension, which you can download from our website
https://magecomp.com/magento-2-shipping-restriction.html
As already pointed out by someone, It's not working for logged in customers. TYI :-)
Due to Community Demand, we developed this free extension, which you can download from our website
https://magecomp.com/magento-2-shipping-restriction.html
not working all shipping method are display
Heya,
This is great but is there a way to hide free shipping when any other paid method is available?
Yes, you can but you need to change the logic in same file Vendor\Extension\Plugin\Model\ShippingMethodManagement.php to achieve your goal.
can u let me know specifically what needs to be changed?
In the same file, you just need to place reverse logic, so your goal can be achieved.
sorry I am very new to this, can you give me the exact code or do I need to pay you to do it?
Please contact us at support@Magecomp.com
Whereabouts in this file do I change the logic? Can you tell me the exact change I need?
Hello Thank you so much for your code which works perfectly but I would like to only hide the shipping method which is flat rate not the other ones how could i change the code to achieve this?
Check this function : function filterOutput($output) where we fetch all the methods and you can put your logic into that one
Its not working for logged in customers
Sadly this did not work for me. Is there something I need to do ensure the code is registered? Thank you!!
https://uploads.disquscdn.com/images/7b716896e924dde9d3e633cea11da78d14fd9bfe8092c9ab486a3ee0c4a8538f.jpg
Confirm you have implemented code properly. Check your module is enabled. Your module is registered in Magento? Is it executing? Make sure any other extension is not conflicting.
Hi, thank you for your tip, I registered and enabled the module and voila it works GREAT!! Thank you again for your code that is exactly what I needed. Cheers...
You are most welcome :) Keep Reading and Sharing.
The code is working for shipping quote, but it doesn't hide default shipping price on checkout page ( /checkout/#shipping ).
Hi, can you confirm this code will work in all areas of the website? If this is a duplicate my apologies as I don't see my previous comment. I see it works in the Shopping Cart and when estimating https://uploads.disquscdn.com/images/9ae6e0321b429ee07ca11454e89d630dea2af05b8bde5e22f82e98e29e83a7bc.png shipping costs but does not work in Order Review or Checkout. Please see attached screen shots. Anyway to have this across the entire site? Cheers... https://uploads.disquscdn.com/images/fc88b62752611fc8bf6faa15a345cc0b6ca8ce2c0cb7da6f9164b6af84a196e2.png https://uploads.disquscdn.com/images/d016da62bec25b023025a5c6e40038a14f486a6389dae8975882e26f4d1a52d2.png
PS. During my testing I just noticed this works perfectly on Estimated Shipping Charges when looking at the Shopping Cart. However when you check out it reverts back to all options? See attached screen shots. Is there a way to hide these as well? Cheers.. https://uploads.disquscdn.com/images/9ae6e0321b429ee07ca11454e89d630dea2af05b8bde5e22f82e98e29e83a7bc.png https://uploads.disquscdn.com/images/d016da62bec25b023025a5c6e40038a14f486a6389dae8975882e26f4d1a52d2.png https://uploads.disquscdn.com/images/fc88b62752611fc8bf6faa15a345cc0b6ca8ce2c0cb7da6f9164b6af84a196e2.png
I am new to Magento so I am not sure how to enable the code. However my site did struggle for a bit so I have a feeling it might be conflicting. How do I see if it is executing? Can you please email me on nsm068@gmail.com with any tips. I appreciate any assistance you can provide. If all else fails I will just delete your code. Thank You
For better performance, I would suggest to put ´break;´ or ´return $free;´ on line 16. In case you choose 2nd option, you don't need lines 18-20 ?
Thanks for your suggestion, Yes we can apply that and it may help for performance, thanks for your contribution,