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

[vc_cta h2=”The Below Code Works for the Following Plugins:”]

[/vc_cta]

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 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 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 wintom 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.

 

 

2 thoughts on “Hide Shipping methods when Shipping classes (exist) / (do not exist) in cart.”

  1. Hi Author,

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

    Regards
    Don

Comments are closed.

Scroll to Top