Discuss and resolve questions on Liquid, JavaScript, themes, sales channels, and site speed enhancements.
Hi, I'm trying to retrieve some product metafields via ajax using an alternate template (?view=metafields.json).
$.ajax({
url: "/products/" + data.handle + "?view=metafields.json",
success: function(data){
console.log(data);
}
});
The metafields are product references which I need the urls from. Normally I would use {{product.metafields.namespace.key.value.url}} to access the product object and get the url but this isn't working within the template. The only data I can get is the product reference itself in the format gid://shopify/Product/XXXXXXXX, which I can't use.
My template is just a .json.liquid file using {% layout none %}
Using {{ product.metafields.namespace.key }} gives me the global ID of the product, but {{ product.metafields.namespace.key.value }} returns null.
Is there any way I can get the url within the alternate template so that it can be returned in my ajax request? Or alternatively is there a way I can use the global ID to get the product info easily?
I'll try to check and test this later if I remember
But does {{product.metafields.namespace.key.url}} work?
Other things to account for:
Is the product_reference individual mf's or lists.
What renders in the template source directly
In the alt template are you trying to filter the metafields with the JSON filter.
What happens if you used the metafield_tag filter
https://shopify.dev/api/liquid/filters/metafield_tag#metafield_tag-product_reference
Unfortunately the metafield_text filter returns a product title instead of a handle for some inane reason which isn't reliable for building urls.
Contact paull.newton+shopifyforum@gmail.com for the solutions you need
Save time & money ,Ask Questions The Smart Way
Problem Solved? ✔Accept and Like solutions to help future merchants
Answers powered by coffee Thank Paul with a ☕ Coffee for more answers or donate to eff.org
Shopify and our financial partners regularly review and update verification requiremen...
By Jacqui Mar 14, 2025Unlock the potential of marketing on your business growth with Shopify Academy's late...
By Shopify Mar 12, 2025Learn how to increase conversion rates in every stage of the customer journey by enroll...
By Shopify Mar 5, 2025