Fix inconsistent cart count: include calendar entries in market app
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 41s
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 41s
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 <noreply@anthropic.com>
This commit is contained in:
29
app.py
29
app.py
@@ -18,10 +18,12 @@ async def market_context() -> dict:
|
|||||||
Market app context processor.
|
Market app context processor.
|
||||||
|
|
||||||
- menu_items: direct DB query
|
- 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.infrastructure.context import base_context
|
||||||
from shared.services.navigation import get_navigation_tree
|
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.infrastructure.cart_identity import current_cart_identity
|
||||||
from shared.models.market import CartItem
|
from shared.models.market import CartItem
|
||||||
from sqlalchemy.orm import selectinload
|
from sqlalchemy.orm import selectinload
|
||||||
@@ -30,30 +32,29 @@ async def market_context() -> dict:
|
|||||||
|
|
||||||
ctx["menu_items"] = await get_navigation_tree(g.s)
|
ctx["menu_items"] = await get_navigation_tree(g.s)
|
||||||
|
|
||||||
# Market owns CartItem/Product — query directly for template compat
|
|
||||||
ident = current_cart_identity()
|
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)]
|
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"])
|
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"])
|
filters.append(CartItem.session_id == ident["session_id"])
|
||||||
else:
|
else:
|
||||||
ctx["cart"] = []
|
ctx["cart"] = []
|
||||||
ctx["cart_count"] = 0
|
|
||||||
ctx["cart_total"] = 0
|
|
||||||
return ctx
|
return ctx
|
||||||
|
|
||||||
result = await g.s.execute(
|
result = await g.s.execute(
|
||||||
select(CartItem).where(*filters).options(selectinload(CartItem.product))
|
select(CartItem).where(*filters).options(selectinload(CartItem.product))
|
||||||
)
|
)
|
||||||
cart_items = list(result.scalars().all())
|
ctx["cart"] = 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
|
|
||||||
))
|
|
||||||
|
|
||||||
return ctx
|
return ctx
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user