I’ve been exploring and testing for hours how to retrieve the metafield values from a specific product handle.
I’ve tested using AJAX calls and using getJSON neither of which have had any success with all seem to throw the same error.
I’m aware that you most definitely can’t access the product metafields directly as they’re somewhat protected and therefore are excluded from the usual handle.js file I’m currently successfully able to get.
I am seeking someones help to essentially point me in the direction of how to accomplish what I’m looking for.
product-view.js file
$(document).ready(function () {
$.getScript("//cdnjs.cloudflare.com/ajax/libs/fancybox/2.1.5/jquery.fancybox.min.js").done(function() {
createProductData();
});
});
function createProductData() {
$(".product-view").click(function () {
if ($('#product-view').length == 0){
$("body").append('<div id="product-view"></div>');
}
var product_handle = $(this).data('handle');
$('#product-view').addClass(product_handle);
//Default Product Fields
jQuery.getJSON('/products/' + product_handle + '.js', function (product) {
var images = product.images;
var title = product.title;
var price = product.price;
var desc = product.description;
});
//Product Meta Fields
jQuery.getJSON('/products/' + product_handle + '?view=metafields.json', function (product) {
var dish_type = product.metafields.arena.dish;
console.log(dish_type);
//error: Cannot read properties of undefined (reading 'arena')
});
});
}
product.metafields.liquid file
{% layout none %}
{{ product.metafields.arena | json }}
2
Answers
So I managed to find a website finally that used what they described as a 'hack' because they don't believe this is the way that
{% capture %}
however, it did achieve the require outcome that I needed and hopefully is useful for others in future:theme.liquid file
You can then access the
dishMetaFields
from the javascript file you're using to access the product object to get the defined metafields should the handles match.product-view.js
You can also do it like this following your initial approach. In this scenario, you don’t need to iterate over all metafields of a collection but only for the required product. This should be better comparatively in terms of performance. The idea is same, that you generate the JSON object in Liquid snippet and then parse the JSON in JavaScript.
And for JavaScript part, use simple GET and then parse the response.