Methods
# async addItemToCart()
Add (or upsert) a line item to a user’s pending cart. This writes cart_items via our own repository AND updates cart totals.
# async applyCartItemsDiscount()
Create per-item discount rows (optional; depends on your schema).
# async applyDiscount()
Apply a coupon to a cart:
- Validate via couponCodeService
- Create a “cart” discount row (via CartRepository upsertDiscount if you keep that)
- Optionally create per-item discount rows
- Update cart.total
# calculateDiscount()
Very simple discount math; most teams move this to the coupon service. Expects coupon entities to expose getType()/getAmount() or you adapt here.
# async clearCart()
Clear all items and reset totals.
# async deleteCartItem()
Delete a single item and refresh totals.
# async findOrCreatePendingCart()
Find or create a pending cart for the user.
# async findPendingCartForUser()
Find the user’s pending cart, with items. If none, returns null.
# async getCartWithItems()
Get a cart by id with user + items. Returns raw with relations (or you can wrap if you prefer Entities here).
# getProductService()
Map entity_type -> service used to read that product. Throws if a service is not configured.
# async updateTotals()
Recompute totals from the current cart_items rows. (This is a placeholder; customize your tax/discount strategy as needed.)
# async upsertCartWithItems()
Upsert a batch of items into a user’s pending cart.