# Tips & Tricks

# Filter: JSON Encode

With the Twig filter json_encode a complete JSON object with all data can be created.

# Example:

{{ order|json_encode|raw }}
1

# Resolve associations

For some events, the associations, such as billingAddress, are not completely resolved by Shopware.

With:

{% set billingAddress = null %}
{% for address in order.addresses %}
    {% if address.id is same as order.billingAddressId %}
        {% set billingAddress = address %}
    {% endif %}
{% endfor %}
1
2
3
4
5
6

data can be accessed.

# Example:

{% if billingAddress is not same as null %}
{{ billingAddress.firstName }}
{% endif %}
1
2
3

# Error message when accessing variables

# Example

Failed rendering string template using Twig: Impossible to access an attribute ("translated") on a null variable
1

This message can appear if a variable is not filled or is not available. To solve the problem, it is necessary to check whether the variable is available:

{% if product.manufacturer.translated.name is defined %}{{ product.manufacturer.translated.name }}{% endif %}
1

or

{{ product.manufacturer.translated.name ?? '' }}
1