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.

Re: How do I query the cart line properties attribute?

Solved

How do I query the cart line properties attribute?

aashby13
Shopify Partner
18 5 5

I am trying to query some custom data from the properties attribute from cart lines but failing.

Neither of the following works. Any suggestions? Is it possible?

 

 

query RunInput {
  cart {
    lines {
      id
      quantity
      attribute(key: "properties") {
        value
      }
    }
  }
}
query RunInput {
  cart {
    lines {
      id
      quantity
      attribute(key: "properties") {
        custom_data: value
      }
    }
  }

 

 

Accepted Solution (1)
aashby13
Shopify Partner
18 5 5

This is an accepted solution.

I managed to figure it out. Data added to the properties attribute are considered to be custom properties, so one just has to target the key of that custom property.

query RunInput {
  cart {
    lines {
      id
      quantity
      custom_data: attribute(key: "_custom_data") {
        value
      }
    }
  }

View solution in original post

Replies 14 (14)

MastersHub
Shopify Partner
216 21 38

You can't use all properties. Instead, you should use the 'properties' key. For example:

 

attribute(key: "discount") {
  key
  value
}

Helpful ? Like and Accept Solution
Buy me a Coffee
Need a Shopify Developer?
Send Email or Chat on WhatsApp
aashby13
Shopify Partner
18 5 5

Thanks, but I'm not trying to get all the properties. I'm trying to get the attribute 'properties', the key is 'properties'. Custom data is added to that when the product is added to cart.

 

From the cart.json:

 Screen Shot 2024-01-25 at 12.45.06 AM.png

MastersHub
Shopify Partner
216 21 38

Are you sure it's dependent on the Cart Line properties field? If it is associated with the cart, you should do it like this.

 

query RunInput {
  cart {
attribute(key: "properties") {
value
} lines { id quantity } } }

 

Helpful ? Like and Accept Solution
Buy me a Coffee
Need a Shopify Developer?
Send Email or Chat on WhatsApp
aashby13
Shopify Partner
18 5 5

This is an accepted solution.

I managed to figure it out. Data added to the properties attribute are considered to be custom properties, so one just has to target the key of that custom property.

query RunInput {
  cart {
    lines {
      id
      quantity
      custom_data: attribute(key: "_custom_data") {
        value
      }
    }
  }
rogueyoshi
Shopify Partner
3 0 1

FYI, There's a better way to do this using Inline Fragments:

 

query RunInput {
  cart {
    lines {
      id
      quantity
      attributes {
        ... on Attribute {
          key
          value
        }
      }
    }
  }
}

 

 That will give you an array of all the attributes:

 

"attributes": [
  {
    "key": "test1",
    "value": "true"
  },
  {
    "key": "test2",
    "value": "false"
  }
]

 

JulianVan
Shopify Partner
5 0 0

This does not work with Shopify's cart function input where I've tried.
`attributes` doesn't exist in the schema. I am only able to grab them individually based on keys, not a list.  

rogueyoshi
Shopify Partner
3 0 1

What API version are you using? It's definitely supported in latest.

 

rogueyoshi_0-1721752139130.png

 

Nick_Wesselman
Shopify Staff
171 43 73

Hi @rogueyoshi. This is a screenshot of the Storefront API. Functions APIs have their own GraphQL schemas. Generally, functions cannot query unbounded lists, for performance reasons. Thus you need to specify the specific attribute.

https://shopify.dev/docs/api/functions/reference/product-discounts/graphql/common-objects/cartline

Nick Wesselman | 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 the Shopify Help Center or the Shopify Blog

JulianVan
Shopify Partner
5 0 0

Thanks for the clarification @Nick_Wesselman

If this is the case, is there no way to preserve all of the properties from an expanded product?
I understand I can take the specific values by using the key for each attribute I want, but I have forms with a bunch of inputs/selects that add specific custom properties and the property names aren't all the same every time. 

All I want is all the attributes/properties from the expanded product to be kept through to the order notes when I run a cart expand on it, but as soon as I look at the order details all of the original properties on item that I expanded are gone and it breaks the functionality that I need. 

It even shows them in the checkout screen on the parent of the expanded items but it doesn't carry through to the order notes where we need them in order to see all the options chosen. 

image (1).png

image (2).png

image (3).png

 

aashby13
Shopify Partner
18 5 5

I recently had the same issue. What I ended up doing was adding attributes to ExpandedItems in the cart transform code. ExpandedItem attributes are available in api version 2024-04 and up.

JulianVan
Shopify Partner
5 0 0

 

@aashby13 wrote:

I recently had the same issue. What I ended up doing was adding attributes to ExpandedItems in the cart transform code. ExpandedItem attributes are available in api version 2024-04 and up.


Yeah that's what I'm afraid of, the integration I'm building is for a huge variety of different options and properties that will be possible to have on a product. 

There's no way for me to predict or know exactly what each of the properties keys will be as they will be different on all the products. I think that approach only works if you have the same properties/property names every single time on the products you are expanding. 

aashby13
Shopify Partner
18 5 5

Gotcha. My issue was somewhat similar. I took all the key/value pairs and put them in a nested array and then stringified that nested array and use it as a value in a hidden custom cart attribute. That way I could query the custom attribute and then parse it in my cart transform extension code.

JulianVan
Shopify Partner
5 0 0

That's smart, I will see if I can make something like that route work, basically add all the properties to one hidden property that stays the same and build them on the expanded items off of that. Thank you! 

Would be a really nice feature if you could just preserve them from the base product. 

eric-mainhard
Shopify Partner
1 0 0

Hi @aashby13 first of all! thank you so so so much! I was so stuck.

I was able to get my properties with the custom-data, but now I'm not sure how to set this in the function:


export function run(input) {
  const targets = input.cart.lines
  // Only include cart lines with a quantity of two or more
  .filter(line => line.custom_data && line.custom_data?.value)
  .map(line => {
    return /** @type {Target} */ ({
      // Use the cart line ID to create a discount target
      cartLine: {
        id: line.id
      }
    });
  });