0

How to get the correct shipping address for an order ID? I am using Magento\Sales\Api\OrderRepositoryInterface 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 Magento\Sales\Api\OrderAddressRepositoryInterface, 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.

3
  • Did you try getList? get($id) where $id is the entity_id column of sales_order_address table.
    – Sohel Rana
    Mar 28 at 11:59
  • @SohelRana: But the problem is that there are exactly two rows with two different entity_id in the sales_order_address table that have the same parent_id. So using getList($orderId) will return a list of two addresses, but I only need one. Mar 28 at 12:13
  • Nope, Check my answer that is what I want to refer you.
    – Sohel Rana
    Mar 28 at 13:26
0

Check the following way:

/** @var \Magento\Sales\Model\Order\AddressRepository $addressRepository */
$addressRepository = $obj->create('Magento\Sales\Model\Order\AddressRepository');

/** @var \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder */
$searchCriteriaBuilder = $obj->create('Magento\Framework\Api\SearchCriteriaBuilder');
$searchCriteriaBuilder
    ->addFilter('parent_id', '3')
    ->addFilter('address_type', 'shipping');
$address = $addressRepository
    ->getList($searchCriteriaBuilder->create());

Don't use the object manager, I used it for a quick check. Use DI instead.

0

When you say you got the wrong shipping address - do you mean you got billing address?

As a quick test, I can retrieve correct shipping address from order repository, but it's a bit convoluted.

  1. You call getExtensionAttributes which returns \Magento\Sales\Api\Data\OrderExtensionInterface
  2. then call getShippingAssignments which returns \Magento\Sales\Api\Data\ShippingAssignmentInterface
  3. then loop assignment and call getShipping which returns \Magento\Sales\Api\Data\ShippingInterface
  1. call getAddress which returns \Magento\Sales\Api\Data\OrderAddressInterface
  2. from there, you can get the individual shipping lines

shipping address code (sorry code kept getting mangled when I tried pasting)

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.