Shipping methods example scripts

This guide provides a series of example scripts that implement the shipping methods API.

Filter shipping methods based on configuration

This script filters shipping methods when both of the following conditions are met:

  • The shipping method name matches the shippingMethodName field from the configuration, or Unknown if the field wasn't set in the configuration.
  • The total price of the checkout (purchase proposal) is greater than the threshold field from the configuration in CAD, or $100.00 CAD if the field wasn't set in the configuration.

Rename first shipping method

This script renames the first shipping method to Renamed Shipping Method! if there are one or more shipping methods.

Sort shipping methods

This script sorts shipping methods by name.

  • If no configuration is provided, then shipping methods are sorted in ascending order.
  • If the sortDirection field of the configuration is set to ascending, then shipping methods are sorted in ascending order.
  • If the sortDirection field of the configuration is set to descending, then shipping methods are sorted in descending order.
  • If the sortDirection field of the configuration is set to anything else, then the script raises an error.

Add a message

This is a complex and advanced script. It uses a configuration list, which allows merchants to customize the script. Each item in the configuration list contains the following fields:

  • provinceCodes: A comma-separated list of two-digit province codes.
  • provinceCodeMatchType: Valid values: All, Include, or Exclude.
  • countryCode: A two-digit country code.
  • message: A string.

The script iterates over all configuration list items to find one where the countryCode matches the purchase proposal's country code, and the purchase proposal's province code matches the provinceCode based on provinceCodeMatchType. If a match is found, then every shipping option has the message appended to the end of its name with a dash (-) as a separator.

Example configurations

You have an order with country code CA and province code ON, and your configuration includes the following item:

The name of each shipping method will have " - Not destined for the prairies" appended to it.

Similarly, you have an order with country code US and province code NY, and your configuration includes the following item:

The name of each shipping method will have " - Destined for the USA" appended to it.

Next steps