There are a few blogs that show how to use ErrorExit to prevent the customer from checking out; like this one.
Here's how to prevent checkout if the cart has products that don't all have the same vendor:
There are a few blogs that show how to use ErrorExit to prevent the customer from checking out; like this one.
Here's how to prevent checkout if the cart has products that don't all have the same vendor:
You use ErrorExit rather than OptionExit when you want to show an error message to the customer, and not allow them to continue the checkout process. In nopCommerce 2.50, they introduced a hidden setting: shippingsettings.returnvalidoptionsifthereareany; the default is 'true' which is to show successful options if any. The setting was added so that when using multiple external shipping plugins\carriers, and one of the carriers returns with successful shipping methods, that it would still present them to the customer, rather than showing the failed carrier's error message.
If you check all your error conditions up front, then the default setting is OK. If however, you check for errors in the middle of Option records, then you may want to change the setting to 'false' if using ErrorExit.
If you've set up your records as multiple OptionExit records expecting that the first matching one will be the only shipping method offerred to the customer, you might follow it with a final ErrorExit in case none of the conditions match:
OptionExit <some condition> ...
OptionExit <some condition> ...
ErrorExit true ... "Error Message"
In this case either setting is OK.
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.