skip to Main Content

I wrote this snippet in order to fetch all the products of a store,

using PHP Shopify SDK :

$productCount = $shopify->Product->count();
$limit = 250;
$totalpage = ceil($productCount/$limit);

for($i=1; $i<=$totalpage; $i++){

  $params = array(
    'limit' => '250',
    'page' => $i
  ); 

  $products = $shopify->Product->get($params);
}

But I am getting only the first 50 products.
Do you have any suggestions on how to get them all?

Thanks in advance!

2

Answers


  1. You have to pass page_info instead Of page in params and you will get page info from responses like :

    $page_info="";
    for($i=1; $i<=$totalpage; $i++){
        $productResource = $shopify->Product();
        $params = array(
                    'limit' => '250',
                    'page_info' => $page_info
                  );
                  
        $product = $productResource->get($params);
        $nextPageProducts = $productResource->getNextPageParams();
        $page_info = $nextPageProducts['page_info'];
    }
    

    for the first page, you have to pass in empty page_info.

    Login or Signup to reply.
  2. I have created a function for our project that can fetch all resource items of a store. See example:

    $this->existingProducts = $this->getAllResourceItems($this->shopify->Product());
    
    
    private function getAllResourceItems(PHPShopifyShopifyResource $resource): array
    {
        $result = [];
    
        $page_info = '';
        while ($page_info !== NULL) {
            $params = [
                'limit' => '250', // max 250
                'page_info' => $page_info
            ];
    
            $items  = $resource->get($params);
            $result = array_merge($result, $items);
    
            $nextPageParams = $resource->getNextPageParams();
            $page_info      = $nextPageParams['page_info'];
        }
    
        return $result;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search