1

This used to work in commerce 1: {{ order.billingAddress.firstName }} within a for loop of craft.orders.all().

No longer in Commerce 2. If I use this syntax I get

Impossible to access an attribute ("firstName") on a null variable.

But when I do: {{ dump(order.billingAddress) }} then I see the firstName and it is clearly not null:

object(craft\commerce\models\Address)#2512 (35) {
  …
  ["firstName"]=> string(6) "Sophie"
  …
}

Why can't I access this? Also | first doesn't help. I also tried several versions with customer or primaryBillingAddress as mentioned in the changes doc But no luck so far.

1

My guess is that one of the orders in the array of orders has a null billingAddress, and the dump you did was for just one of the orders.

If you are going to access the address firstName through the order's billing address, but the address might be null you need to do a check.

You can either do:

{% if order.billingAddress %}
 {{ order.billingAddress.firstName }}
{% endif %}

Or use the null-coalescing operator in twig:

{{ order.billingAddress.firstName ?? '' }}

https://twig.symfony.com/doc/2.x/templates.html#other-operators

Hope that helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.