Seems like Magento doesn't have a way to get all orders by shipping addresses. This is obviously because of the following:

  1. Magento doesn't store addresses as unique entities, this means every time there is an order, Magento will create a new entity for that address even if already exists and is exactly the same.
  2. Because its a new entity, every time an order is placed it will have its unique entity_id which is then stored against the sales_order table under the shipping_address_id column. This means we will not be able to easily check how many orders have been done under a specific address because they all have different entity_id (unique keys).

So has anyone every come across this before or can you think of a way of achieve this?


Here is some context why I need this.

We basically want a way to flag addresses as trusted if it has x amount of non-refunded orders between z timeframe. When a customer checks out the input address will be lookup on a table with trusted addresses if it is found then it is sent to our fraud system to do extra validations with the address set as trusted.

  • load all order addresses and use a foreach loop to compare them, maybe? Jan 18, 2018 at 16:50
  • @simonthesorcerer hmm not really an efficient way. I'll add a proper description when I get time, so it makes more sense. Jan 18, 2018 at 16:52
  • @simonthesorcerer I say its not an efficient way because when you have millions or orders that can really degrade the site performance. Jan 18, 2018 at 17:01
  • yes, it didn't sound to me like you would want to do it regularly on the fly. can you maybe add the business use case? Jan 18, 2018 at 17:02

1 Answer 1


So done some research, this is obviously going to take a loot of resources and time to do on the fly. Here is my current approach to this issue which seems to work pretty well.

Because we can't really get orders by addresses in a easy way, every time an order is place a new entry is added for the address being used under sales_order_address. I came up with this query which concatenates the street, city and country of the sales_order_address and then groups all other matching addresses which doing a JOIN to the sales_order table to check if the entity_id of the sales_order table record matches the parent_id of the sales_order_address table.

     CONCAT(order_addresses.`street`, order_addresses.`city`, order_addresses.`country_id`) AS address,
     COUNT(*) AS 'order_count'
     FROM `sales_order_address` AS `order_addresses`
         LEFT JOIN `sales_order` AS `sales` ON sales.`entity_id` = order_addresses.`parent_id`
             WHERE order_addresses.`address_type` = "shipping"
                 AND sales.`base_total_refunded` IS NULL
                 AND sales.`status` = "complete"
                 AND sales.`state` = "complete"
                 AND DATE(sales.`created_at`) >= DATE(NOW() - INTERVAL [THIS_IS_A_SYSTEM_VALUE] MONTH)
     GROUP BY address
     HAVING order_count > [THIS_IS_A_SYSTEM_VALUE]

The above query will be run occasionally every month and once a customer places an order there will be an observer which will look up into a temporary table which was created from the above SELECT and then if the address is trusted it will sent the required information to our fraud system.

Here is a link to my git repo where I will be posting useful SQL queries for Magento 2.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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