cakephp Shopify API通过名称或order_number获取订单

mccptt67  于 10个月前  发布在  PHP
关注(0)|答案(2)|浏览(84)

我使用了一个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:这真的很令人困惑,有人能帮我吗?

ftf50wuq

ftf50wuq1#

我发现你实际上可以使用name或order_number来获取订单。这是另一个由于某种原因没有在文档中列出的属性。但是在URL中,如果你使用另一种语言,你所要在GET中添加的就是admin/order.json?name=%2310001&status=any这是为了获得订单10001,所以只需在%23之后添加order_number。我在Shopify大学的一个论坛上看到了这个,我只是在我的代码中实现了这个错误。如果你像我一样使用CakePhp shopify插件,我所做的就是在$字段上添加?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

wnrlj8wa

wnrlj8wa2#

您不需要转义#,只需删除#并进行API调用即可
像这样https://myshopify.com/admin/api/2022-01/orders.json?name=12345678&status=any
对于订单:#12345678,它将返回完全相同的响应

相关问题