Magento2 How to disabled free shipping method at frontend

Magento2 How to disabled free shipping method at frontend.

In this article, I am going to show Magento2 How to disabled free shipping method at frontend.

At magento2, it difficult to disable a shipping method at frontend and Enable at admin area.

At first, I target to override  Free shipping method class  Magento\OfflineShipping\Model\Carrier\Freeshipping ‘s method  collectRates() using Plugin around method

 

And defined under  di.xml  at app/code/Devamitbera/CoreRewrite/etc/  and try disable by using Area code

 

But this is not working at Magento checkout page. I was going strange as the condition as was not working as excepted.

Then, I was started to debug and find that issue is this Magento area.At Checkout page, Magento is estimating Shipping method current quote using of   its Rest API   and that why area code at plugin is webapi_rest.

That why the condition was not working!.

 

So, again trying to find the solution of that issue.

  • Create a new field is_frontent_checkout at Quote table using setup install script InstallData.php  at at app/code/Devamitbera/CoreRewrite/etc/Setup and set it value to 0 or 1  and by default set value 0.

  • As I want to set it to  1 when Customer cart a product frontend that why I had fired an observer on the event   checkout_cart_save_before  and declare events.xml at app/code/Devamitbera/CoreRewrite/etc/frontend.

 

Observer class  AssignCurrentStoreIsFrontend.php at app/code/Devamitbera/CoreRewrite/Observer

 

  • Now disabling the free shipping method when that field value  1.So, I have changed in the Plugin class code.

 

 

In this logic, you can disable any shipping method.

 

Hope, it will help other people.

Related Post:

  1. How to get magento2 base url
  2. Magento2 redirection from Observer
  3. Magento2 get admin url

One thought on “Magento2 How to disabled free shipping method at frontend

  • Hi Amit,

    You will remember that we could not get the featured products to be listed at $0.00 shipping when other (non-free) products were included in the cart. And if we started with the free shipping item, then the rest of the products’ shipping charges was ignored.

    The client finally asked me. “what if we set the weight to “0” lb? That did it! In fact, Magento 2 has a config item for each product asking whether the item has weight (or not). Result: zero weight means no charge in most cases. I was able to mix free shipping into any shopping cart mix and the weight of the free shipping items was ignored.
    John

Leave a Reply

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