【问题标题】:Shopify API getting order by name or order_numberShopify API 按名称或 order_number 获取订单
【发布时间】:2015-04-29 16:47:51
【问题描述】:

我正在使用 CakePHP 的插件来进行调用以获得某些订单。我可以调用具有特定字段的所有订单,但我想知道如何调用才能获取具有特定名称或 order_number 的订单?这是调用 Shopify 的来源。它已经过身份验证和一切:

public function call($method, $path, $params=array())
    {
        if (!$this->isAuthorized())
            return;
        $password = $this->is_private_app ? $this->secret : md5($this->secret.$this->ShopifyAuth->token);
        $baseurl = "https://{$this->api_key}:$password@{$this->ShopifyAuth->shop_domain}/";

        $url = $baseurl.ltrim($path, '/');
        $query = in_array($method, array('GET','DELETE')) ? $params : array();
        $payload = in_array($method, array('POST','PUT')) ? stripslashes(json_encode($params)) : array();
    $request_headers = in_array($method, array('POST','PUT')) ? array("Content-Type: application/json; charset=utf-8", 'Expect:') : array();
    $request_headers[] = 'X-Shopify-Access-Token: ' . $this->ShopifyAuth->token;
        list($response_body, $response_headers) = $this->Curl->HttpRequest($method, $url, $query, $payload, $request_headers);
    $this->last_response_headers = $response_headers;
    $response = json_decode($response_body, true);

        if (isset($response['errors']) or ($this->last_response_headers['http_status_code'] >= 400))
            throw new ShopifyApiException($method, $path, $params, $this->last_response_headers, $response);

        return (is_array($response) and (count($response) > 0)) ? array_shift($response) : $response;
    }

    private function shopApiCallLimitParam($index)
    {
        if ($this->last_response_headers == null)
        {
            return 0;
        }
        $params = explode('/', $this->last_response_headers['http_x_shopify_shop_api_call_limit']);
        return (int) $params[$index];
    }

...以及调用GET 的代码:

// I only want the id and title of the collections
            $fields = "fields=name,id,status,financial_status,fulfillment_status,billing_address,customer";
            // get list of collections
            $custom_collections = $this->ShopifyAPI->call('GET', "/admin/orders.json", $fields);
            $this->set('collections', $custom_collections);

我想我错过了可以设置调用条件以获得某些订单的地方。我已经阅读了 API 文档,但似乎无法得到答案。

我尝试将?name=%231001 放在.json 之后的url 上以尝试获取订单#1001,但它会返回一个空数组。

然后我尝试了 ?order_number=1001,但它给我带来的每个订单也是 1001 D:这真的很混乱,有人能帮帮我吗?

提前致谢。

【问题讨论】:

    标签: api cakephp shopify restful-url


    【解决方案1】:

    好吧,我发现您实际上可以使用名称或订单号来获取订单。由于某种原因,它的另一个属性未在文档中列出。但是在 URL 中,如果您使用另一种语言,您只需在 GET 中添加 admin/order.json?name=%2310001&status=any 这是为了获取订单 10001,因此只需在 %23 之后添加 order_number。我在 Shopify 大学的一个论坛上看到了这个,我只是在我的代码中实现了这个错误。如果您像我一样使用 CakePhp shopify 插件,我所做的只是在 $field 上添加 ?name=%23".number ."&status=any";

    我把代码放在这里:

        $this->layout = 'main';
    
    $order_number = "18253";
    
    $fields = "name=%23". $order_number ."&status=any";
    
    $order = $this->ShopifyAPI->call('GET', "/admin/orders.json", $fields);
       if (!empty($order)) {
         $this->set('order', $order);
    
         } else {
           $this->Session->setFlash('<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button> No existe el numero de orden ingresado.','default',array('class' => 'alert alert-danger alert-dismissible', 'type' => 'alert'));
         }
    

    希望这可以帮助某人:P

    【讨论】:

    • 如果您必须手动转义查询字符串中的 #,这意味着 api 调用将传入原始输入。真正的修复应该在您的 HttpRequest 类中,该类应该是传递给它的 url 编码查询参数。 php.net/manual/en/function.http-build-query.php
    • 实际上我对 # 进行编码,因为它是我们商店用于订单名称的前缀。其他人的订单号可能有其他前缀。并且在 http 上编码 # 会更有效,因为不是每次我都会在字段上发送名称以供 ShopfyAPI 进行调用。
    • 我理解 name=#000000 是格式。我的意思是,如果您必须手动转义一个值(使用 %23 而不是 #),这意味着查询字符串中具有保留字符(空格、#?&)的任何值都不会被编码正确。在 ->call() 方法中修复此编码问题意味着您不必担心“$fields”数据中的特殊字符。 http_build_query() 可以为您处理这些细节。
    • 在 admin/orders.json?name=1234 上执行 GET 请求时,您不需要提供 #、转义或其他。 Shopify 订单搜索 api 会忽略它。所以 GET admin/orders.json?name=#1234 & admin/ordes.json?name=1234 返回完全相同的结果。
    猜你喜欢
    • 2012-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-03
    相关资源
    最近更新 更多