Skip to content

fix: show the header cart badge after an add to cart - #9

Merged
ogorzalka merged 2 commits into
mainfrom
fix/cart-badge-after-add
Sep 25, 2026
Merged

ogorzalka merged 2 commits into
mainfrom
fix/cart-badge-after-add

Conversation

@ogorzalka

Copy link
Copy Markdown
Member

Problem (reported by the maintainer)

On an empty cart, the header badge renders with hidden. After adding a product it stays hidden.

  • WooCommerce's fragment replaced only .cart-count.
  • The MutationObserver in cart.js watched the old .cart-count element, which had just been removed from the page, so it never saw the change.

Measured in a browser (pollora-test, empty cart, variation added):

count class shown
before the add 0 hidden no
after the add 1 hidden no

The Store API cart held the item.

Fix

  • parts/header/cart-badge.blade.php holds the badge and decides flex/hidden itself.
  • The header includes it, and the woocommerce_add_to_cart_fragments filter returns it whole as .cart-badge.
  • The MutationObserver goes (−22 lines of JS).

Test

shop.spec.ts:

  • the badge is hidden on an empty cart;
  • after an add, without a reload, it is visible and shows 1.

It runs in CI; pollora-test's copy of apiary doesn't have the fix.

The badge renders hidden on an empty cart. After an add, WooCommerce's
fragment replaced the .cart-count element, and the MutationObserver
meant to reveal the badge watched the element that had just been
removed: the count read 1, the badge kept its hidden class. Measured in
a browser on an empty cart: count "1", class "hidden", not displayed.

The badge lives in a Blade partial that decides its own visibility,
included by the header and returned whole as the fragment. The observer
goes. The shop spec checks the badge after an add, without a reload.
The archive add-to-cart goes through WooCommerce's own Ajax handler,
not the single product form, and it is where the badge defect was seen
first. The spec creates a category holding only its products, adds one
from that grid and expects the badge shown with 1, without a reload.
Replayed on a site: without the previous commit's fix, the badge stays
hidden and the test fails.
@ogorzalka
ogorzalka merged commit 7e06278 into main Sep 25, 2026
6 checks passed
@ogorzalka
ogorzalka deleted the fix/cart-badge-after-add branch September 25, 2026 15:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant