SaaS CMS has officially launched! Learn more now.

Type of Variants in the Cart



Wondering is their any way to find out type of variants added to the cart? ex: cart.GetAllLineItems() returns InMemoryLine item - how do I know what's variant type without loading content item using variant code.



Jul 02, 2024 14:38

Hi Shakti,

Yes, however you will first need to extend your lineitems with a metafield / property to do this do the following:

  1. Extend the metafield for lineitems or extend the lineitems in the serializablecart to include an additional property.
  2. When saving lineitems to the cart ensure you retrieve the variant type and then store this against the lineitem.
  3. Retrieve the line item and the metafield / property.

I have put together some example code that could possibly be used however you will need to test yourself:

Jul 02, 2024 21:35
SG - Jul 03, 2024 7:58
Thanks Paul

Hi Shakti

It would probably be most efficient to load the variant by code. The variants are already cached in Commerce. So loading the variant is usually an insignificant operation. Which scenario do you have where you need to know the type of the variant, without loading the actual variant.

If you store the catalog entry type on the line item, you may waste space in the database. And you risk the data integrity (e.g.g if you change a type somewhere else, and don't update it in the carts).

Jul 07, 2024 11:36

GitHub - vimvq1987/CatalogContentTypeResolver could be a good way to resolve the content type without having to load the content itself

Jul 09, 2024 6:10
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.