Fetching a years worth of orders

I am using the following PHP script to fetch orders for a single customer over the period of the last year, i.e. longer than 60 days. It does not return any data. I am using an access token with over 60 days access, which used to work with the REST API. I am wondering if you can tell me why my query is not working for fetching the last years data. Which should be around 10 orders.

// Customer email
$customerEmail = $_GET["email"];

$fromDate = "\"2025-01-01T00:00:00+10:00\"";

$customerQuery = <<<GQL
query getCustomerByEmail {
  customers(first: 1, query: "email:$customerEmail") {
    edges {
      node {
        id
        email
        firstName
        lastName
      }
    }
  }
}
GQL;

$payload = json_encode(["query" => $customerQuery]);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://$shop.myshopify.com/admin/api/2024-10/graphql.json");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    "Content-Type: application/json",
    "X-Shopify-Access-Token: $token",
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
curl_close($ch);

$data = json_decode($response, true);

print_r($data);

if (empty($data['data']['customers']['edges'])) {
    die("Customer not found.");
}

$customerId = $data['data']['customers']['edges'][0]['node']['id'];
echo "Customer ID: $customerId\n";

$ordersQueryString = "customer_id:".str_replace("gid://shopify/Customer/", "", $customerId)." processed_at:>=$fromDate";

$ordersQuery = <<<GQL
query getOrders(\$ordersQuery: String!) {
  orders(first: 250, query: \$ordersQuery, sortKey: PROCESSED_AT, reverse: true) {
    edges {
      node {
        id
        name
        processedAt
        totalPriceSet { shopMoney { amount currencyCode } }
        lineItems(first: 100) {
          edges {
            node {
              id
              title
              quantity
              sku
              variantTitle
              product {
                id
                title
                featuredImage { url altText }
              }
            }
          }
        }
      }
    }
    pageInfo {
      hasNextPage
      endCursor
    }
  }
}
GQL;

$payload = json_encode([
    "query" => $ordersQuery,
    "variables" => ["ordersQuery" => $ordersQueryString]
]);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://$shop.myshopify.com/admin/api/2024-10/graphql.json");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    "Content-Type: application/json",
    "X-Shopify-Access-Token: $token",
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
curl_close($ch);

$ordersData = json_decode($response, true);

echo "<pre>";
print_r($ordersData);
echo "</pre>";

To confirm, are you using the read_all_orders scope, and have requested permission via the dashboard?

Hey @Robert_Palmer!

I tested this on my dev store and it successfully returned orders from January onwards filtered by customer ID. Here’s the raw GraphQL that worked:

query {
  orders(
    first: 10
    query: "customer_id:8575543246997 AND processed_at:>='2025-01-01'"
    sortKey: PROCESSED_AT
    reverse: true
  ) {
    edges {
      node {
        id
        name
        processedAt
        totalPriceSet { shopMoney { amount currencyCode } }
        lineItems(first: 100) {
          edges {
            node {
              id
              title
              quantity
              sku
              variantTitle
              product {
                id
                title
                featuredImage { url altText }
              }
            }
          }
        }
      }
    }
    pageInfo {
      hasNextPage
      endCursor
    }
  }
}

Comparing this with your PHP script, it looks like the issue is in your query string construction - you’ve got escaped quotes around the date that are breaking the search syntax. When you set $fromDate = "\"2025-01-01T00:00:00+10:00\""; and interpolate that into the query, it could be creating literal \" characters in the search string which the parser can’t handle.

To confirm what @Luke mentioned - this does assume you have read_all_orders scope provisioned and approved in your Partner Dashboard, which it sounds like you do since you mentioned having >60 days access.

If the above doesn’t help, feel free to log the response headers for another attempt (or use the above raw GraphQL via a HTTP client like Postman or Insomnia) and share the x-request-id here in the thread - I’ll use it to find the request in our logs and be able to say for sure what’s going wrong!

Got it working by changing the escape characters around the string to be single quotes. Also had to double check the token, I was using a limited token, not the read_all_orders token.

1 Like