#Custom attribute is not shown in related entity.

4 messages · Page 1 of 1 (latest)

cloud reef
#

I added 'is_excluded' field in LineItem entity, then completed migration steps as guided in 'How to Extend an Entity(https://docs.medusajs.com/development/entities/extend-entity)' docs.
Then I retrieve a cart entity, I expect 'item' object of 'cart.items[]' has 'is_excluded' field, but there is no 'is_excluded' field.
Is there further configuration needed to access a custom attribute in a related entity?
(I already added custom attributes to serveral entities successfully, but, this is the only case doesn't work.)

mild glade
#

hi,
I don't know if it would be ideal to extend your Cart or LineItem service to add your new property to the select property of a TypeORM query.

For example, when you retrieve a shopping cart on the store side, you can see that what's passed to the service is a select property with several keys :

const cart = await cartService.retrieve(id, {
    select: ["id", "customer_id"],
  })

Source : https://github.com/medusajs/medusa/blob/7a12aa04ac755c4bb4324d78a0bd1dc7d05757d2/packages/medusa/src/api/routes/store/carts/get-cart.ts#L54C14-L54C14

The idea would be to extend the service and override the default selector:

// In an extended `Cart` service

/**
   * Gets a cart by id.
   * @param cartId - the id of the cart to get.
   * @param options - the options to get a cart
   * @param totalsConfig
   * @return the cart document.
   */
  async retrieve(
    cartId: string,
    options: FindConfig<Cart> = {},
    totalsConfig: TotalsConfig = {}
  ): Promise<Cart> {
  
  const extendedSelect = ['is_excluded']
  const newOptions = {
  ...options,
  select: [...extendedSelect, ...options.select]
  }

  return await super.retrieve(cartId, options, totalsConfig)
}
GitHub

Building blocks for digital commerce. Contribute to medusajs/medusa development by creating an account on GitHub.

#

Don't rely on the code, I don't know if it's really the right syntax, but you get the idea.

PS: I don't know if anyone has a simpler solution, but in any case, that's how I've managed certain cases on my side ^^

cloud reef
# mild glade hi, I don't know if it would be ideal to extend your `Cart` or `LineItem` servic...

Thanks, adevinwild! sadly, I tried similar approach but it didn't work. because 'is_excluded' is in LineItem entity, not in Cart entity.

  async retrieve(
    cartId: string,
    options: FindConfig<Cart> = {},
    totalsConfig: TotalsConfig = {}
  ): Promise<Cart> {
  
  const extendedSelect = ['items.is_excluded']   // <== this didn't work.
  const newOptions = {
  ...options,
  select: [...extendedSelect, ...options.select]
  }

  return await super.retrieve(cartId, options, totalsConfig)
}