This feature needs the MauticCustomObjectsBundle plugin. Make sure it’s installed and active in Mautic.

You can show information from your Mautic Custom Objects (like orders, subscriptions, etc.) directly in your emails or landing pages using Twig Templates.

Getting Custom Object Data

Use the getCustomObjectItems function to fetch data from a specific Custom Object.

getCustomObjectItems(alias, limit, page, orderBy, orderDirection, search, contactId)

What the options mean:

  • alias (Required): The unique name (alias) of your Custom Object (e.g., 'orders').
  • limit: How many items to get (default is 1000).
  • page: Which page of results to get (for pagination, default is 1).
  • orderBy: How to sort the items ('id' or 'name', default is 'id').
  • orderDirection: Sort direction ('ASC' for A-Z, 'DESC' for Z-A, default is 'ASC').
  • search: Text to search for within the items (optional).
  • contactId: Get items linked only to a specific contact ID. Use contact.id for the current contact (optional).

What you get back:

The function gives you a list (array) of items. Each item contains:

  • id: The item’s ID.
  • name: The item’s name.
  • fields: A collection of all the custom field values for that item. You access them using their alias (e.g., item.fields.your_field_alias).

Using the Data

After fetching the items (e.g., {% set myItems = getCustomObjectItems('orders') %}), you can loop through them and show their details:

{% for item in myItems %}
  <p>Order Name: {{ item.name }}</p>
  <p>Order Status: {{ item.fields.status_alias }}</p> {# Example custom field #}
{% endfor %}

Example: Show Recent Changelog Entries

Fetch the 5 newest items from a ‘changelogs’ Custom Object.

{# Get the latest 5 changelog items, sorted by name Z-A #}
{% set changelogs = getCustomObjectItems('changelogs', 5, 1, 'name', 'DESC') %}

{% if changelogs is not empty %}
    <h2>Recent Changelog</h2>
    <ul>
        {% for item in changelogs %}
            <li>
                <h3>{{ item.name }}</h3> {# Show the item's name #}
                <p>
                    Product: {{ item.fields.product_alias }} <br/> {# Show 'product_alias' field #}
                    Version: {{ item.fields.version_alias }} {# Show 'version_alias' field #}
                </p>
                <div>
                    {{ item.fields.content_alias | raw }} {# Show 'content_alias' field (allow HTML) #}
                </div>
            </li>
        {% endfor %}
    </ul>
{% else %}
    <p>No changelog entries found.</p>
{% endif %}

Example: Show the Current Contact’s Last Order

Fetch the single latest order from an ‘orders’ Custom Object linked to the current contact.

{% if contact.id is defined %} {# Make sure we know the current contact #}
    {# Get the 1 latest order for this contact #}
    {% set lastOrder = getCustomObjectItems('orders', 1, 1, 'id', 'DESC', null, contact.id) %}

    {% if lastOrder is not empty %}
        {% set orderItem = lastOrder|first %} {# Get the first (and only) item from the list #}
        <p>
            Your last order (#{{ orderItem.name }}) was placed on
            {{ orderItem.fields.date_ordered_alias }}. {# Show 'date_ordered_alias' field #}
        </p>
    {% else %}
        <p>You have no previous orders.</p>
    {% endif %}
{% endif %}