0
votes

from what I've read, we should be able to update product metafields in bulk using a similar type JSON as below:

$updateInfo = array (
        "metafields" => [
              [
                  "namespace" => "product_info",
                  "key" => "available",
                  "value" => $available,
                  "value_type" => "string",
                  "description" => "Planned release date"
              ],
              [
                  "namespace" => "product_info",
                  "key" => "length",
                  "value" => sprintf("%.2f", $indLength),
                  "value_type" => "string",
                  "description" => "Item length"
              ],
                        ....]);

I'm using PHP Shopify SDK for anyone wondering...now if a field doesn't have any metafields....it works ok, but as soon as the API would need to update, it throws an error about a unique key...

Is there a different way I should go about this? I call the API on a Product endpoint, not a Metafield one. Like this:

$rez = $shopify->Product($product['id'])->put($updateInfo);

Thanks for the help.

1

1 Answers

1
votes

Once metafields are created you have to pass metafield IDs to update them.

$updateInfo = array (
  "metafields" => [
    [
      "id" => $availableMetafieldId,
      "namespace" => "product_info",
      "key" => "available",
      "value" => $available,
      "value_type" => "string",
      "description" => "Planned release date"
    ],
    [
      "id" => $lengthMetafieldId,
      "namespace" => "product_info",
      "key" => "length",
      "value" => sprintf("%.2f", $indLength),
      "value_type" => "string",
      "description" => "Item length"
    ],
]);

Replace $availableMetafieldId and $lengthMetafieldId with your values.