How to Enable/Disable Shipping Method Programmatically in Magento 2

Online shoppers look forward to shipping charges and it often becomes the decision point for purchase.

Hence, E-commerce store owners need to define the shipping system strategically so that neither the customers are disappointed nor the business has to incur any loss.

In the default Magento 2, the admin can enable or disable shipping method from the backend.

However, if one wants to enable/disable shipping method programmatically in Magento 2 based on specific conditions, follow the below solution.

For instance, you want to enable the free shipping method for a minimum cart total and above. Or, you want to enable a certain shipping service for fixed regions or states only.

If the customer’s orders fulfil such condition, then only the shipping method should be enabled or disabled.

It can be done with the programmatic solution in this post. Or, you may prefer the Magento 2 Shipping Restrictions extension to restrict shipping methods based on the cart, customer attributes, zip codes, and days of the week!

Steps to Enable/Disable Shipping Method Programmatically in Magento 2

  1. Create a plugin in di.xml file and use this below code.
  2. Create a plugin file in Vendor/Extension/Plugin.
    The shipping method will be disabled if free shipping is selected else enables.

    That’s it!

    Any doubts about the solution can be mentioned in the Comments section below. I’d be happy to help.

    Feel free to share the solution with Magento 2 community via social media.

    Thank You. 

magento 2 shipping restrictions extension

Shipping Restrictions

Allows restricting shipping methods based on the cart, customer attributes, zip codes, and days of the week for efficient shipping management.

5
(based on 11 Reviews)
How to Enable/Disable Shipping Method Programmatically in Magento 2Author Magento Badge

Sanjay Jethva

Sanjay is a co-founder at Meetanshi. He is a Certified Magento Developer who loves creating Magento E-commerce solutions. Owing to his contributions in Magento Forums and posting solutions, he is among the top 50 contributors of the Magento community in 2019. When he is not engrossed with anything related to Magento, he loves to play cricket.

2 Comments

  • Robert E Sorenson

    Greetings,
    My problem is very similar to the one you describe.
    I want to disable free shipping method in the Admin Panel> Stores> Configuration> Sales> Shipping Methods> Free Shipping> Enable> No.
    When I change the ‘Enable’ box to No and save it nothing changes.
    In fact any change in the Free Shipping section is not saved. I checked ‘Scope’ is set to ‘Main Website’ (only website).
    How do I debug this? Any assistance will be a big help!
    Thank you!
    Bob Sorenson

Leave a Reply

Your email address will not be published.