FROM CACHE - en_header

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 15 (15)
HunkyBill
Shopify Expert
4769 54 555

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
4769 54 555

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
4769 54 555

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
4769 54 555

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
4769 54 555

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
rpw
Shopify Partner
74 0 10

True. Will email the metfield developers.

Cheers,
Rob

sinejoe
Shopify Expert
63 0 12

Rob,

Simply configuring metafields in our app doesn't save or create anything in your Shopify store data. To Shopify a metafield is completely standalone and one-off and you can have a different metafield for every resource with no requirement that namespaces or keys match each other across resources of the same type. The app tries to make it easier for you to configure/setup metafields for all your products or all your blogs, etc. which you can then populate and use for each resource. But until you actually save/put a value into a configured metafield for any particular resource, it doesn't exist there. the metafield is not created. In fact as Bill states, it couldn't be created until there is a value because metafields cannot be blank. By extension, if you blank a value for any metafield, the metafield itself (only that one for that single resource) actually gets deleted because we cannot save a blank value there.

Hope this helps.

Regards/Joe

rpw
Shopify Partner
74 0 10

Hi Joe,

Thanks for the reply 🙂 I've had a poke around the metafields admin with a DOM inspector, and see that hidden inputs which contain metafield ID's. Those which have content have an ID, those that don't have dontent, dont have an ID.

So am I correct in thinking that would explain why when I'm Posting, or putting I'm able to view my updates, and see my updates in my app - but cannot see any changes from within metafields admin, as they won't have been created with the metafield app?

[The thing I'm writing is not a replacement for metafields - it's complimentary... metafields is tightly wound in... we couldn't run our store without it - it allowed me to stay with Shopify 🙂 ]

 

Cheers,
Rob

sinejoe
Shopify Expert
63 0 12

Rob, 

Not sure why/how you think you are putting data into metafields from the front-end of your store but there is no way currently to put data into metafields without going thru the API proper or any app that does that for you. Maybe you are just updating some json data that seems to persist but it is not being posted to Shopify.

**EDIT** On the other hand, if you are (inadvisably) posting data to your store via the Shopify API with oauth or otherwise, you may in fact be saving metafield data, but it won't show up in our app until that product gets sync'd again. Shopify provides no way of notifying or pushing metafield updates to external apps so we have to go get updates whenever it makes sense. If after you save your metafield value, you open the product you are working on and click the Apps menu and then "Metafields2 App products" the app will do a quick sync for just that resource and pull in the metafield data you put there externally.

I do want to stress how insecure using API credentials in client-side code is and really opens your store's data up to very vulnerable exposure.

/Joe

 

rpw
Shopify Partner
74 0 10

Hi Joe,

I'm going through the API - the items are being updated, and are visible within my product pages... I can add, delete, update etc. I'm running the app off a subdomain to prevent any cross domain issues.

It's just a single page javascript app.. was going to use Angular, but in the end did it with jquery. Simple login form is used to set some javascript var's containing the log in credentials / api key etc [these are not hard wired into the page].

Data is sent / updated / retrieved / deleted  via ajax. Works quite nicely. I normally wouldn't store credentials as javascript var's - however, as everything is being sent over https I'm not too worried.

Rob

rpw
Shopify Partner
74 0 10

Hi Joe,

Yep - passing keys etc around client side isn't best practice, however, in this case I'm not too worried because:

The subdomain in question is only accessible for 1 IP address
Everything is being sent over https
Credentials etc are not hard wired into the code
Although once 'logged in' the credentials are stored as javascript var's, in practice, this is comparable to having stuff stored in a cookie.

I might have a go at making it work with express.js or meteor - my backend skills aren't too hot.

Rob