Hi everyone, I’m having an issue with finding the order that has the nearest due date. As shown in the screenshot, I have a list of orders with two that are currently payment pending, and order 1006 has the nearest due date — November 13, 2025. Has anyone encountered this issue before? I’d really appreciate it if you could share a solution. Thanks in advance!
Hi @Ph_m_Th_Thu_Ha! The orders query doesn’t have a built-in way to sort by payment due dates, the available sort keys cover things like created date and financial status, but not payment terms.
You’ll need to fetch the orders with payment terms and sort client-side by the dueAt field from payment schedules:
query {
orders(first: 50, query: "financial_status:pending OR financial_status:partially_paid") {
nodes {
id
name
displayFinancialStatus
paymentTerms {
paymentSchedules(first: 5) {
nodes {
dueAt
balanceDue {
amount
currencyCode
}
}
}
}
}
}
}
Once you have the results, filter out orders without payment terms and sort by the dueAt timestamp in your application code to find the order with the nearest due date. Keep in mind that not all orders will have payment terms, only those with deferred payment options will have this data.
Thank you, Donal, for sharing about the order filter. It would be very convenient if we could filter directly from the API.
I appreciate the feedback - I’ll pass it along to the relevant team internally.
Just so I can pass on as much context as possible, what’s the use case? Are you building something like a payment dashboard where you need to surface orders by urgency?
Our use case is that we want to display upcoming-due orders to customers in the customer account or in the portal, so that our customers — or their customers — can see which orders are nearing their due date and make payments on time.
We also have another issue related to retrieving the totalSpent of a location within a specific time range. From what I’ve found, Shopify currently only provides the all-time totalSpent. We hope the Shopify team can consider updating this, as it would be extremely helpful for customers to track their total purchases over time.
Below is the query we are using to fetch the all-time totalSpent:
query companyLocation($id: ID!) {
companyLocation(id: $id) {
totalSpent {
amount
currencyCode
}
ordersCount {
count
precision
}
}
}
Thanks for following up @Ph_m_Th_Thu_Ha!
Unfortunately, companyLocation.totalSpent is an all-time value and doesn’t support date range filtering. The field returns a MoneyV2 scalar representing the lifetime total for that location.
Good news though - as of October 1, 2025, ShopifyQL is back via shopifyqlQuery in the GraphQL Admin API, and the sales dataset supports filtering by company location which could be a viable alternative:
{
shopifyqlQuery(query: "FROM sales SHOW total_sales WHERE company_location_name = 'Example Company Location' GROUP BY month SINCE 2024-01-01 UNTIL today ORDER BY month") {
tableData {
columns {
name
dataType
displayName
}
rows
}
parseErrors
}
}
This returns the total sales for that specific company location within your date range, already aggregated by month. You can also use company_location_id instead of company_location_name if you prefer filtering by the numeric ID.
Requirements:
-
Your app needs the
read_reportsaccess scope -
If querying protected customer data, you’ll need to request access in the Partner Dashboard
Check out the B2B reports examples in the Help Center for more query patterns.
Thanks Donal, for the information about ShopifyQL. I’m also familiar with this technique. However, I’m a bit concerned because requiring the read_reports scope from others would involve the store’s analytics, and they might have concerns about installing the app.
That’s a fair concern - read_reports does give access to store analytics data which some merchants are hesitant about.
You could query orders directly and aggregate the totals client-side. The orders query supports filtering by date range and only requires read_orders scope:
query getCompanyLocationOrders {
orders(
first: 250
query: "created_at:>='2025-01-01' AND created_at:<='2025-11-18'"
) {
pageInfo {
hasNextPage
endCursor
}
edges {
node {
id
createdAt
totalPriceSet {
shopMoney {
amount
currencyCode
}
}
purchasingEntity {
... on PurchasingCompany {
location {
id
name
}
}
}
}
}
}
}
Then filter for orders where purchasingEntity.location.id matches your company location ID and sum the totalPriceSet.shopMoney.amount values. You’ll need to paginate through results using pageInfo.endCursor if there are more than 250 orders.
Note: purchasingEntity is only populated for B2B orders, so this approach works specifically for the B2B use case you described. Regular B2C orders will have an empty purchasingEntity object.
This approach is less efficient than ShopifyQL (you’re doing the aggregation yourself instead of the database), but it keeps your scope requirements more focused on order data rather than analytics. Let me know if you have any other questions!
Thank you, Donal, for the information about the report data
You’re very welcome!

