Aside from the metafields_global_title_tag and metafields_global_description_tag which are used for SEO purposes, metafields don’t live on the Product object itself.
Instead they live in their own Metafield object, and the location of the metafields depends on which type of resource you are trying to access.
For product metafields, you need to access them on the individual product at the following route ‘{shop_url}/admin/products/#{id}/metafields.json’.
Hopefully that helps you accomplish what you were trying to do!
Thanks for your reply @Zameer . But that won’t work. I just need to access the metafield to use them on the store front. I don’t want to implement the whole API or create a new private APP to be able to pull just some metafields.
Isn’t there a simple way like doing {shop_url}/products/{product-url}.json
So a question on API thread about pulling product metafields without using API ?
You have a few options. As you noticed, the default product URL of your storefront can be pulled as content type javascript or JSON by appending .js or .json to the URL. However, that returns a serialised product object that does not include metafields. Getting metafields on that URL won’t be possible because you cannot extend what is being serialised.
Option 1
Like @Zameer wrote, you would need to use Admin API to expose your metafields which would work quite neatly with app proxies. But I understand that’s not what you’re after as you do not wish to implement an app et al.
Option 2
Write an alternate template with no layout, fetch your product metafields there and run them through the | json filter to output JSON. Use AJAX to load that as you’re already doing above. Something like
Extend your product page template by adding a element and do same as above. Then you can read the JSON from within that by going the usual getElementById or $(‘#product-metafields’) and JSON.parse() the content.
Thanks for your reply @KarlOffenberger , you gave me a few ideas. I ended up adding some hidden inputs which I then pulled with JS from the pop up that I was supposed to fill with AJAX. Although I really think that Shopify should include such metas in the product json or add a way to pull them through the Storefront API
I’d agree on the Storefront API and I am sure it will come in time - it’s still quite new and judging by the amount of questions here, not very wildly used. Most merchants have a difficult time adjusting basic HTML and CSS.
I’d sort of disagree on the point that it should be part of the product JSON because it isn’t even part of the product entity. I’d also be weary of the cost of using too many metafields which would happen if they were even easier to use in Liquid or Admin UI - oh the horror, the horror!
We’ve used the .json feature to pull transfer order data into our WMS for a while and all of a sudden it is giving us this error:
{"errors":"Not Found"}
when we add .json to the end of the transfer url. Has something changed? This is a critical part of our processes and would love any help in resolving.