From 44f475857b809ec7ac0f70d2d6f8d7ed6461f8cc Mon Sep 17 00:00:00 2001 From: giles Date: Sun, 15 Feb 2026 10:11:49 +0000 Subject: [PATCH] Fix AttributeError on g.cart in product cart route MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit g.cart was never populated in the market app — the cart loader before_request hook was only registered in the cart microservice. Replace the dead filter-building code with an actual query that loads cart items inline, scoped to the current user/session. Co-Authored-By: Claude Opus 4.6 --- bp/product/routes.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) 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),