3
votes

I have recently started developing and would like some assistance if possible regrading the following issue i am facing. Firstly my aim [project scope] is to do the following:

 1. Connect to the shopify API via an external web app [Done this Successfully]
 2. Fetch a list of all products in that specific store [Struggling with this]
 3. Insert (or update there after) in to a MySQL database [Can do this successfully]

Currently i can fetch as per shopify's API limit of 250 products per call and echo this out. I have done some research and found that i need to paginate the request on the overall count of products [5000 products / 250 products per page = 20 pages] in the store. From that i can loop through each page and echo out all the products. How ever when i run this loop i get the first 250 products echoed out over and over for the overall count of products in the store (apologies if the wording seems a bit vague as i am completely new to both API's & PHP).

My question is: How do i loop through and echo out all the products and all the pages and echo out on to the page that i have created "list-products.php"?

My current code is as follows:

get-products.php // connects to shopify api.

<?php
session_start();
$username = $_SESSION['username'];
if(empty($_SESSION['username']))
{
    header("Location: loginpage.php");
    die("Redirecting to login.php");
}
$api_url = 'https://apikey:[email protected]';
$products_obj_url = $api_url . '/admin/products.json?limit=250&page='.($i+1);
$products_content = @file_get_contents( $products_obj_url );
$products_json = json_decode( $products_content, true );
$products = $products_json['products'];
?>

get-product-count.php // Counts the amount of products currently listed in the store.

<?php
 session_start();
 $username = $_SESSION['username'];
if(empty($_SESSION['username']))
{
    header("Location: loginpage.php");
    die("Redirecting to login.php");
}
 $api_url = 'https://apikey:[email protected]';
 $count_obj_url = $api_url . '/admin/products/count.json';
 $count_content = @file_get_contents( $count_obj_url );
 $count_json = json_decode( $count_content, true );
 $count = $count_json['count'];
?>

list-products.php // echos out a list of all the products in the store

<!DOCTYPE html>

 <?php
  include('../get-products.php'); 
  include('../get-product-count.php'); 
?>

<html>
<head>
  <title>List Products</title>
  <link rel="stylesheet"href="https://cdnjs.cloudflare.com/ajax/libs/uikit/3.0.0-beta.40/css/uikit.min.css" />
  <script src="https://cdnjs.cloudflare.com/ajax/libs/uikit/3.0.0-beta.40/js/uikit.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/uikit/3.0.0-beta.40/js/uikit-icons.min.js"></script>
</head>

<body>
 <?php include('../header.php'); ?>

  <div class="uk-alert-success uk-margin-medium-left uk-margin-medium-right" uk-alert>
  <a class="uk-alert-close" uk-close></a>

  <p>All products.</p>
  <table class="uk-table uk-table-small uk-table-striped uk-table-divider uk-margin-small uk-margin-small-left uk-text-small" uk-grid="">

<tr>
    <td>Product ID</td>
    <td>Product Title</td>
    <td>Product SKU</td>
    <td>Variant Title</td>
    <td>Price</td>
    <td>Inventory</td>
</tr>
<?php

// Loop through each page and pull all products. Reason being, Shopify API call limit is 250 calls at once.
// This currently display the first 250 products on the first page for the full product count.

$i = 0;
$pages = ceil($count/250); // Count products / 250 from get-products.php = to amount of pages rounded up.

    for($i = 0; $i < $pages; $i++){
        foreach($products as $product){   
            echo "<tr>
            <td>". $product['variants'][0]['product_id'] . "</td>
            <td>". $product['title']. "</td>
            <td>". $product['variants'][0]['title'] . "</td>
            <td>". $product['variants'][0]['sku'] . "</td>
            <td>". $product['variants'][0]['price'] . "</td>
            <td>". $product['variants'][0]['old_inventory_quantity'] . "</td>
            </tr>";
        } 
    }
?>   
</table>

<?php include('../footer.php'); ?>

</body>
</html>

In closing: Any help on this would be greatly appreciated.

1
Thanks for sharing code, it really means lot.Yogesh Trivedi

1 Answers

2
votes

You are very close.

You need to keep fetching the next 250 products inside of your loop:

for($i = 0; $i < $pages; $i++){

    // not sure if this exact example will work 100% for you but you need to re-execute the code from this file on every iteration
    // By placing this here, get-products.php will become aware of the incrementing $i and fetch the next page of products
    require( '../get-products.php' );

    foreach($products as $product){   
        echo "<tr>
        <td>". $product['variants'][0]['product_id'] . "</td>
        <td>". $product['title']. "</td>
        <td>". $product['variants'][0]['title'] . "</td>
        <td>". $product['variants'][0]['sku'] . "</td>
        <td>". $product['variants'][0]['price'] . "</td>
        <td>". $product['variants'][0]['old_inventory_quantity'] . "</td>
        </tr>";
    } 
}