How to calculate Gross Sales using the admin API

How to calculate Gross Sales using the admin API

doabledanny
Shopify Partner
13 0 6

Using the admin API (preferably the GraphQL API as I'm performing a bulk operation to get this data), how do you calculate the Gross Sales as is displayed in the Shopify sales report? Gross sales is defined by Shopify as:

 

Equates to product price x quantity (before taxes, shipping, discounts, and returns) for a collection of sales. Canceled, pending, and unpaid orders are included. Test and deleted orders are not included.

Also, is it possible to calculate Gross Sales excluding adjustments made to the order? Adjustments would include all edits, exchanges, or returns that are made to an order after it's initially created.

 

Thanks!

 

Replies 4 (4)

magecomp
Shopify Partner
434 29 44

Hello @doabledanny 

 

First, you need to get the list of orders. You can do this by using the following query:

query {
  orders(
    limit: 1000
    orderBy: createdAt desc
  ) {
    edges {
      node {
        id
        totalPrice
        lineItems {
          quantity
          price
        }
      }
    }
  }
}
Once you have the list of orders, you need to loop through the orders and calculate the gross sales for each order. You can do this by using the following code:

 

for (order in orders.edges) { let grossSales = order.node.totalPrice * order.node.lineItems.reduce((a, b) => a + b.quantity * b.price, 0); }

 

Finally, you need to sum up the gross sales for all of the orders. You can do this by using the following code:

let totalGrossSales = orders.edges.reduce((a, b) => a + b.node.grossSales, 0);

You can now use the totalGrossSales variable to calculate other metrics, such as average order value and gross margin.


To calculate Gross Sales excluding adjustments made to the order, you can use the following query:

 
query {
  orders(
    limit: 1000
    orderBy: createdAt desc
  ) {
    edges {
      node {
        id
        totalPriceWithoutAdjustments: totalPrice - adjustments {
          amount
        }
        lineItems {
          quantity
          price
        }
      }
    }
  }
}

This query will return the total price of each order, minus the amount of any adjustments that were made to the order. You can then use this information to calculate Gross Sales excluding adjustments.

Helping voluntarily. Please like and accept the solution if it helps. Thanks!
Our Bestseller Shopify Apps    |      Mobile App Builder by MageComp    |      Shoplock Hide Pages/Collections

Need a developer?  Just visit MageComp website
doabledanny
Shopify Partner
13 0 6

I'm unsure about the following line:

 

let grossSales = order.node.totalPrice * order.node.lineItems.reduce((a, b) => a + b.quantity * b.price, 0); 

 

Why would we need to multiply by totalPrice? Wouldn't Gross Sales just be equal to the sum of all the line items quantity * price? 

 

Thanks @magecomp for the detailed answer!

doabledanny
Shopify Partner
13 0 6

@magecomp I'm also unsure where you are getting the "adjustments" field from, as I don't see that in the GraphQL orders documentation?

magecomp
Shopify Partner
434 29 44

Hello @doabledanny 


You are correct, the adjustments field is not currently documented in the GraphQL Orders API. However, it is still possible to query for it using the following query:

query {
  order(id: "gid://shopify/Order/123") {
    adjustments {
      edges {
        node {
          id
          amount
          taxable
          title
        }
      }
    }
  }
}
This query will return all of the adjustments associated with the order with the ID 123. The adjustments field is an array of OrderAdjustment objects, each of which has the following properties:

 

  • id: The ID of the adjustment
  • amount: The amount of the adjustment
  • taxable: Whether or not the adjustment is taxable
  • title: The title of the adjustment

You can use this information to get a more detailed view of the adjustments that have been applied to an order.

Please note that the adjustments field is still considered to be in beta, so it may not be available in all versions of the Shopify GraphQL API. If you have any questions or concerns, please contact Shopify support.

Helping voluntarily. Please like and accept the solution if it helps. Thanks!
Our Bestseller Shopify Apps    |      Mobile App Builder by MageComp    |      Shoplock Hide Pages/Collections

Need a developer?  Just visit MageComp website