How do I get product meta fields in Shopify store cart.js response?
Currently, In cart.js not providing any details of product metafields.
How do I get product meta fields in Shopify store cart.js response?
Currently, In cart.js not providing any details of product metafields.
6
Answers
Metafields are available client-side via Liquid. You do not need cartJS to fetch them. You can render the product metafields of interest into your own data structure of choice, and use the as you wish anyway you want.
You could also build out a StorefrontAPI based system and try GraphQL if you’re really keen.
You can access the
metafield
usingitem.product.metafields.your-namespace.your-key
.You have many possible hack.
The one I would recommend if you are using CartJS is
In your product page, print the product metafield in the HTML
When product is added, simply add the metafield as a line item property
var properties = {metafield : $('.product-page').data('metafield')};
CartJS.addItem(variantId, 1 ,properties);
The metafield is now accessible at CartJS.cart.items[i].properties[metafield] !
You can get the metafields content of the appropriate products, collections, orders by assigning it to a variable in the liquid file.
In the
product-template.liquid
, you can use// You can use the Shopify docs to understand how you create Metafields
Access the variable
{{key_val_meta}}
If you assign unique values to the metafield, you could use it to get the exact information you can input that information in your cart.js function.
** You can do this by adding the following step**
Use this code and show data on cart page