commerce icon indicating copy to clipboard operation
commerce copied to clipboard

How to use shopify metafields?

Open joshbedo opened this issue 2 years ago • 1 comments

Is their currently any way to use shopify metafields? Right now i have a scenario where i need to pass data from add to cart to the order metafield. Is this possible? Basically trying to attach more order information. TIA!

joshbedo avatar May 14 '22 17:05 joshbedo

Yes this is possible, here's how I did it:

  1. Add your metafield query in packages\shopify\src\utils\queries\get-product-query.ts
# ...
myMetafield: metafield(namespace: "my_namespace", key: "my_key") {
        description
        id
        key
        namespace
        value
        valueType
}
  1. Add property to the Product type in packages\commerce\src\types\product.ts
export type Product = {
  // ...
  myMetafield: Metafield
}

export interface Metafield {
  description: string
  id: string
  key: string
  namespace: string
  value: string // depend on your metafield content type
  valueType: string
}
  1. I can now use it inside the ProductSidebar component for example
// ...
<Collapse title="Care">
    { product.myMetafield.value }
</Collapse>

fafifox avatar May 20 '22 18:05 fafifox

Hey there! Thank you for your contribution. We have decided to take Next.js Commerce in a new direction and will be closing out current PRs and issues due to this change. Please see this PR for more details: https://github.com/vercel/commerce/pull/966

leerob avatar Apr 18 '23 02:04 leerob