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.