diff --git a/bp/product/routes.py b/bp/product/routes.py index a3807e6..04b0fe9 100644 --- a/bp/product/routes.py +++ b/bp/product/routes.py @@ -192,11 +192,23 @@ def register(): ident = current_cart_identity() - filters = [CartItem.deleted_at.is_(None), CartItem.product_id == product_id] + # Load cart items for current user/session + from sqlalchemy.orm import selectinload + cart_filters = [CartItem.deleted_at.is_(None)] if ident["user_id"] is not None: - filters.append(CartItem.user_id == ident["user_id"]) + cart_filters.append(CartItem.user_id == ident["user_id"]) else: - filters.append(CartItem.session_id == ident["session_id"]) + cart_filters.append(CartItem.session_id == ident["session_id"]) + cart_result = await g.s.execute( + select(CartItem) + .where(*cart_filters) + .order_by(CartItem.created_at.desc()) + .options( + selectinload(CartItem.product), + selectinload(CartItem.market_place), + ) + ) + g.cart = list(cart_result.scalars().all()) ci = next( (item for item in g.cart if item.product_id == product_id),