Exclude Ground Shipping Based on State

First, I recommend checking if a Country/State/Zip has been provided, because the “Estimate Shipping” will work even if they are not selected, and depending on rate calculation method (shipping plugin), you could get funny results, or an error. (Only US and Canada have states.)   For this, we use the ErrorExit record Type.  This will Exit if the Expression (condition) evaluates to true, and the customer will see the message calculated in the Description Expression.  Note, that this is a String Expression, so be sure to include quotes for a literal value.

Order

10

Type

ErrorExit

Name

Missing Country State Zip

Expression

ShippingAddress.Country = null or ShippingAddress.ZipPostalCode = null or ("US,CA".Contains(ShippingAddress.Country.TwoLetterIsoCode) and ShippingAddress.StateProvince = null)

Description Expression

"Please Enter Country, State, and Postal Code"

Then, let’s say we want to prevent shipping if the State is Alaska or Hawaii.  We would use the ErrorExit type here too:

Order

20

Type

ErrorExit

Name

No Shipping to Alaska and Hawaii

Expression

"AK,HI".Contains(ShippingAddress.StateProvince.Abbreviation)

Description Expression

"Sorry, we can’t ship to Alaska or Hawaii"

The condition Expression could have also been written as

Expression

ShippingAddress.StateProvince.Abbreviation = "HI" or ShippingAddress.StateProvince.Abbreviation = "AK"

But when using an “or”, be sure to use parenthesis if you have a more complex expression that includes “and”s too.

Alternately, you may just want to exclude just Ground shipping to Alaska and Hawaii.  For example, you want to use the Shipping.ByWeight rate calculation method, but don’t want to offer Ground to Alaska or Hawaii.  Then the above would instead be entered with an Option type:

Order

20

Type

Option

Name

Ship By Weight but no Ground for AK and HI

Expression

true

Rate Expression

Shipping.ByWeight

Name Expression

"AK,HI".Contains(ShippingAddress.StateProvince.Abbreviation) and [$Name].Contains("Ground") ? "" : [$Name]

For the Option type, the Expression field is a condition.  If it evaluates to true, then the option record is processed, otherwise it's skipped.  The Rate Expression can either be a decimal expression calculating the actual rate you want to charge, or the name of a shipping plugin.  The Name Expression calculates the shipping option Name shown to the customer.  However, if the Name Expression evaluates to blank (""), then Shipping Director will exclude the Option.  The [$Name] is a built-in variable that contains the Name of the shipping option as returned by the other rate caclulation plugin.  So, here we are saying "if the state is Alaska or Hawaii, then exlude the named option, else show the named option".  The If-Then-Else is achieved using the " ? : " ternary operator -  condition ? if-true : if-false.

Tags :  ExcludeOptionErrorExit
Comments
Leave your comment
:
Chinajiyong@gmail.com
Created on: 3/23/2015 11:27 PM
I want to use "Shipping Director" 3.3 plugin to restrict to shipping to states/provinces of US country.

how to configure the records? Can you give some examples?
support@nopTools.com
Created on: 3/24/2015 7:39 AM
The above example shows how to use ErrorExit to prevent shipping to Alaska and Hawaii (AK,HI).  You would do similar for other stated using their two letter state codes.
eh@novastar.net
Created on: 4/26/2017 3:45 PM
Is there an easier way to exclude a shipping method for ALL states other than one state? For example, we want to include Local Pickup "shipping" for only MI and exclude that option for all other states.
support@nopTools.com
Created on: 4/26/2017 8:40 PM
Yes, that is easy to do; just two Option records.  The first for the pickup with Expression condition state specific, and then follow it with another Option record with Expression 'true' (applies to all), and reference whatever shipping rate provider you already use - e.g. SHipping.ByWeight:

Type                    Option
Name                  Pickup in Store
Expression           State = "MI"
Rate Expression   0.00

Type                    Option
Name                  Ship By Weight
Expression           true
Rate Expression   Shipping.ByWeight
sample@email.tst
Created on: 6/26/2023 5:31 PM
1
sample@email.tst
Created on: 6/26/2023 5:31 PM
1