Writing availability scripts

Availability scripts allow you to really refine when a certain object is available in a certain context. For example:

  • a delivery scenario is only available when the events in the order are at least 20 days in the future
  • a certain payment scenario is not available for a certain event
  • a custom field should only be filled in for events with a certain tag

Availability scripts can be defined for:

  • payment scenarios
  • delivery scenarios
  • custom fields of type Checkout

Availability

When defining availability, you will always first select a subset of the sales channels. The availability script will further refine this selection.


Simple script

The script should be written in Javascript and should always return a boolean. It’s not necessary to write a function, only the script itself.

For example, this simple script will return true when the current date is between 01/01/2015 and 01/04/2015:

1 var now = new Date();
2 var start = new Date(2015,0,1);
3 var end = new Date(2015,0,4);
4 return start.getTime() < now.getTime() && now.getTime() < end.getTime();

Script with context

In the script there are a few context variables available:

  • order: The current order (as defined in the public data model) is always available.
  • saleschannelid

For example this script will return true when tickets for a certain event are in the order:

1 if (!order) return false;
2 
3 for (var i = 0 ; i < order.tickets.length ; i++) {
4     var t = order.tickets[i];
5     if (t.eventid == 777711) {
6         return true;
7     }
8 } return false;

The order will contain a lookup section that allows you to lookup related objects to the order. For example: you can retrieve the pricetypeid for alle tickettypepriceids in the order. The idea is similar to the order lookup that is available when creating order documents or order mails, but the content of the lookup here is much more limited for performance reasons.

Important: keep in mind that the order context will sometimes be empty, so make sure your script handles the case of an empty order correctly.


Questions?

We're always happy to help! Send us an e-mail