Skip to content

How to get the shipping address by order ID?

How to get the correct shipping address for an order ID? I am using MagentoSalesApiOrderRepositoryInterface to get the order information by order ID, but there are no methods for getting the shipping address, despite a lot of people on the internet claiming otherwise.

I considered using MagentoSalesApiOrderAddressRepositoryInterface, and I am able to return a shipping address, but it is not the correct one.

It took me a long time to understand why the get() method from OrderAddressRepositoryInterface wasn’t returning the right data when passing the same $orderId parameter as in OrderRepositoryInterface, but then I found out that Magento inserts two entries in the sales_order_address table for each entry in the sales_order table – for some reason.
The address row from sales_order_address is then relationally matched with the right order in the sales_order table by the column shipping_address_id in the sales_order table, which corresponds to the entity_id in the sales_order_address table.

It would be easy to use OrderAddressRepositoryInterface if there was a getShippingAddressId() method declared in OrderRepositoryInterface, but there is none!

So how can I find/match the right shipping address with a particular order ID? I would love to hear suggestions for how to use the service layer to solve this, and not the persistence or domain layers.