Have your say in Community Polls: What was/is your greatest motivation to start your own business?
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.

Save metafields for customers

Save metafields for customers

Baam
Shopify Partner
6 0 0

I have created a Customer metafield is 'Time'; please let me know how I can use the code to save a variable to that metafield

Reply 1 (1)

Liam
Community Manager
3108 344 891

Hi Baam,

 

To save a variable to a Customer metafield with the key 'Time', you can use the customerUpdate mutation, it would look something like:

 

mutation UpdateCustomerMetafield {
  customerUpdate(input: {
    id: "gid://shopify/Customer/1234567890123", # Replace with the actual customer ID
    metafields: [
      {
        namespace: "custom_namespace", # Replace with your namespace
        key: "Time",
        value: "2023-10-03T15:18:59.106Z", # Replace with the value you want to save
        type: "date_time" # Ensure the type matches the value format
      }
    ]
  }) {
    customer {
      id
      metafields(first: 5) {
        edges {
          node {
            namespace
            key
            value
            type
          }
        }
      }
    }
    userErrors {
      field
      message
    }
  }
}

 

In this example:

  • Replace "gid://shopify/Customer/1234567890123" with the actual customer ID.
  • Replace "custom_namespace" with the appropriate namespace for your metafield.
  • Replace the value with the actual value you want to save to the metafield.
  • Ensure the type matches the format of the value you are saving.

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