Product reference metafields in alternate templates

Product reference metafields in alternate templates

Bek12
Visitor
1 0 0

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?

Reply 1 (1)

PaulNewton
Shopify Partner
7721 678 1626

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