Using [$ShippingOptionsCount] to check for failed carrier APIs

Sometimes calling a carrier plugin (API) can fail because the carrier’s service is temporarily down. In Shipping Director, after an Option rule you can reference the $ShippingOptionsCount variable in expressions to check the number of shipping methods returned by an external shipping provider.  If no methods are returned, then you can have alternate shipping methods/rates so that your customer can continue checkout.  For example, you could have

100    Option    FedEx    true                         Shipping.FedEx

120    Option    UPS      [$ShippingOptionsCount] = 0  Shipping.UPS

Note that you can't use OptionExit on your plugin's rule (because Exit will stop processing further rules), so you do have to be careful about what rules follow.  An alternative would be to have an "End" rule which you could $Goto - e.g.

100    Option    FedEx    true                         Shipping.FedEx

110    Integer   $Goto    [$ShippingOptionsCount] = 0 ? 0 : 9999    

100    Option    UPS      true                         Shipping.UPS

...  other rules ...

9999   String    "The End"

( $Goto 0 just goes to the next line.  I.e., "if no options then go to next line else got to the end")

P.S. You may want to consider “normalizing” the carrier method names using the Name Expression on the Option rule – e.g. both “FedEx Ground” and “UPS Ground” can be shown to the customer as “Economy Shipping”, and “FedEx Priority” and “UPS Next Day” can show as “Priority Shipping”.

Comments
Leave your comment
: