From d7162f55434193a2b21e59f259c8b5441e274852 Mon Sep 17 00:00:00 2001 From: giles Date: Thu, 19 Feb 2026 05:17:04 +0000 Subject: [PATCH] Fix inconsistent cart count: include calendar entries in market app Market context processor was only counting product CartItems for cart_count, while blog/cart/events apps include calendar entries too. Use cart service for consistent counts across all apps. Co-Authored-By: Claude Opus 4.6 --- app.py | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/app.py b/app.py index dd6fd0c..266adbf 100644 --- a/app.py +++ b/app.py @@ -18,10 +18,12 @@ async def market_context() -> dict: Market app context processor. - menu_items: direct DB query - - cart/cart_count/cart_total: direct DB (market owns CartItem/Product) + - cart_count/cart_total: via cart service (includes calendar entries) + - cart: direct ORM query (templates need .product relationship) """ from shared.infrastructure.context import base_context from shared.services.navigation import get_navigation_tree + from shared.services.registry import services from shared.infrastructure.cart_identity import current_cart_identity from shared.models.market import CartItem from sqlalchemy.orm import selectinload @@ -30,30 +32,29 @@ async def market_context() -> dict: ctx["menu_items"] = await get_navigation_tree(g.s) - # Market owns CartItem/Product — query directly for template compat ident = current_cart_identity() + + # cart_count/cart_total via service (consistent with blog/events apps) + summary = await services.cart.cart_summary( + g.s, user_id=ident["user_id"], session_id=ident["session_id"], + ) + ctx["cart_count"] = summary.count + summary.calendar_count + ctx["cart_total"] = float(summary.total + summary.calendar_total) + + # ORM cart items for product templates (need .product relationship) filters = [CartItem.deleted_at.is_(None)] - if ident["user_id"]: + if ident["user_id"] is not None: filters.append(CartItem.user_id == ident["user_id"]) - elif ident["session_id"]: + elif ident["session_id"] is not None: filters.append(CartItem.session_id == ident["session_id"]) else: ctx["cart"] = [] - ctx["cart_count"] = 0 - ctx["cart_total"] = 0 return ctx result = await g.s.execute( select(CartItem).where(*filters).options(selectinload(CartItem.product)) ) - cart_items = list(result.scalars().all()) - - ctx["cart"] = cart_items - ctx["cart_count"] = sum(ci.quantity for ci in cart_items) - ctx["cart_total"] = float(sum( - (ci.product.special_price or ci.product.regular_price or 0) * ci.quantity - for ci in cart_items if ci.product - )) + ctx["cart"] = list(result.scalars().all()) return ctx