How to sort Categories & Items

A menu is sent to your Menu Update webhook as a set of linked collections. This guide explains how these collections fit together and how to interpret the payload to reconstruct the menu structure and display it correctly.

Menu structure

A menu payload has four collections. categories is an array; the other three are objects keyed by _id.

The menu's hierarchy and display order are determined by each item's subProducts array. This array contains the _id values of the items one level below, listed in the order they should be displayed to the customer.

When reconstructing the menu tree, use the subProducts array to establish parent-child relationships and determine display order. The _id values act as references between collections, allowing you to identify the corresponding items and build the complete menu hierarchy.

CollectionproductTypeWhat it issubProducts contains
categories[]–A section of the menu, such as Starters or Drinks.Product IDs
products{}1An item that can be ordered on its own. It can also be offered as an option in a modifier group.Modifier group IDs
modifierGroups{}3A set of options shown as one choice, with rules on how many can be selected.Modifier IDs, or product IDs for products offered as options
modifiers{}2An option that changes a product, such as a topping or a cooking preference.Nested modifier group IDs

_id and plu

Every item has an _id and a plu:

  • _id describes the menu structure. Use it to follow subProducts from one level to the next.
  • plu identifies the item itself. Use it to specify which items to include when creating an order.

Sort order

Display order comes from the same subProducts arrays that define the menu structure:

  • Categories are shown in the order they appear in the categories array.
  • Everything else is shown in the order its ID appears in its parent's subProducts array.

A product can appear in more than one category, so read its position from each category's subProducts, not from the products object. Order can change each time the menu is published, so always use the latest payload.

subProductSortOrder
This is a legacy field and shouldn't be used to interpret sorting. Use the order of subProducts instead.

Building the menu tree

To turn the payload into a menu:

  1. Start with each category's subProducts.
  2. Look up each ID, then do the same for that item's subProducts.
  3. Stop when an item's subProducts is empty.

Every level follows the same pattern, so one recursive function handles all of them, including nested modifiers. Each ID exists in only one collection, so you can merge products, modifierGroups and modifiers into a single lookup first.

Example

Hover over or select an ID in the payload, or an item in the menu, to see where it's defined and referenced:

idCategoryidProductidModifier groupidModifier
Menu Update payload
{  "categories": [    {      "_id": "63cf…c0a0",      "name": "Steaks",      "subProducts": ["63cf…c05d"]    },    {      "_id": "63cf…c0a1",      "name": "Drinks",      "subProducts": ["63cf…c06a"]    }  ],  "products": {    "63cf…c05d": {      "_id": "63cf…c05d",      "name": "Delicious Steak Frites",      "plu": "STK-01",      "productType": 1,      "price": 1850,      "subProducts": ["63cf…c061", "63cf…c064"]    },    "63cf…c065": {      "_id": "63cf…c065",      "name": "Side Salad",      "plu": "SID-02",      "productType": 1,      "price": 0,      "subProducts": []    },    "63cf…c06a": {      "_id": "63cf…c06a",      "name": "Ginger Beer",      "plu": "DRK-04",      "productType": 1,      "price": 350,      "subProducts": []    }  },  "modifierGroups": {    "63cf…c061": {      "_id": "63cf…c061",      "name": "Cooking instructions",      "plu": "CK-01",      "productType": 3,      "min": 1,      "max": 1,      "subProducts": ["63cf…c062", "63cf…c063"]    },    "63cf…c064": {      "_id": "63cf…c064",      "name": "Choose a side",      "plu": "SD-01",      "productType": 3,      "min": 1,      "max": 1,      "subProducts": ["63cf…c065"]    }  },  "modifiers": {    "63cf…c062": {      "_id": "63cf…c062",      "name": "Rare",      "plu": "CK-R",      "productType": 2,      "price": 0,      "subProducts": []    },    "63cf…c063": {      "_id": "63cf…c063",      "name": "Well Done",      "plu": "CK-WD",      "productType": 2,      "price": 0,      "subProducts": []    }  }}
What the customer sees
Steaks…c0a0
Delicious Steak Frites£18.50…c05d
Cooking instructionsRequired, choose 1…c061
Rare…c062
Well Done…c063
Choose a sideRequired, choose 1…c064
Side Salad…c065
Drinks…c0a1
Ginger Beer£3.50…c06a
Resolve the menu tree in JavaScript
// menu = the JSON body received on https://yourwebhook.com/menu_update
const items = { ...menu.products, ...menu.modifierGroups, ...menu.modifiers };

const resolve = (id) => {
  const item = items[id];
  return { ...item, subProducts: (item.subProducts || []).map(resolve) };
};

const tree = menu.categories.map((category) => ({
  name: category.name,
  items: category.subProducts.map(resolve),
}));

Run against the example payload, this produces:

Steaks
  Delicious Steak Frites       productType 1
    Cooking instructions       productType 3
      Rare                     productType 2
      Well Done                productType 2
    Choose a side              productType 3
      Side Salad               productType 1
Drinks
  Ginger Beer                  productType 1

Did this page help you?