# Tipps & Tricks

# Filter: JSON Encode

Mit dem Twig Filter json_encode kann ein komplettes JSON Objekt mit allen Daten erstellt werden.

# Beispiel:

{{ order|json_encode|raw }}
1

# Associations auflösen

Bei manchen Events werden die Associations, wie z.B. billingAddress von Shopware nicht vollständig aufgelöst.

Mit:

{% 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

kann auf Daten zugegriffen werden.

# Beispiel:

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

# Fehlermeldung bei Zugriff auf Variablen

# Beispiel

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

Diese Meldung kann erscheinen, wenn eine Variable nicht gefüllt oder nicht zur Verfügung steht. Um das Problem zu lösen, muss geprüft werden, ob die Variable verfügbar ist.:

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

oder

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