diff --git a/bp/product/routes.py b/bp/product/routes.py index ca230c8..4ea209a 100644 --- a/bp/product/routes.py +++ b/bp/product/routes.py @@ -252,19 +252,23 @@ def register(): # no explicit commit; your session middleware should handle it - # htmx support (optional) + # htmx response: OOB-swap mini cart + product buttons if request.headers.get("HX-Request") == "true": - # You can return a small fragment or mini-cart here + from shared.infrastructure.fragments import fetch_fragment + from shared.infrastructure.cart_identity import current_cart_identity as _ci + frag_ident = _ci() + cart_params = {} + if frag_ident["user_id"] is not None: + cart_params["user_id"] = frag_ident["user_id"] + if frag_ident["session_id"] is not None: + cart_params["session_id"] = frag_ident["session_id"] + cart_mini_html = await fetch_fragment("cart", "cart-mini", params=cart_params) return await render_template( "_types/product/_added.html", + cart_mini_html=cart_mini_html, cart=g.cart, item=ci, - total=total, - cart_count=sum(i.quantity for i in g.cart), - cart_total=total(g.cart), - calendar_total=lambda entries: 0, - calendar_cart_entries=[], ) # normal POST: go to cart page diff --git a/templates/_types/product/_added.html b/templates/_types/product/_added.html new file mode 100644 index 0000000..ad44b9d --- /dev/null +++ b/templates/_types/product/_added.html @@ -0,0 +1,16 @@ +{# HTMX response after add-to-cart: OOB-swap the mini cart + product buttons #} +{% import '_types/product/_cart.html' as _cart %} + +{# 1. Update mini cart via cart-mini fragment #} +{{ cart_mini_html | safe }} + +{# 2. Update add/remove buttons on the product #} +{{ _cart.add(d.slug, cart, oob='true') }} + +{# 3. Update cart item row if visible #} +{% from '_types/product/_cart.html' import cart_item with context %} +{% if item and item.quantity > 0 %} + {{ cart_item(oob='true') }} +{% elif item %} + {{ cart_item(oob='delete') }} +{% endif %}