Need help with syntax for adding metafield to variant using shopify-api-node lib in javascript

Solved
Shopify Partner
14 1 0

Hi,

I am using same code to get product list and to add metafields to the shop, but cant seem to get the correct incantation to add to product variants...

my code:

var record = {
    key: key,
    value: 1,
    value_type: "integer",
    namespace: "sale_class",
    owner_resource: 'variants',
    owner_id: variantId
 }
 
await shopify.metafield
.create(record)
.then(metafield => {
logIt(" metafield.create() response:\n" + stringify(metafield));
 }, err => {
logIt(" metafield.create() ERROR:\n" + stringify(err));
 });

The response:

{ key: 'Wholesale',
value: 1,
value_type: 'integer',
namespace: 'sale_class',
owner_resource: 'variants',
owner_id: 16207071608866 }
metafield.create() ERROR:
{
name: "HTTPError",
host: undefined,
hostname: "case-size-selector-development-store.myshopify.com",
method: "POST",
path: "/admin/metafields.json",
protocol: "https:",
url: undefined,
statusCode: 400,
statusMessage: "Bad Request",
headers: {
server: "nginx",
date: "Wed, 10 Apr 2019 06:46:05 GMT",
"content-type": "application/json; charset=utf-8",
"transfer-encoding": "chunked",
connection: "close",
"x-sorting-hat-podid": "33",
"x-sorting-hat-shopid": "4983586850",
"referrer-policy": "origin-when-cross-origin",
"x-frame-options": "DENY",
"x-shopid": "4983586850",
"x-shardid": "33",
"x-stats-userid": "26989658146",
"x-stats-apiclientid": "2785525",
"x-stats-apipermissionid": "86388211746",
http_x_shopify_shop_api_call_limit: "3/40",
"x-shopify-shop-api-call-limit": "3/40",
"strict-transport-security": "max-age=7889238",
"x-request-id": "e5b798a1-e793-4f14-a6d9-0ef8be26bc58",
"x-shopify-stage": "production",
"content-security-policy": "default-src 'self' data: blob: 'unsafe-inline' 'unsafe-eval' https://* shopify-pos://*; block-all-mixed-content; child-src 'self' https://* shopify-pos://*; connect-src 'self' wss://* https://*; frame-ancestors 'none'; img-src 'self' data: blob: https:; script-src https://cdn.shopify.com https://checkout.shopifycs.com https://js-agent.newrelic.com https://bam.nr-data.net https://dme0ih8comzn4.cloudfront.net https://api.stripe.com https://mpsnare.iesnare.com https://appcenter.intuit.com https://www.paypal.com https://maps.googleapis.com https://www.google-analytics.com https://v.shopify.com https://widget.intercom.io https://js.intercomcdn.com 'self' 'unsafe-inline' 'unsafe-eval'; upgrade-insecure-requests; report-uri /csp-report?source%5Baction%5D=create&source%5Bapp%5D=Shopify&source%5Bcontroller%5D=admin%2Fmetafields&source%5Bsection%5D=admin_api&source%5Buuid%5D=e5b798a1-e793-4f14-a6d9-0ef8be26bc58",
"x-content-type-options": "nosniff",
"x-download-options": "noopen",
"x-permitted-cross-domain-policies": "none",
"x-xss-protection": "1; mode=block; report=/xss-report?source%5Baction%5D=create&source%5Bapp%5D=Shopify&source%5Bcontroller%5D=admin%2Fmetafields&source%5Bsection%5D=admin_api&source%5Buuid%5D=e5b798a1-e793-4f14-a6d9-0ef8be26bc58",
"x-dc": "chi2,gcp-us-central1"
}

0 Likes
Highlighted
Shopify Staff
Shopify Staff
297 32 63

Hey Mike,

 

Your metafield needs to follow the syntax shown in the docs. Specifically, you're missing the outer "metafield" key.

 

Edit: I overlooked your use of the shopify-api-node and incorrectly linked the normal REST docs. The correct format is found in the docs here

shopify.metafield.create({
  key: 'warehouse',
  value: 25,
  value_type: 'integer',
  namespace: 'inventory',
  owner_resource: 'product',
  owner_id: 632910392
}).then(
  metafield => console.log(metafield),
  err => console.error(err)
);

 

0 Likes
Highlighted
Shopify Partner
14 1 0

Hi Zameer,

 

Sorry for being such a noob, but I have been loosing many cpu cycles trying to get this syntax correct.

 

The REST docs seem to require both a products id and a variants id, which has me very unsure of correct syntax:

Product Variant/admin/products/#{id}/variants/#{id}/metafields.json

So, to add a metafield to a variant, is this correct?

Is the owner_resource parameter set correctly to "products", or should it be "variants"?

    {
        "metafield": {
            "namespace": theNameSpace,
            "key": theKey,
            "value": theValue,
            "value_type": "integer", 
            "owner_resource": 'products',
            "owner_id": variantId,
        }
    }

Thanks for your help!!

0 Likes
Highlighted

Success.

Shopify Partner
14 1 0

Zameer,

 

The information you gave me was completely wrong, and cost me days to figure out the correct syntax!

The following syntax is correct, and allows me to add metafields to variants:

                               var mfRecord = {
                                        "key": key,
                                        "value": 1,
                                        "value_type": "integer",
                                        "namespace": "case_size_selector",
                                        "owner_resource": "variant",
                                        "owner_id": variantId,
                                    }

I found the answer by reviewing the documentation here.

0 Likes