Attribute B2B orders
This guide describes how to use the purchasingEntity field on the Order object to correctly attribute orders to the buyer, whether it's an individual customer (D2C) or a company (B2B). The purchasingEntity field returns a union type that resolves to one of two types:
Customer(D2C): An individual customer placed the order.PurchasingCompany(B2B): A company placed the order through a company contact. ThePurchasingCompanyobject includes the company, contact, and company location associated with the order.
The customer field always returns the individual who placed the order, which for B2B orders is the company contact, not the purchasing company itself. On shops that process B2B orders alongside D2C orders, apps that rely solely on Order.customer will attribute B2B orders to the company contact rather than the company, which can cause incorrect reporting, broken analytics, and misattributed order data.
Anchor to Comparing D2C and B2B order dataComparing D2C and B2B order data
The following examples show how the same fields return different data depending on the order type:
Individual (D2C) order
B2B order
The B2B order still has a customer (the individual contact who placed it), but purchasingEntity correctly identifies the purchasing company and its location.
Anchor to Before and after: fixing order attributionBefore and after: fixing order attribution
The following examples show how a typical order attribution integration can misattribute B2B orders, and how to fix it using purchasingEntity.
Anchor to Incorrect implementationIncorrect implementation
The following integration reads Order.customer, which misattributes B2B orders to the individual contact:
This produces wrong results for B2B orders:
Anchor to Correct implementationCorrect implementation
The following integration reads Order.purchasingEntity and branches on __typename to correctly route both order types:
This correctly routes both order types:
Anchor to Query examplesQuery examples
The following queries show how to retrieve buyer attribution data for both single orders and bulk operations using the GraphQL Admin API.
Anchor to Query buyer attribution for a single orderQuery buyer attribution for a single order
The following order query (OrderBuyerAttribution) uses inline fragments to return the appropriate fields for both D2C and B2B orders. For a D2C order, purchasingEntity resolves to Customer and returns the individual buyer's details. For a B2B order, it resolves to PurchasingCompany and returns the company, the contact who placed the order, and the company location associated with it:
POST https://{shop}.myshopify.com/api/{api_version}/graphql.json
GraphQL query
JSON response
Anchor to Check the order type with ,[object Object]Check the order type with __typename
__typenameThe __typename field returns "Customer" for D2C orders and "PurchasingCompany" for B2B orders. Use this to branch your app logic:
The following JavaScript snippet shows how to handle the __typename from the query response:
Anchor to Bulk query for orders with buyer attributionBulk query for orders with buyer attribution
The following orders query fetches multiple orders with correct buyer attribution across both order types: