Open Frank9716 opened 2 years ago
Late, sure, but in case anyone else requires getting past the pagination limits here is the recursive function that I wrote to do exactly that (in this case, for Product objects).
You would just need to modify the top-most line to use your instance of the SDK, whereas I do so in the constructor of the class this method is inside of...
public function getProductIds($vendor_name = null): array
{
$productResource = $this->shopify->sdk->Product();
$idList = [];
do {
// Determine which type of call to make
if ( $productResource->getNextPageParams() ) {
$whatToDo = $productResource->getNextPageParams();
} else {
$whatToDo = ['limit'=>250, 'fields'=>'id'];
// If one vendor was passed-in, add it to the parameter array
if ( $vendor_name ) {
$whatToDo['vendor'] = $vendor_name;
}
}
// Make the call
$products = $productResource->get($whatToDo);
// Append the array
foreach ( $products as $prodId ) {
$idList[] = $prodId['id'];
}
} while ( $productResource->getNextPageParams() );
return $idList;
}
Example:
$this->existingCollections = $this->getAllResourceItems($this->shopify->CustomCollection());
$this->existingProducts = $this->getAllResourceItems($this->shopify->Product());
private function getAllResourceItems(PHPShopify\ShopifyResource $resource): array
{
$result = [];
$params = ['limit' => '250']; // max 250
do {
$items = $resource->get($params);
$result = array_merge($result, $items);
$params = $resource->getNextPageParams();
} while ($params['page_info']);
return $result;
}
- Create shopify resource
- Pass it to custom function getAllResourceItems.
Example:
$this->existingCollections = $this->getAllResourceItems($this->shopify->CustomCollection()); $this->existingProducts = $this->getAllResourceItems($this->shopify->Product());
private function getAllResourceItems(PHPShopify\ShopifyResource $resource): array { $result = []; $params = ['limit' => '250']; // max 250 do { $items = $resource->get($params); $result = array_merge($result, $items); $params = $resource->getNextPageParams(); } while ($params['page_info']); return $result; }
@tselikin is it possible to do this for a child resource? i'm trying to get all addresses from a customer, but get an error that:
No action named getNextPageParams is defined for CustomerAddress
$this->getAllResourceItems($shopifyApi->Customer($customerID)->Address);
Ah sorry i needed to update the version.
Hi, I'm trying to show all the products of my shop, the limit is 250 products and in the store there are 800+ products, via API there's the pagination method to show all the products when the items number exceed the limit of 250. There is some parameter that i can set for this type of output? My current configuration is like this:
Thanks for the support