Hide Shipping methods when Shipping classes (exist) / (do not exist) in cart.

The below code snippet can be used to hide shipping methods based on whether shipping classes are present in cart or not.

Add the following code to your functions.php or anywhere relevant.

If you are looking for a plugin to do this job, visit ELEX Hide Shipping Methods Plugin. Using this plugin, You can easily hide shipping methods based on various conditions.

– If shipping ID “42” is present in cart, then the snippet will hide “Free Shipping”.
– If shipping ID “42” is not present in cart, then the snippet will hide these services :
            ‘wf_shipping_ups:03’, – UPS Ground
            ‘wf_shipping_ups:02’, – UPS 2nd Day Air
            ‘wf_shipping_ups:01’ –  Ups Next Day Air
-If shipping ID “43” is not present in cart, then the snippet will hide “Free Shipping”.
What do i mean by shipping ID “42” and “43”?
Don’t worry. To make it simple, developers use Shipping IDs to map shipping classes in code.
How do I map it?

You can do that pretty easily. Just go to Products –> Shipping Classes and you will find the corresponding Shipping ID for a shipping class as shown in the two screenshots below :

Shipping Class Wood

Shipping Class Wood

Shipping Class Metal

Shipping Class Metal

What about shipping methods? I cannot understand the code that is to be used in the code snippet.
No Problem. You can easily find the service by the following method :
– Go to the cart page and fetch the rates for an order as shown below :
In the screenshot below, you can see “UPS Next day Air Early AM”. Take the mouse pointer just above the service name, do a right click and then click on inspect.

A small window called developers tools will open at the bottom of the page. You can find the service code from here.

Cart

Cart

Use this code if you want to hide this shipping method for a particular shipping ID/Class.

 

 

Comments (2)

  • admin113
    admin113

    Hi Author,

    can we hide Hide Shipping methods in case of multiple shipping packages.

    Regards
    Don

    April 5, 2017 at 6:47 pm
    • admin113
      admin113

      Hi Besty,

      can you please resolve my problem.

      Regards
      Don

      April 5, 2017 at 7:20 pm

Leave a Reply