Render cart-mini directly in add-to-cart HTMX response
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 59s
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 59s
The handler already has g.cart with the updated items — no need to make an HTTP round-trip to the cart app's fragment endpoint. Renders the _mini.html macro directly with the count from g.cart. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -254,23 +254,8 @@ def register():
|
||||
|
||||
# htmx response: OOB-swap mini cart + product buttons
|
||||
if request.headers.get("HX-Request") == "true":
|
||||
# Commit so the cart app sees the updated data when we fetch the fragment
|
||||
await g.tx.commit()
|
||||
g.tx = await g.s.begin()
|
||||
|
||||
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,
|
||||
)
|
||||
|
||||
@@ -1,13 +1,9 @@
|
||||
{# 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 (fallback to local macro) #}
|
||||
{% if cart_mini_html %}
|
||||
{{ cart_mini_html | safe }}
|
||||
{% else %}
|
||||
{% from '_types/cart/_mini.html' import mini with context %}
|
||||
{{ mini(count=cart | sum(attribute="quantity")) }}
|
||||
{% endif %}
|
||||
{# 1. Update mini cart directly — handler already has the cart data #}
|
||||
{% from '_types/cart/_mini.html' import mini with context %}
|
||||
{{ mini(count=cart | sum(attribute="quantity")) }}
|
||||
|
||||
{# 2. Update add/remove buttons on the product #}
|
||||
{{ _cart.add(d.slug, cart, oob='true') }}
|
||||
|
||||
Reference in New Issue
Block a user