<p>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.</p>

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

<p>You can either do:</p>

<pre><code>{% if order.billingAddress %}
 {{ order.billingAddress.firstName }}
{% endif %}
</code></pre>

<p>Or use the null-coalescing operator in twig:</p>

<pre><code>{{ order.billingAddress.firstName ?? '' }}
</code></pre>

<p><a href="https://twig.symfony.com/doc/2.x/templates.html#other-operators" rel="nofollow noreferrer">https://twig.symfony.com/doc/2.x/templates.html#other-operators</a></p>

<p>Hope that helps.</p>

CC BY-SA 4.0
2020-01-23T23:39:52.123Z
2020-01-23T23:39:52.123Z
Luke Holder
<p>This used to work in commerce 1: <code>{{ order.billingAddress.firstName }}</code> within a for loop of <code>craft.orders.all()</code>.</p>

<p>No longer in Commerce 2. If I use this syntax I get </p>

<blockquote>
  <p>Impossible to access an attribute ("firstName") on a null variable.</p>
</blockquote>

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

<pre><code>object(craft\commerce\models\Address)#2512 (35) {
  …
  ["firstName"]=&gt; string(6) "Sophie"
  …
}
</code></pre>

<p>Why can't I access this? Also <code>| first</code> doesn't help. I also tried several versions with <code>customer</code> or <code>primaryBillingAddress</code> as mentioned in the <a href="https://docs.craftcms.com/commerce/v2/changes-in-commerce-2.html#twig-template-changes" rel="nofollow noreferrer">changes doc</a> But no luck so far.</p>

<craft3><plugin-craftcommerce2>
Get address data of Order
# Get address data of Order

(Asked by: username_0 on 2020-01-19)

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.

**Tags:** craft3, plugin-craftcommerce2


---

## 1 answers

### Answer by username_1 on 2020-01-23. Score: 1 | Accepted answer

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.


---

