Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

How can I retrieve product variant metafields on checkout UI extension?

How can I retrieve product variant metafields on checkout UI extension?

remy727
Shopify Partner
44 2 26
Hi everyone!
Is it possible to retrieve product variant metafields on the checkout UI extension?
Neither useAppMetafields nor useMetafields react hooks don't work.
The solution I found:
  • Get variant IDs with StandardAPI.
  • Then use storefront API for each variant to retrieve metafields.

But if there are 10 line items, this requires 10 storefront API calls.

Is there any better way?

Looking for a Shopify App developer? Please contact me at remy.wang727@gmail.com
Replies 3 (3)

charles_loder
Shopify Partner
25 1 6

Any update on this? I got the same question

lindahlcorp
Shopify Partner
9 0 2

I also came here to find the answer! Any updates Shopify?

 

EDIT: I found a suggestion in github:

 

In addition to using the storefront API, you can use the "app metafields" API. With this API, you list the metafields you want as a metafields key in your shopify.extension.toml, and those metafields are queried on a number of objects in checkout, including the products and variants in cart lines. The useAppMetafields() hook lets you look these metafields up based on the results you get from the cart lines:

import {useCartLines, useAppMetafields} from '@shopify/checkout-ui-extensions-react';

function MyExtension() {
  const firstLine = useCartLines()[0];
  const variantMetafields = useAppMetafields({id: firstLine.merchandise.id});
  const productMetafields = useAppMetafields({id: firstLine.merchandise.product.id});
}
Freelance Shopify Developer & Ecommerce Operator

minitia82
Shopify Partner
3 0 0

Having same problem to get product metafields in webPixel extension

probably related to 
https://community.shopify.com/c/extensions/using-storefront-api-in-custom-web-pixel/td-p/2531794