【问题标题】:PHP - JSON request not working when attempting to update productPHP - 尝试更新产品时 JSON 请求不起作用
【发布时间】:2014-12-10 19:20:01
【问题描述】:

我正在使用 PHP 访问 shopify API,我可以获取产品数据,但是当我尝试更新产品记录及其变体数量时它不起作用。奇怪的是,我收到一个 json 响应,详细说明了我要更新的产品的信息,它总是指定 inventory_quantity 值和 old_inventory_quantity 值是相同的,所以如果我发送一个新数量或 1 而旧数量是 0响应表明它们都是 0,告诉我没有任何更新。转储时,我编码的 json 看起来像这样:

"{
"product":
{
"id":127417494,"variants":
{
"id":288267528,
"inventory_quantity":2
}
}
}"

我得到的回应是:

" ["created_at"]=> string(25) "2013-03-18T16:47:45-07:00" ["handle"]=> string(23) "peacock-herl-small-pack" ["id"]=> int(127417488) ["product_type"]=> string(15) "Tying Materials" ["published_at"]=> string(25) "2013-03-18T16:47:00-07:00" ["published_scope"]=> string(6) "global" ["template_suffix"]=> string(0) "" ["title"]=> string(25) "Peacock Herl - Small Pack" ["updated_at"]=> string(25) "2014-12-09T18:15:07-08:00" ["vendor"]=> string(19) "Pacific Fly Fishers" ["tags"]=> string(8) "Feathers" ["variants"]=> array(1) { [0]=> array(22) { ["barcode"]=> NULL ["compare_at_price"]=> NULL ["created_at"]=> string(25) "2013-03-18T16:47:45-07:00" ["fulfillment_service"]=> string(6) "manual" ["grams"]=> int(45) ["id"]=> int(288267518) ["inventory_management"]=> string(7) "shopify" ["inventory_policy"]=> string(4) "deny" ["option1"]=> string(5) "Small" ["option2"]=> NULL ["option3"]=> NULL ["position"]=> int(1) ["price"]=> string(4) "3.50" ["product_id"]=> int(127417488) ["requires_shipping"]=> bool(true) ["sku"]=> string(12) "762820007750" ["taxable"]=> bool(true) ["title"]=> string(5) "Small" ["updated_at"]=> string(25) "2014-12-08T15:23:28-08:00" ["inventory_quantity"]=> int(5) ["old_inventory_quantity"]=> int(5) ["image_id"]=> NULL } } ["options"]=> array(1) { [0]=> array(4) { ["id"]=> int(152687222) ["name"]=> string(5) "Title" ["position"]=> int(1) ["product_id"]=> int(127417488) } } ["images"]=> array(1) { [0]=> array(7) { ["created_at"]=> string(25) "2014-12-08T15:23:23-08:00" ["id"]=> int(1077719361) ["position"]=> int(1) ["product_id"]=> int(127417488) ["updated_at"]=> string(25) "2014-12-08T15:23:25-08:00" ["src"]=> string(93) "https://cdn.shopify.com/s/files/1/0211/7110/products/peacock-herl-small-pack.jpg?v=1418081005"; ["variant_ids"]=> array(0) { } } } ["image"]=> array(7) { ["created_at"]=> string(25) "2014-12-08T15:23:23-08:00" ["id"]=> int(1077719361) ["position"]=> int(1) ["product_id"]=> int(127417488) ["updated_at"]=> string(25) "2014-12-08T15:23:25-08:00" ["src"]=> string(93) "https://cdn.shopify.com/s/files/1/0211/7110/products/peacock-herl-small-pack.jpg?v=1418081005"; ["variant_ids"]=> array(0) { } } } } array(1) { ["product"]=> array(16) { ["body_html"]=> string(169) "

Beautiful peacock sword that is often used for tails on patterns like Zug Bugs and Spruce Fly streamers. Also used in many atlantic salmon and steelhead patterns.

我一遍又一遍地检查了 id,它似乎到达了正确的端点,但无论出于何种原因,它都不会接受新的数量值,我不知道我是否缺少必需的参数或什么但我很漂亮在这方面几乎是死胡同。

如果有人好奇,我的 cUrl 请求看起来像这样

if ($data != null) {
    curl_setopt_array($ch, array(        
    CURLOPT_CUSTOMREQUEST  => 'PUT', //sets method if specified
    CURLOPT_RETURNTRANSFER => true, //Causes curl_exec() to return the response
    CURLOPT_HEADER         => false, //Suppress headers from returning in curl_exec()
    CURLOPT_HEADERFUNCTION => array($this, 'header_callback'), //call header_callback upon receiving headers
    CURLOPT_HTTPHEADER => array('Content-Type: application/json','Content-Length: ' . strlen($data)),
    CURLOPT_POSTFIELDS => $data,        
    ));

  $this->response = curl_exec($ch);
  // close curl
  curl_close($ch);
  // return json data
  return json_decode($this->response, true);
}

【问题讨论】:

  • 快速浏览变体 id 288267528 不是返回字符串中的变体 id。也许如果您使用正确的变体编号,它会起作用吗?您的 Json 中的变体数组周围也不应该有括号吗?
  • 您必须将变体更新为产品,而不是产品,请查看手册docs.shopify.com/api/product_variant

标签: php json shopify


【解决方案1】:

如果您尝试单独更新库存,则有效负载数组应如下所示

$inventory = array (            
    "variant"=>array (
        "id"=>{{'your_variant_id'}},
        "inventory_quantity"=>{{'your_inventory_quantity'}}
    )               
);

您的网址应如下所示

$url = "variants/".{{'your_variant_id'}}.".json";

使用PUT方法和$inventory数组作为参数调用上面的url。

我有一个工作演示,使用上述方法管理库存。

您可以在http://sarkutils.in/swpaneldemo:password 看到该演示

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-04
    相关资源
    最近更新 更多