# Fetch from User Journey

### forEachProductInJourney Function

`#forEachProductInJourney` is a HandleBars helper function for realtime campaigns which can be used to iterate through the cart object sent within events. You can use it to display and filter products in the cart. For example, you can use this helper function to set up an Abandoned Cart Campaign.

Before using the helper function, it's important to ensure that the events used have the right structure. If these do not contain the cart object, the helper function will not work. For ease of setup, please see the example event below containing the cart object:

```
{
"externalId": "31764034000",
"events": [{
		"properties": {
			"orderId": "50314b8e9bcf000000000000",
			"cart": {
				"total": 500.00,
				"currency": "EUR",
				"products": [
					{
					"id": "507f1f77bcf86cd799439011",
					"sku": "975229855",
					"name": "Item 1",
					"price": 200,
					"quantity": 1,
					"colour": "red",
					"hasAccessories": false
					},
                                        {
					"id": "507f1f77bcf86cd799439011",
					"sku": "975229856",
					"name": "Item 2",
					"price": 200,
					"quantity": 1,
					"colour": "blue",
					"hasAccessories": true
					},
					{
					"id": "507f1f77bcf86cd799439011",
					"sku": "975229857",
					"name": "Item 3",
					"price": 20,
					"quantity": 1,
					"colour": "blue",
					"hasAccessories": false
					}
				]
			}
		},
		"event": "Completed Order"
	}]
}
```

Now that the events are set up correctly, we can target them with the HandleBars function. For our example, we assume that we have a single-step user journey that uses the **Completed Order** event as the campaign trigger. We set up the helper function so that it will only return 3 products with unique colours with a price over 50 EUR sorted by price (descending):

```
{{#forEachProductInJourney '{
  "JourneyStep": [0], 
  "options": {
    "maxResults": 3, 
    "sortBy": "price", 
    "desc": true, 
    "distinctBy": "colour", 
    "filters": [ 
        {"property": "price", 
        "operator": ">", 
        "value": "50"} 
      ]
  }
}'}}

{{name}}
{{quantity}}
{{price}}

{{/forEachProductInJourney}}
```

**JourneyStep** represents the journey step used for the event data extraction (0 is the first step of a journey)

The **options** object allows you to apply filters to your selection (optional):

* **maxResults** lets you set the maximum number of results the function will return
* **sortBy** lets you select the property which will be used to sort the results. You can select any cart property you'd like as the function can sort both alphabetically and numerically
* **desc** lets you set whether the function should return the results descending. **"desc": false** will enable ascending sorting. Results will be sorted based on the value used in the **sortBy** field
* **distinctBy** lets you select what cart property you'd like to use to distinguish returned products by. In our example, we only return products that have unique colours
* **filters** is an array that allows you to add filters to the returned products. In the example above, we filter products that only have a price over 50 EUR. Please use the operators below when setting up the filters


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://documentation.crossengage.io/data-and-engagement-platform/messages/personalization/product-helper-functions/fetch-from-user-journey.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
