Advanced Order Routing
Introduction
Occasionally there are scenarios which require business logic that can not be expressed through standard configuration options. Whether this is optimizing on-demand vs traditional fulfillment or routing orders to regional fulfillment centers based upon the customer’s assigned territory, the advanced order routing sub-system allows for merchant defined JavaScript logic to assist in routing items to particular distribution centers or changing shipping methods.
Configuration
The first step is to define the required JavaScript file on the StoreFront file system:
/udf/order-routing.js
This JavaScript file should look like the following template:
function route(order, customer, items) {
// Implement your business logic here.
return order;
}
The simple function receives the order, customer (if a profile is available), and an array of all the item objects associated with the items on the order. Here is what the file should look like within your StoreFront → File Manager.
To make writing the business logic easier the following helper methods are currently available:
function mapShippingMethod(order, from, to)
function doesOrderContainUpsells(order)
function canDistributionCenterHandleAll(order, items, desiredDistributionCenterCode)
function adjustDistributionCenter(order, desiredDistributionCenterCode)
function isUsingDistributionCenter(order, checkDistributionCenterCode)
function changeItemIdToDistributionCenter(order, itemId, distributionCenterCode)
Logging
Logging of the advanced order routing is down through the Integration Logs system. If your script encounters a syntax or runtime error, logs console information, etc. then all that information will be captured in the integration log.
Examples
If the other order doesn’t contain upsells, route to DC 1, else route to DC 2
This sample uses the helper functions provided to determine if the order contains any upsells and then adjusts the distribution centers codes to a particular DC if that DC can handle all of the items. This example helps optimize on-demand fulfillment vs traditional fulfillment.
Force a particular item id to a distribution center if the order is for a specific channel partner
This example looks to see if the order is for a specific channel partner then changes the distribution center for a particular item id.
FAQ
Q) Does this slow down order processing?
A) Advanced order routing introduces an additional stage into the order processing pipeline that sits between the payment processing the the shipping department. This processing takes less than one minute to occur and has no further impact on order flow. The processing of the script is done asynchronously and has zero impact on the user checkout experience.
Q) Can we have different logic per StoreFront?
A) Yes, the advanced routing function is associated with each StoreFront. If an order is placed through a StoreFront that does not have the /udf/order-routing.js script available on the file system then the routing functionality is skipped. If you want to roll out the logic on multiple StoreFronts, we recommend getting the script working on a single StoreFront and then using the file manager copy operation to duplicate the script to other StoreFronts.
Q) What happens if my script encounters an error?
A) All the error details are available within the Integration Log for review. UltraCart Support can assist with further troubleshooting by reviewing these logs and your script file. When an error does occur, nothing on the order is modified and the order moves on to the shipping department.
Q) What fields can be changed on the order?
A) Only them item distribution center code and the order shipping method can be changed at this time.
Q) How long can my script run?
A) The script will time out at 5 seconds. Your script is only doing purge logic operations and should execute in a few hundred milliseconds.
Q) Can I use XYZ JavaScript library?
A) No, you need to stick with vanilla JavaScript in your logic.