shopify-api-php icon indicating copy to clipboard operation
shopify-api-php copied to clipboard

How to paginate orders

Open abishekrsrikaanth opened this issue 4 years ago • 7 comments

I am using the API version 2020-01. How do I go about paginating the orders?

Here is the code I have so far

$client = new Client($credential, 'shop_ name.myshopify.com', [
            'metaCacheDir' => './tmp',
        ]);


        $orders = $client->getOrderManager()->findAll([
            'since_id' => 'order_id_goes_here',
            'limit'    => 250,
            'status'   => 'any',
        ]);

Not quite sure, how to proceed after this.

abishekrsrikaanth avatar Jan 02 '20 21:01 abishekrsrikaanth

Did you solve this? I'm currently working on upgrading my script to use pagination.

Here's my code - it runs fine, but for some reason is returning no orders in my testing. It might help you, and if you've already solved it, I'm curious how you did it.

$client = new Client($credential, 'shop_name.myshopify.com', [
    'metaCacheDir' => './tmp',
]);

$pagination = $client->getOrderManager()->paginate([
    'limit' => 20,
    'created_at_min' => $created_at_min,
]);
$orders = $pagination->current();
update_orders($orders); // internal fn to do things with orders
while ($pagination->hasNext()) {
    $orders = $pagination->next();
    update_orders($orders); // internal fn to do things with orders
}

benholmen avatar Mar 11 '20 15:03 benholmen

I tried that and had the same issue, couldn't debug further to figure out the issue in the package, has a bit of a time crunch. I had to write my own function to get the next page information from the header and pass that to the request to get the next page.

https://shopify.dev/tutorials/make-paginated-requests-to-rest-admin-api

Here is the function that I created, hope it helps to fix the issue:

/**
     * Gets next page info string for use in pagination
     *
     * @param $headerLine
     *
     * @return string
     */
    public function parseNextPageInfo($headerLine)
    {
        if ($headerLine) {
            $matchData = [];
            if (preg_match("/<([^>]*)>; rel=\"next\"/", $headerLine, $matchData)) {
                // found rel="next"
                $query = parse_url($matchData[1], PHP_URL_QUERY);
                $pairs = explode("&", $query);
                foreach ($pairs as $p) {
                    [
                        $key,
                        $value,
                    ] = explode("=", $p);
                    if ($key == "page_info") {
                        return $value;
                    }
                }
            }
        }

        return false;
    }

    public function hasNextPageInfo()
    {
        return !empty($this->getNextPageInfo());
    }

    public function getNextPageInfo()
    {
        return $this->parseNextPageInfo($this->client->getLastResponse()->getHeaderLine('Link'));
    }

abishekrsrikaanth avatar Mar 12 '20 11:03 abishekrsrikaanth

Did you solve this? I'm currently working on upgrading my script to use pagination.

Here's my code - it runs fine, but for some reason is returning no orders in my testing. It might help you, and if you've already solved it, I'm curious how you did it.

$client = new Client($credential, 'shop_name.myshopify.com', [
    'metaCacheDir' => './tmp',
]);

$pagination = $client->getOrderManager()->paginate([
    'limit' => 20,
    'created_at_min' => $created_at_min,
]);
$orders = $pagination->current();
update_orders($orders); // internal fn to do things with orders
while ($pagination->hasNext()) {
    $orders = $pagination->next();
    update_orders($orders); // internal fn to do things with orders
}

I think it should be:

$pagination = $client->getOrderManager()->paginate([
    'limit' => 20,
    'created_at_min' => $created_at_min,
    'status' => 'any'
]);

baorv avatar Mar 13 '20 02:03 baorv

Bob - I think you're right about the status=any addition. I've also tested that but had no luck.

Since I posted this comment I've done a lot of troubleshooting and I'm getting the same empty result set even when I hit the API directly with curl. I've found a group of users reporting the same issue - I think it is a Shopify API issue not a slince/shopify-api-php issue.

benholmen avatar Mar 13 '20 03:03 benholmen

The issue was not with slince/shopify-api-php - it was due to a change in the Shopify Order API. My app was configured as a sales channel and they changed the API response to only return orders made through my app.

I worked with a Shopify rep to remove the sales channel privilege from my app (not reversible) and I now have access to 60 days of orders.

benholmen avatar Mar 21 '20 18:03 benholmen

Hello In version 2.4.1, you can get page info like this

$nextPageInfo = $pagination->getNextPageInfo();
$prevPageInfo = $pagination->getPrevPageInfo();

$products = $pagination->current($nextPageInfo);

slince avatar Mar 23 '20 05:03 slince

It is awesome! Thank you so much @slince.

imandydoan avatar Mar 23 '20 06:03 imandydoan