Coding Notes Shopify

Cursor-Based Pagination Under Shopify’s Latest API

Shopify will no longer use the page parameter in their latest 2019-07 API changes, here’s an example code for cursor-based pagination using the phpclassic/php-shopify package.

This code was tested on API version 2020-01.

PHP 7.2 + Required

This code assumes you’re executing the PHP script from your terminal / command prompt.

Install the Dependencies

composer require phpclassic/php-shopify

The Code

<?php

require('vendor/autoload.php');

$config = array(
    'ShopUrl' => 'website.myshopify.com',
    'ApiKey' => 'apikey',
    'Password' => 'password',
);

$shopify = PHPShopify\ShopifySDK::config($config);

$productResource = $shopify->Product();
$products = $productResource->get(['limit' => 250]);
$nextPageProducts = $productResource->getNextPageParams();
$nextPageProductsArray = [];

while ($nextPageProducts) {
    $nextPageProductsArray = $productResource->get($productResource->getNextPageParams());
    $products = array_merge($products, $nextPageProductsArray);
    $nextPageProducts = $productResource->getNextPageParams();
}

echo 'Total Products: ' . count($products) . PHP_EOL;

Follow the link for more information on cursor-based pagination change from Shopify.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.