phpclassic / php-shopify

PHP SDK for Shopify API
Apache License 2.0
568 stars 211 forks source link

Can't make paginated requests to the REST Admin API #284

Closed brianruff closed 1 year ago

brianruff commented 1 year ago

I'm unable to do pagination for shops that have more than 250 customers (max number per request). I'm told we have to do it this way:

https://shopify.dev/api/usage/pagination-rest

zamliyconsulting commented 1 year ago

Use this order pagination example for customers

function getOrders() { global $config; $shopify = new PHPShopify\ShopifySDK($config);

$filters = [
    "status" => "any",
    "limit" => 250, 
    "updated_at_min" => date('Y-m-dTH:i:s-P', strtotime('-48 hours', time()))
];

$orderResource = $shopify->Order();
$orders = $orderResource->get($filters);
$next_page = $orderResource->getNextPageParams();
while ($next_page) {
    $new_orders = $orderResource->get($orderResource->getNextPageParams());
    $orders = array_merge($orders, $new_orders);
    $next_page = $orderResource->getNextPageParams();
}
return $orders;

}

brianruff commented 1 year ago

Thank you that worked. Here's the modified version of your example I used:

$filters = [
    "published_status" => "published", 
    "fields" => "id,first_name,last_name",
    "limit" => 250, 
    "status" => "active" 
];

$customerResource = $shopify->Customer();
$customers = $customerResource->get($filters);
$nextPage = $customerResource->getNextPageParams();

while ($nextPage) {
    $newCustomers = $customerResource->get($customerResource->getNextPageParams());
    $customers = array_merge($customers, $newCustomers);
    $nextPage = $customerResource->getNextPageParams();
}