【问题标题】:Fetch all products on shopify store using shopify's API & PHP, then echo to a "list-products.php" page使用 shopify API 和 PHP 获取 shopify 商店中的所有产品,然后回显到“list-products.php”页面
【发布时间】:2018-03-26 16:53:23
【问题描述】:

我最近开始开发,如果可能的话,我希望得到一些帮助来重新评估我面临的以下问题。首先我的目标[项目范围]是做以下事情:

 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]

目前,我可以根据 shopify 的 API 限制(每次调用 250 个产品)获取并回显这一点。我做了一些研究,发现我需要对商店中产品的总数量 [5000 个产品 / 每页 250 个产品 = 20 页] 的请求进行分页。从那里我可以循环浏览每个页面并回显所有产品。但是,当我运行此循环时,我会一遍又一遍地回显前 250 种产品,以反映商店中的产品总数(如果措辞似乎有点含糊,我深表歉意,因为我对 API 和 PHP 都是全新的)。

我的问题是:我如何循环并回显所有产品和所有页面并回显到我创建的“list-products.php”页面?

我目前的代码如下:

get-products.php // 连接到 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:password@store.myshopify.com';
$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 // 统计商店中当前列出的产品数量。

<?php
 session_start();
 $username = $_SESSION['username'];
if(empty($_SESSION['username']))
{
    header("Location: loginpage.php");
    die("Redirecting to login.php");
}
 $api_url = 'https://apikey:password@store.myshopify.com';
 $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 // 输出商店中所有产品的列表

<!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>

最后:对此的任何帮助将不胜感激。

【问题讨论】:

  • 感谢分享代码,真的很重要。

标签: php api shopify


【解决方案1】:

你很亲密。

您需要在循环中继续获取接下来的 250 个产品:

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>";
    } 
}

【讨论】:

  • @MokeyZeus,成功了!非常感谢您史诗般的(ly)快速响应和有效答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-28
  • 1970-01-01
  • 1970-01-01
  • 2012-08-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多