Getting product metafields via the api using javascript

rpw
Shopify Partner
74 0 10

I'm attempting to call a specific metafield of a product - I know the namespace and key of the metafield I require. I do not know the ID. However, each product only has one instance of this metafield.

$.getJSON(baseURL  + 'products/' + mainProductID + '/metafields.json?namepace=recommendations&key=productHandles', function(data){
  }).done(function(data) {
	console.log('associated products' , data.metafields[0] )

...

if this metafield is populated, I receive a json object.

if this metafield is not populated, I receive 'undefined'.  I assumed I would receive the object with empty fields... 

I know the product has the metafield - I can see it via the metafields plugin... Any advice would be greatly apprecieted.

 

Thanks,

Rob

 

Replies 16 (16)
HunkyBill
Shopify Expert
4611 54 531

Can you clarify your question. You say that if the metafield is populated, you recieve a JSON object, which we can assume is your metafield. You also say that if there is no metafield, you get back no JSON but instead "undefined". What exactly is the problem?

You're not happy getting "undefined" instead of an empty array?

 

 

Custom Shopify Apps built just for you! hunkybill@gmail.com http://www.resistorsoftware.com
rpw
Shopify Partner
74 0 10

Yep. So the metafield is present - but contains not data.

I was hoping I'd receive something like the ID of the empty metafield - that I could update against. I've been playing about - with 

 

if(typeof data.metafields[0] === 'undefined'){
   	// your code here.
   	alert('undefined')

// metafield has no content - so post some dummy data up

      $.ajax({
          url: '.............. .............. metafields.json',
          type: 'POST', 
          contentType: 'application/json',   
          data: JSON.stringify({ "metafield": { "namespace": "recommendations", "key": "productHandles", "value": "none","value_type": "string"}} ),
          dataType: 'json',
          success: function(result) {
              alert("success?");
          }
      });


 	} else {
            // metafield has content - so we can read its data and display it.

 

which seems to be working. I can then do stuff later to get the ID and update against that. Just wondering if there was a way I can do this without having to post some dummy data?

 

Cheers,

Rob

HunkyBill
Shopify Expert
4611 54 531

A metafield is a resource. If a product exists, that does not imply any metafields exist for it. So you should not expect an ID for something that does not exist. If you get undefined, you know you need to create the resource. If you get a resource, you know you can update it. Seems like a normal pattern of use to me. 

Custom Shopify Apps built just for you! hunkybill@gmail.com http://www.resistorsoftware.com
rpw
Shopify Partner
74 0 10

Agreed - however, the metafields plugin clearly shows that it exists... so I don't understand why I need to create it again - when it already exists?

 

Rob

HunkyBill
Shopify Expert
4611 54 531

Back to contradiction time. If a metafield resource exists on a product you said you had no problem getting the JSON for it. Now you're saying it exists, and you cannot get that resource... what is the correct answer :) ???

You should inspect the resource yourself. You don't need an App to play with metafields. It is basic scripting to be able to examine what Shopify has stored. That makes it dead simple to play with the Javascript and figure out what is actually going on.

Custom Shopify Apps built just for you! hunkybill@gmail.com http://www.resistorsoftware.com
rpw
Shopify Partner
74 0 10

I'm not contradicting myself - I wrote:

 

if this metafield is populated, I receive a json object.

if this metafield is not populated, I receive 'undefined'.  I assumed I would receive the object with empty fields... 

I know the product has the metafield - I can see it via the metafields plugin... Any advice would be greatly apprecieted.

 

So the metafield exists - with its own ID. I can see it within the metafields plugin - listed, with its own ID.  Therefor - it exists, it just doesn't contain any data other than namespace, key and ID.

Where there are additional values, a json object is returned. When there aren't additional values, I receive an undefined.

It just doesn't make sense to me - that if the metafield exists (as it's clearly shown in the metafields plugin) albeit without values (other than ID, namespace and key) that I need to create it...

 

And I know I don't need an app to play with metafields - however, I'm building a UI to make it easier for my staff to do some tasks. Just a simply app, running off a subdomain so as to prevent cross domain issues.

HunkyBill
Shopify Expert
4611 54 531

Bizarre that you can have a metafield resource with nothing but a namespace and a key. To successfully create one I have always had to provide a value and a value_type. How are you creating them in the first place so that you get an ID but do not provide the value_type and value? 

Custom Shopify Apps built just for you! hunkybill@gmail.com http://www.resistorsoftware.com
rpw
Shopify Partner
74 0 10

For quite some time, I've been using the metafields plugin to provide my staff with a rudimentary way to update things. From within the plugin you can specify metafields for all products...

It's very bizarre.

Also having created a 'new' metafield - the existing metafield does contain a new value, nor does the new field appear in Metafields plugin. Yet the metafield ID reports to be the same?? I can also view the new metafield via Chrome's console...

So I've no idea what's going on here?

Rob

HunkyBill
Shopify Expert
4611 54 531

Perhaps that App is not functioning as you expect. I would establish what it is doing first before working on why the Javascript is not doing as you expect. 

Custom Shopify Apps built just for you! hunkybill@gmail.com http://www.resistorsoftware.com