phpclassic / php-shopify

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

How to show all products with pagination? #241

Open Frank9716 opened 2 years ago

Frank9716 commented 2 years ago

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:

$params = array(
     'limit' => 250,
);
$orders = $shopify->Product->get($params);

Thanks for the support

smoopins commented 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;
    }
tselikin commented 2 years ago
  1. Create shopify resource
  2. 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;
    }
kelenakamura commented 5 months ago
  1. Create shopify resource
  2. 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);

kelenakamura commented 5 months ago

Ah sorry i needed to update the version.