Cart Storefront API cartMetafieldsSet usage and order relation

Cart Storefront API cartMetafieldsSet usage and order relation

itooze
Shopify Partner
3 0 0

Able to setup the cartMetafieldsSet using StoreFrontAPI properly. 
Now, my main concerns is how it will save automatically when order will placed. Or do I need to save those values using webhooks when order is placed?

I just want to setup few custom metafields on cart and than access them in Order Admin API later. But I am stuck how I can access these values. 

We are not using shopify plus plan, so modifying the checkout attribute is not possible.

Using this code to create metafields for cart

 

 

 

const cartid = 'gid://shopify/Cart/' + carttoken;
        const getcartQuery =
           `    mutation cartMetafieldsSet($cartID: ID!) {
            cartMetafieldsSet(
              metafields:[
                {
                  ownerId: $cartID,
                  key: "public.materials",
                  type: "multi_line_text_field",
                  value: "95% Cotton"
                },
                {
                  ownerId: $cartID,
                  key: "public.manufactured",
                  type: "single_line_text_field",
                  value: "Made in Canada"
                }
            ]) {
              metafields {
                namespace
                key
                value
                type
              }
              userErrors {
                code
                field
                message
              }
            }
          }`
        ;
        const response = await graphql(
          getcartQuery, { variables: { cartID: cartid } }
        );

 

 

 

 

Thank You

Replies 6 (6)

Liam
Shopify Staff
2873 312 820

Hi Itooze,

 

Are you looking to save the metafield values on the order itself, or save them to your database when the order is made?

Liam | Developer Advocate @ Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

itooze
Shopify Partner
3 0 0

Hi Liam

I want to save these cart metafields to order itself. That is the approach I am looking for. 

Thank You

itooze
Shopify Partner
3 0 0

HI Liam

 

Do you get a chance to look into this and let me know the best possible way to achieve the same.

 

Thank You

Liam
Shopify Staff
2873 312 820

Hi Itooze,

 

I'm still looking to figure this out - do you want the metafield values to be saved to an order as an order metafield? Is there already an order metafield definition set up that should accept the cart metafield value?

Liam | Developer Advocate @ Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

shpfyaficionado
Shopify Partner
1 0 0

Hello @Liam,

I think @itooze, like me, was hoping for the metafields in the cart level to be transferred to the order. Is that possible using the approach that @itooze mention on their initial post?

Thanks!

EverestX
Visitor
1 0 0

Hi @Liam Also curious what the preferred approach is on this. Looking for a way to pass information derived from cart interaction and checkout interaction to the order after checkout.