An efficient way of listing all product costs?

Highlighted
New Member
1 0 1

I'm making a backend in ASP.NET MVC to manage my product pricing which to begin with will be as simple as Product Cost + Markup %.

 

I'm using the /admin/api/2020-01/inventory_items.json which a mandatory Ids field which then I need to loop through all the products variants which I've got from /admin/api/2020-01/products.json to get the InventoryItemId then to take batches of 100 at a time to send to Shopify and keep looping through until I've got all the data. It seems very overly complicated for such a simple task?

 

Is this the best approach or is there a better approach?

 

Thanks for reading.

1 Like
Highlighted
Shopify Staff (Retired)
Shopify Staff (Retired)
624 103 124

Hey @Spinks ,

 

If you're using the Shopify REST Admin API, then the method you describe is the most efficient way to get this data. Unfortunately REST does not allow you to make connections between resources very easily, so you are sometimes stuck with having to make multiple calls to multiple endpoints to get all the data you need.

 

However if you were willing to look into the Shopify GraphQL Admin API, you could get this same data much easier. In fact, all you would need to do is make this one API call to get all of the Unit Costs associated to the product variants of all of the products on your store. However for this call, you would have to iterate through the results with cursor based pagination to get through all the products on your store

{
  products(first:10) {
    pageInfo {
      hasNextPage
      hasPreviousPage
    }
    edges {
      cursor
      node {
        variants(first:40) {
          edges {
            node {
              inventoryItem {
                unitCost {
                  amount
                  currencyCode
                }
              }
            }
          }
        }
      }
    }
  }
}

Or if you do not want to deal with cursor based pagination and just get all of your results once, you can use use the GraphQL BulkOperations Run Query: https://shopify.dev/tutorials/perform-bulk-operations-with-admin-api

mutation {
  bulkOperationRunQuery(
   query: """
   {
  products {
    pageInfo {
      hasNextPage
      hasPreviousPage
    }
    edges {
      cursor
      node {
        id
        variants {
          edges {
            node {
            id
              inventoryItem {
                id
                unitCost {
                  amount
                  currencyCode
                }
              }
            }
          }
        }
      }
    }
  }
}
    """
  ) {
    bulkOperation {
      id
      status
    }
    userErrors {
      field
      message
    }
  }
}

 

Hassain | Developer Support Specialist @ Shopify
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Click Accept as Solution 

0 Likes