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.)
#Custom attribute is not shown in related entity.
4 messages · Page 1 of 1 (latest)
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"],
})
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)
}
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 ^^
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)
}