<p>You could also use <code>$order-&gt;getShippingAddress()</code> which is even shorter! :)</p>

CC BY-SA 4.0
2022-06-02T14:34:35.550Z
2022-06-02T14:34:35.550Z
iamkeir
<p>You can use the <a href="https://docs.craftcms.com/commerce/api/v3/craft-commerce-services-addresses.html#public-methods" rel="noreferrer">Addresses</a> service to get the shipping address:</p>
<pre><code>$address = craft\commerce\Plugin::getInstance()-&gt;getAddresses()-&gt;getAddressById($order-&gt;shippingAddressId);
</code></pre>
<p>This is the more direct method to get the address, and it's a bit shorter to type too!</p>

CC BY-SA 4.0
2022-04-07T12:55:22.293Z
2022-04-07T12:55:22.293Z
Martin Spain
<p>I'm trying to add an address column to my orders index but I'm having trouble correctly getting the actual address.</p>
<p>From my code below, when I dump on <code>$orderShippingAddress</code> I get the ID of a valid address. But when I dump on <code>$address</code> I get <code>null</code>.</p>
<p>Can anyone spot where I'm going wrong? For reference, I was working from here: <a href="https://craftcms.com/docs/commerce/3.x/addresses.html#get-current-customer-address-by-id" rel="nofollow noreferrer">https://craftcms.com/docs/commerce/3.x/addresses.html#get-current-customer-address-by-id</a></p>
<pre><code>Event::on(
    Order::class,
    Order::EVENT_REGISTER_TABLE_ATTRIBUTES,
    function (RegisterElementTableAttributesEvent $event) {
        $event-&gt;tableAttributes['address'] = ['label' =&gt; 'Address'];
    }
);  

Event::on(
    Order::class,
    Order::EVENT_SET_TABLE_ATTRIBUTE_HTML,
    function (SetElementTableAttributeHtmlEvent $event) {
        $order = $event-&gt;sender;
        $orderShippingAddress = $order-&gt;shippingAddressId;
        $address = craft\commerce\Plugin::getInstance()-&gt;getCustomers()-&gt;getCustomer()-&gt;getAddressById($order-&gt;shippingAddressId);
        dump($shippingAddress);
        if ($event-&gt;attribute === 'address') {
             $event-&gt;html = $address;
        }
    }
);
</code></pre>

CC BY-SA 4.0
2022-04-07T09:06:33.513Z
What do you get if you just use the `addresses` service? `$address = craft\commerce\Plugin::getInstance()->getAddresses()->getAddressById($order->shippingAddressId);`
Martin Spain
CC BY-SA 4.0
2022-04-07T09:55:24.430Z
Nice one! That does it!
supazu
CC BY-SA 4.0
2022-04-07T12:37:36.307Z
@MartinSpain if you add that as an answer I'll mark it accepted
supazu
<plugin-craftcommerce>
Get order shipping address in module
# Get order shipping address in module

(Asked by: username_0 on 2022-04-07)

I'm trying to add an address column to my orders index but I'm having trouble correctly getting the actual address.
From my code below, when I dump on $orderShippingAddress I get the ID of a valid address. But when I dump on $address I get null.
Can anyone spot where I'm going wrong? For reference, I was working from here: https://craftcms.com/docs/commerce/3.x/addresses.html#get-current-customer-address-by-id
Event::on(
    Order::class,
    Order::EVENT_REGISTER_TABLE_ATTRIBUTES,
    function (RegisterElementTableAttributesEvent $event) {
        $event->tableAttributes['address'] = ['label' => 'Address'];
    }
);  

Event::on(
    Order::class,
    Order::EVENT_SET_TABLE_ATTRIBUTE_HTML,
    function (SetElementTableAttributeHtmlEvent $event) {
        $order = $event->sender;
        $orderShippingAddress = $order->shippingAddressId;
        $address = craft\commerce\Plugin::getInstance()->getCustomers()->getCustomer()->getAddressById($order->shippingAddressId);
        dump($shippingAddress);
        if ($event->attribute === 'address') {
             $event->html = $address;
        }
    }
);

**Tags:** plugin-craftcommerce

> **Comments:**
> **username_1**: What do you get if you just use the `addresses` service? `$address = craft\commerce\Plugin::getInstance()->getAddresses()->getAddressById($order->shippingAddressId);`  
> **username_0**: Nice one! That does it!  
> **username_0**: @MartinSpain if you add that as an answer I'll mark it accepted  

---

## 2 answers

### Answer by username_1 on 2022-04-07. Score: 6 | Accepted answer

You can use the Addresses service to get the shipping address:
$address = craft\commerce\Plugin::getInstance()->getAddresses()->getAddressById($order->shippingAddressId);

This is the more direct method to get the address, and it's a bit shorter to type too!


---

### Answer by username_2 on 2022-06-02. Score: 2

You could also use `$order->getShippingAddress()` which is even shorter! :)


---

