Query based orders from Shopify via GraphQL using PHP

Kiranrohra
3 min readMay 23, 2021

Shopify is an e-commerce platform for online stores and retail point-of-sale systems. As Shopify’s motive to make commerce better for everyone. But it is not possible for all that is why they empower third-party developers to create apps. In Shopify, developer has an extensive access to Shopify’s core features and one of them is Admin Api

The Admin Api is available through two ways:

  1. REST Api
  2. GraphQL

In this story, I am not going in the depth of REST or GraphQL Api, feel free to access above links.

The main goal of this story is to help the Shopify developers who want to apply multiple filters on orders, product, customer or other endpoints.
Once I had a task to extract the orders from shopify store where financial_status is paid OR financial_status is pending and Tag not equal to exported. In the start of a task, there was only one filter so it was easy to extract the orders from store using REST Api but when I had three filters, two on same field (financial_status is paid or pending) and including not operator it was hard with REST Api so after spending few hours I came up GraphQL Admin Api. Hope you find it useful.

Before going in depth of GraphQL api, lets first setup the environment

Setup the environment:

  1. Create Shopify Partners account by following link https://www.shopify.com/partners
  2. For php, you need a server and IDE. If you are running the script locally then need a xampp or wamp server and you could use any IDE for writing the script such as notepad++, Sublime, Atom
  3. Generate the api credentials from Shopify admin, follow these steps:
    i. From your Shopify admin, go to Apps.
    ii. Click Manage private apps.
    iii. Click Create a new private app.
    iv. In the App details section, enter a name for your app, and an emergency developer email.
    v. In the Admin API section, select the areas of your store that you want the app to access.
    vi. Grant the Order “Read & Write” Access
    vii. Click Save
    viii. Create App
After saving, the app details look like this

After setup, let’s write the URL request syntax in php. The URL format for authentication consists of five parts.

  1. username: The Api key that you generated
  2. password: the Api Password
  3. shopname: the name of your development store
  4. Api-version
  5. resource: The Api endpoint.

For example: https://{username}:{password}@{shopname}.myshopify.com/admin/api/{api-version}/{resource}.json
As in the blog we are discussing about graphql api so the resource will be graphql.json

Now talk about the structure of GraphQL Api.

The topic of tutorial to apply multiple values in a filter for example “get the orders from shopify store where financial_status is paid OR financial_status is pending AND tag is NOT exported”
In above example we applied three conditions/filter:

  1. financial_status = paid
  2. financial_status = pending
  3. TAG != “exported”.

Means the query includes three operators (AND, OR, NOT). The syntax to write query in GraphQL:

In the above code, orders has three parameters:

  1. first: return up to first n orders
  2. reverse: true means return starting orders from a list
  3. query: multiple filters on orders

Further it has connections and fields
Connections:

  1. shippingLine: Line item that contains the shipping costs.
  2. lineItems: List of the order’s line items.

Fields:

  1. id: Globally unique identifier of an order
  2. name: Unique identifier for the order that appears on the order. For example, #1000 or _Store1001.
  3. createdAt: Date and time when the order was created in Shopify.
  4. tags: A comma separated list of tags associated with the order.
  5. displayFinancialStatus: Financial status of the order.
  6. transactions: list of all transactions or payment method of an order.
  7. discountCode: Discount code provided by the customer.
  8. customer: Detail of the customer who placed the order such as name, address.
  9. shippingAddress: Mailing address for shipping provided by the customer.

In the final step, create a post array and send a post request to Shopify store via CURL.

Here you find the complete code: https://github.com/kiranrohra/shopify/blob/0a97b407e46246eedc8a830ccf183d07165c7273/query%20based_orders.php

PS -> share your suggestions/feedback in comment section.

--

--

Kiranrohra

Computer Science student at Technical University of Berlin and Web developer at Merconic.de