Cart attributes rarely carry over to customAttributes on Order object

Basically the title. When i add cart attributes with the cartAttributesUpdate mutation, they don’t reliably get carried over into the customAttributes field on the Order object. Happens like one tenth of the time.

Does anyone know what’s going on?

Happy to provide some ids if that would be helpful.

Did some debugging, looks like cart attributes added via cartAttributesUpdate only get transferred to the order object if the customer reloads the checkout page and therefore loads in the new attributes.

Is this the expected behavior? If so, what’s the point of the cartAttributesUpdate mutation if it requires a page reload for the changes to be reflected?

I need a way to transfer info from the cart to the order, and it seems like attributes are the only way to do this, but I’m not sure how to make this work.

1 Like

Hey @aaron_barbieri, since this is happening one tenth of the time, it may be tough to replicate. Can you share a little more about the attributes you’re adding and the mutations made leading to checkout?

Do you by chance have an x-request-id from the mutation made showing the attributes applied but that didn’t carry over to the checkout (without the refresh)?

Hi Kyle, the “one tenth” was before i realized that i was occasionally refreshing the page without knowing.

If you don’t refresh checkout, the cart attributes added via the graphQL mutation never get carried over.

I need to be able to set attributes from my backend, and have them be reflected in the order object. Let me know if you need any more details!

Thanks for that clarity. I would like to try and replicate this. Can you share the mutations you are making that lead a checkout without the attributes added?

For sure. It’s this one from the storefront API. And the cartId I pass has the key param of course.

mutation cartAttributesUpdate($attributes: [AttributeInput!]!, $cartId: ID!) {

  cartAttributesUpdate(attributes: $attributes, cartId: $cartId) {

  }

}

The attributes successfully get added to the cart, but unless the shopper refreshes their checkout, they aren’t “pulled in” and therefore don’t carry over to the order object. It seems like cart attributes have to make it into client side local storage for them to successfully migrate to the order object, otherwise they’re left behind.

Hey Aaron,

Are you noticing this happen on all stores or is is specific to a single store?

I’ve been testing here and I can’t replicate. This is the process I’m testing. Is anything different than what you are doing?

Step 1: create a cart

mutation CreateCart($input: CartInput!) {
  cartCreate(input: $input) {
    cart {
      id
      checkoutUrl
      buyerIdentity {
        email
        countryCode
      }
      lines(first: 5) {
        edges {
          node {
            id
            quantity
            merchandise {
              ... on ProductVariant {
                id
                title
                product {
                  title
                }
              }
            }
          }
        }
      }
    }
    userErrors {
      field
      message
    }
  }
}

Step 2: UpdateCartAttributes Mutation

mutation UpdateCartAttributes($cartId: ID!, $attributes: [AttributeInput!]!) {
  cartAttributesUpdate(cartId: $cartId, attributes: $attributes) {
    cart {
      id
      checkoutUrl
      attributes {
        key
        value
      }
    }
    userErrors {
      field
      message
    }
  }
}

Step 3:
Visit the Checkout URL returned in the response and complete purchase

Step 4:
Check order details in the admin. Attributes are applied

I just noticed your post here. Modify cart attributes with checkout extension

Am I right to assume that the customer is already on the checkout page when you’re applying the mutation? If so, you would need to use checkout extensions for that.

If you apply them pre-checkout though, then the above process should work as above.

Hey Kyle,

The issue is that you’re visiting the checkout URL for the first time after you’ve added the attributes, which has the same effect as reloading the page. What I’m saying is slightly different:

  1. Initiate checkout as a customer usually would, by clicking “checkout” on the front end
  2. Read the cart cookie which contains the cartId as well as a secret key param
  3. Modify the cart attributes using the cartAttributesUpdate mutation
  4. Without refreshing the page, complete checkout
  5. The attributes will not get carried into the order

This should happen every single time.

Just saw your response! Ok that explains it.

So even if the cart attributes are set from the backend, as long as i do it before the user clicks “checkout”, they should get pulled in?

Yes, as long as they are set and the checkout URL is using the proper key parameters (like you mentioned it is).

Alternatively, you can also use the admin API orderUpdate mutation to add the custom attributes after the order is placed.

1 Like

I have the opposite problem, my users don’t want the cart attributes to move to the order additional information fields, is that only possible if we ask for the invasive “write orders” permission?