0

我正在尝试从 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个订单。

谁能帮我这个

谢谢

4

2 回答 2

1

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

有一种方法可以通过使用since_idfetch url 和 cURL 中的选项来获取所有订单。通过这种方式,您可以获得 250 个(或您选择的低于 250 的数量,因为这是您可以一次性获取的最大数量)订单,从 order 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;
    }
于 2021-10-15T07:52:42.153 回答
0

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

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

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

于 2019-12-14T21:18:18.833 回答