In this article, we’ll explain how to hide a shipping method when certain conditions get applied.

In some scenarios store owners may want to restrict shipping options because:

  • a product has big dimensions and weight, hence it can’t be shipped in any standard way,
  • frozen storage holding conditions don’t allow to deliver a product in time
  • shipping is restricted due to the customer’s location (country, region, city)
  • the Cart Total doesn’t allow a shopper to qualify for any shipping offers,
  • etc.

The default selection of Magento shipping options doesn’t offer much flexibility. It’s only possible to restrict shipping for certain counties and regions or when the Cart Total is less than the set amount.

If you think that this is not enough, read on.

I’ll tell you how to create your own plugin that will let you restrict shipping options according to your business model, goals and conditions.

NOTE

I won’t go through the specific steps necessary to create an extension for Magento 2. You can see how to do that here or here.

In order to dynamically disable any given shipping method, we need a new set of classes in our extension – these are 2 plugins. The 1st will be in charge of shipping method validation, while the 2nd will be filtering them.

In a nutshell, every method that doesn’t pass validation will get marked as “is_disabled“, and at the next step, all the “is_disabled” methods get filtered and deleted from the main list.

Let’s roll.

* Since the approach I’m going to describe is based on the principles used in the Shipping Suite extension, I’ll be using the name of the vendor ‘MageWorx’ and the name of the extension ‘ShippingRules’. NOTE! In order to avoid errors, you need to replace those with your own names! 

First, let’s create a plugin (class) that will be used for filtering. Here is how to:

 

We run 3 checks in the body of our plugin:

1.

– ensures that at the point of entry we get what we need: that is an instance of a shipping method. In case it is not, we just return it as it is.

2.

– next, we check whether the current method in on the list of methods for filtering. In case it is not, we return it as it is.

3.

– is the last and the main check. If the flag is applied to the real value, the current method gets disabled: $result->setIsDisabled(true);

We’ll try to filter at least one product added to cart and having weight more than 100 standard units (pounds or kilos).
The methods, available for filtering are ‘flatrate_flatrate’,’ups_XDM’,’ups_XPR’,’ups_WXS’,’carrier_method’. They are hard-coded and stored in the array $filtableMethods.

We operate on the code using the entire shipping method code which, as a rule, consists of the carrier’s code ($result->getCarrier()) and the code of the method itself ($result->getMethod()) connected with “_”.

Products needed for the check are taken directly from the current client’s quote.
NOTE

A little piece of code in the array class constructor will let you define the quote both on the frontend and in the backend (providing an order was created in the Admin panel).

Using \Magento\Framework\App\State we can check in which area we are at the moment, and select the necessary quote:
1. \Magento\Backend\Model\Session\Quote for Admin
2. \Magento\Checkout\Model\Session for the Frontend

That’s it!

Now we can validate shipping methods and can proceed to the next stage – creating a plugin for disabling invalid (according to our conditions) shipping methods.

This is how to:

 

This plugin is used to fetch all available shipping methods data and check each of them. If some of the methods has the “is_disabled” mark, we just exclude it from the list, which enables us to see only those methods that correspond to the conditions  created in the validator. These methods get excluded on any page of a website, be it an estimate shipping block in shopping cart, or the checkout page.

Now, to check how out plugins work, we need to register them in the file:

NOTE

For descriptive reasons, the plugins are divided into 2 different classes. In an ordinary scenario, you can use just 1 class to modify 2 different methods of the class Magento\Shipping\Model\Rate\Result.

Why it works this way?

Magento\Shipping\Model\Rate\Result is the main Magento 2 class that is responsible for shipping methods procession. Its getAllRates method is used to fetch shipping method lists.

Meanwhile, adding shipping methods is usually done with the help of the ‘append’ method. Having validated methods while appending, and having excluded them from the collection, we can get a list of valid methods.

Validation itself can also be modified.

Our extension has a big special class that stores a collection of custom filters with different conditions (such as  store_id, customer_group_id, date from/to, days of the week, etc.). it also has a separate class with a collection of actions that should be applied to a shipping method (e.g. enable or disable it, rewrite its cost according to different conditions, add extra costs or discounts, etc.)

The result of the 1st plugin in debug looks somewhat like this (for a product with the weight of 1 unit):
1

This is how it looks in shopping cart (ups_WXS is still available, as the weight limit is not exceeded):

2

However, if you change the product weight to 101, this shipping method becomes unavailable:

3

It gets available again if you excluded it from the list:
4

5

At the checkout:

f3d3cac03678318af2d569d757fc7e79

NOTE

You can get the list of all available system shipping methods right in the code the following way:

Where $this->shippingConfig is an instance of the class Magento\Shipping\Model\Config. Using this code, you can create the source model for the selector of shipping methods in the extension’s settings (thus, you won’t have to hard-code it in the plugin).

And last but not least.

If you don’t want to create your own extension for disabling shipping methods, you can use our Shipping Suite solution for Magento 2.

The extension lets you exclude any shipping method whenever needed just in a couple of mouse clicks.
1. Create a new shipping rule with the following condition:

8

2. Select Hide Shipping Method action on the list, and choose which methods should be disabled:

9

NOTE

If you don’t find the ‘weight’ attribute on the list of conditions, check is it is enabled to use in promo rules:

10

Should you have any questions or ideas on how to further improve our extension, please share your opinion in the comments section below.

Comments (1)

Comments are closed.