【问题标题】:Get all order in shoplify API在shopify API中获取所有订单
【发布时间】:2019-12-14 18:46:06
【问题描述】:

我正在尝试从 Shopify 中的特定日期范围内获取所有订单。问题是我只能得到 250 个订单。我正在使用以下代码。

<div id="main-content1">
    <div class="container-fluid">
        <?php
            function nextorders($lastorderno,$orderfm,$orderto,$orders) {
                $orders_obj_url = 'XXXXXXXXXXXXXXXXXXX.myshopify.com/admin/api/2019-10/orders.json&since_id='.$lastorderno.'&status=any&created_at_min='.$orderfm.'T00:00:00&created_at_max='.$orderto.'T23:59:59&fields=created_at,id,order_status_url,total_price_set,number,note,note_attributes';
                $orders_content = @file_get_contents( $orders_obj_url );
                $orders_json = json_decode( $orders_content, true );
                $orders_new = $orders_json['orders'];
                $orders = $orders_json['orders'];
                if ($remaningprder > 0) {
                    $lastorderno = $orders['0']["number"];                    
                    nextorders($lastorderno,$orderfm,$orderto,$orders_new);
                }
                return $orders_new;
            }
            if ($_POST) {
                $orderfm = date("Y-m-d", strtotime($_POST['start']));
                $orderto = date("Y-m-d", strtotime($_POST['end']));
                $orders_obj_url = 'XXXXXXXXXXXXXXXXXXX.myshopify.com/admin/api/2019-10/orders.json?limit=250&fields=created_at,id,order_status_url,total_price_set,number,note,note_attributes&status=any&created_at_min='.$orderfm.'T00:00:00&created_at_max='.$orderto.'T23:59:59';
                $orders_content = @file_get_contents( $orders_obj_url );
                $orders_json = json_decode( $orders_content, true );
                $orders = $orders_json['orders'];
                if (count($orders) > 249) {
                    $remaningprder = $total_order - count($orders);
                    $lastorderno = $orders['0']["number"];
                    $orders = nextorders($lastorderno,$orderfm,$orderto,$orders);
                }
            }
        ?> 

        <div class="row clearfix" style="margin-top:20px;margin-bottom:20px;">
            <div class="offset-lg-3 offset-md-3 col-lg-6 col-md-6">
            </div>
            <div class="offset-lg-3 offset-md-3 col-lg-6 col-md-6">
                <div>
                    <form method="POST" action="">
                        <label>Date Range</label>                                    
                        <div class="input-daterange input-group" data-provide="datepicker">
                            <input type="text" class="input-sm form-control" value="<?=$startset;?>" name="start" autocomplete="off">
                            <span class="input-group-addon range-to">to</span>
                            <input type="text" class="input-sm form-control" value="<?=$endset; ?>" name="end" autocomplete="off">
                            <div class="input-group-append">
                                <button class="btn btn-outline-secondary" type="submit">Search</button>
                            </div>
                        </div>
                    </form>
                </div>                    
            </div>
        </div>          

        <div class="row clearfix">
            <div class="col-lg-12">
                <div class="card">
                    <div class="header">
                        <h2>Last order</h2>
                        <ul class="header-dropdown dropdown">
                            <li><a href="javascript:void(0);" class="full-screen"><i class="icon-frame"></i></a></li>
                        </ul>
                    </div>
                    <div class="body">
                        <div class="table-responsive hide-table">
                            <?php if (count($orders) > 0) { ?>
                            <table class="table table-striped table-hover dataTable js-exportable">
                                <thead>
                                    <tr>
                                        <th>#</th>
                                        <th>Order No</th>
                                        <th>Amount</th>
                                        <th>Token</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php 
                                      $i=1; 
                                      foreach ($orders as $order) { 
                                    ?>
                                    <tr>
                                      <th scope="row" data-title="Date"><?=$i++;?></th>
                                      <td data-title="Order No"><a href="<?=$order["order_status_url"];?>" target="_blank"><?=$order["number"];?></a></td>
                                      <td data-title="Amount"><?=$order["total_price_set"]["shop_money"]["amount"];?></td>
                                      <td data-title="Token"></td>
                                    </tr>
                                    <?php } ?>
                                </tbody>                                
                                </table>
                            <?php } ?>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

然后使用 $orders 变量创建输出表。即使我增加订单限制,它也只是播下250个订单。

谁能帮我解决这个问题

谢谢

【问题讨论】:

    标签: php shopify shopify-api-node


    【解决方案1】:

    这是我知道的一个老问题,但也许有人可以找到这个答案的用途。

    有一种方法可以通过使用 fetch url 和 cURL 中的since_id 选项来获取所有订单。通过这种方式,您可以获得 250 个(或您选择的低于 250 的数量,因为这是您一次可以获取的最大数量)订单,从订单 ID 0 开始。这将从第一个订单开始。从这里,您循环并从(使用since_id)上一次调用中的最后一个订单获取订单。当返回的订单数低于您在每次调用中获取的订单数时,您将退出。

    来自 Symfony (5) 应用程序的示例:

    private function getShopifyOrders(): array
        {
            $this->store_name = $this->params->get('app.shopify_store_name');
            $this->api_key = $this->params->get('app.shopify_api_key');
            $this->api_pass = $this->params->get('app.shopify_password');
            $this->api_url = 'https://' . $this->api_key . ':' . $this->api_pass . '@' . $this->store_name;
            $last = 0;
            $allOrders = [];
            while (true) {
                $orders_url = $this->api_url . '/admin/orders.json?limit=250&status=any&since_id=' . $last;
                error_log($orders_url); // Show each call in the log for reference
                $ch = curl_init($orders_url);
                curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                $response = curl_exec($ch);
                $orders = json_decode($response);
    
                foreach ( $orders->orders as $order ) {
                    $allOrders[] = $order;
                    $last = $order->id;
                }
    
                if ( count( $orders->orders ) < 250 ) {
                    break;
                }
            }
    
            return $allOrders;
        }
    

    【讨论】:

      【解决方案2】:

      您需要使用 Shopify 使用的基于 page_info 的分页。本质上,您需要查看返回的标题,并在适用时从那里调用一个新 URL(如果有更多可用结果)。

      不幸的是,这意味着您不能使用get_file_contents(),因为它不允许您查看标题。查看 curl 或其他库以发出正确的 HTTP 请求。

      相关的 Shopify 文档在这里:https://help.shopify.com/en/api/guides/paginated-rest-results

      【讨论】:

        猜你喜欢
        • 2016-04-02
        • 2012-09-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多