Access custom fields in order product (such as text for tshirt)

I have a product where some custom fields are filled by customers when they order. I cannot find a way to access those fields in the graphql order query. I would expect something like metafields in the lineitem.

1 Like

Hey @user125 :waving_hand: - are you using an app to set those custom fields up?

I can’t say for sure, but sometimes apps that allow for customization will actually use Order tags or order attributes rather than metafields to store that data.

If you’re able to share an Order ID (the GID), I can take a look on our end to see if we can figure out where those field values are being set.

The product page template has a liquid section that contains the code shown in the question. It presents the fields for the buyer to fill in. Currently i am using shopify sharp to get the orders and i have access to the data as property fields. I am trying to switch to graphql but finding it lacking.

gid://shopify/Order/5862832046371

Don’t worry about it – just tripped over the solution.

Do have one other issue with the system.

If I add a product with custom attributes to an order, everything is good.

However, I cannot add a second item with different attributes because the process just increases the quantity of the first item. It should create a separate line-item with the attributes of the second thing ordered.

Hey @user125 - no worries, glad you found the solution there!

Not sure how you’re creating those orders, but if you’re using something like draftOrderCreate, you should be able to create two separate line items for the same variant with different custom attributes/options like this in GraphQL:

mutation CreateDraftOrderWithVariantIds {
  draftOrderCreate(
    input: { 
      lineItems: [
        {
          variantId: "gid://shopify/ProductVariant/ID-here", 
          quantity: 1,
          customAttributes: [ 
            { key: "engraving", value: "Attribute 1" }
          ]
        },
        {
          variantId: "gid://shopify/ProductVariant/same-id-here", 
          quantity: 1,
          customAttributes: [
            { key: "engraving", value: "Attribute 2" }
          ]
        }
      ]
     
    }
  ) {
    draftOrder {
      id 
      name
      invoiceUrl 
      status
      totalPriceSet {
        shopMoney {
          amount
          currencyCode
        }
      }
      lineItems(first: 10) {
        edges {
          node {
            id
            title
            variant {
              id 
            }
            customAttributes {
              key
              value
            }
          }
        }
      }
    }
    userErrors {
      field
      message
    }
  }
}

Hope this helps - let me know if you’re encountering any other blockers, happy to keep helping.

We have a 0 cost product for the customer to create a request for a quote right from the store. To deal with this issue I restrict them to one lineitem per order. That precludes any dynamic creating of variants.

Ah - okay, thanks for confirming that @user125. Would something like this work? You can set multiple attributes on a single line item as well, it should keep the quantity the same:

mutation CreateDraftOrderWithMultipleAttributesOnOneLineItem {
  draftOrderCreate(
    input: {
      lineItems: [
        {
          variantId: "gid://shopify/ProductVariant/52731544895510",
          quantity: 1,
          customAttributes: [
            { key: "key1", value: "text1" },
            { key: "key2", value: "text2" },
            { key: "key3", value: "text3" },
            { key: "key4", value: "text4" }
          ]
        }
      ]
    }
  ) {
    draftOrder {
      id
      name
      invoiceUrl
      status
      totalPriceSet {
        shopMoney {
          amount
          currencyCode
        }
      }
      lineItems(first: 10) {
        edges {
          node {
            id
            title
            quantity
            variant {
              id
            }
            customAttributes {
              key
              value
            }
          }
        }
      }
    }
    userErrors {
      field
      message
    }
  }
}

Or is the issue that you can’t edit pre-existing orders to add attributes to a line item without the quantity changing? If you can share the full API call/variable inputs you’re making where you see the line item quantity get bumped up, I can try to replicate on my end and see if we can look into a workaround.

Not really.

My customers use the standard Shopify store front with no customization other than the product template having some liquid code to capture the custom attributes. I don’t want to get into the maintenance of a more complicated system than that like a custom store front.

Unless I can ‘change’ the master product to a variant using liquid code and have that added to the cart this avenue is not really viable.