Skip to content

Fix - Header cart count and total stale after AJAX add to cart - #143

Open
rajatgautam755421 wants to merge 2 commits into
developfrom
fix/109-header-cart-count-fragment
Open

rajatgautam755421 wants to merge 2 commits into
developfrom
fix/109-header-cart-count-fragment

Conversation

@rajatgautam755421

Copy link
Copy Markdown

Changes proposed in this Pull Request:

Fixes the free-theme part of themegrill/spacious-pro#109 (the issue notes it is also present in the free theme). The same fix for Pro is themegrill/spacious-pro#189, already reviewed by Copilot with approval recommended; this PR ports that final version.

The header cart (item count and total) was rendered once at page load and never registered as a WooCommerce cart fragment. After an AJAX add to cart, WooCommerce refreshed its own mini-cart, but the header badge and total kept their old values until the page was reloaded.

The header cart block now lives in one function, spacious_cart_icon_views(), used by both the initial render (spacious_cart_icon()) and a new woocommerce_add_to_cart_fragments filter (spacious_cart_icon_fragment()). Both new functions are wrapped in function_exists() like the rest of the file.

Effect on existing sites

  • Sites that do not use the header cart icon are unaffected: the filter returns early unless Customize > WooCommerce > Design > "Check to show WooCommerce cart icon on menu bar" is on.
  • The first page render is unchanged: the header cart HTML matches the original output. The render condition in spacious_cart_icon() is left exactly as it was.
  • A child theme that overrides spacious_cart_icon() keeps its own markup: the fragment filter is registered together with the default spacious_cart_icon(), so it is not added when that function is overridden.
  • The fragment never overwrites one that a plugin or child theme already supplies for the same selector.
  • The mini-cart widget inside .cart-wrapper is left alone; WooCommerce keeps updating it itself.

How to test the changes in this Pull Request:

  1. Go to Customize > WooCommerce > Design, tick "Check to show WooCommerce cart icon on menu bar" and publish. Make sure WooCommerce > Settings > Products > "Enable AJAX add to cart buttons on archives" is on.
  2. With an empty cart, open the Shop page. The header badge reads 0.
  3. Click Add to cart on a product (the page must not reload) and wait for the "View cart" link. Before the fix the badge and total stay at 0 / $0.00 until you reload. After the fix they update straight away and match the mini-cart.
  4. Add a second product, then remove items through the mini-cart. The badge and total follow each change, including back to 0 / $0.00.
  5. Untick the cart icon setting and publish. The header cart disappears and nothing else changes.

Types of changes:

  • Bug fix (non-breaking change which fixes an issue)
  • Enhancement (modification of the currently available functionality)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Other information:

  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you successfully ran tests with your changes locally? Verified manually in the browser with Playwright (details below). No automated test was added.
  • Have you updated the documentation accordingly?

Changelog entry

Fix - Header cart count and total now update right after an AJAX add to cart, without a page reload.

Verification (Spacious 1.9.12, WooCommerce, logged-in session)

  • Reproduced on the original code first: after an AJAX add the header showed 0 / $0.00 while the mini-cart showed $32.00, and the AJAX response contained only div.widget_shopping_cart_content.
  • With the fix: adds update the header to 1 / $32.00 and 2 / $97.00, removals count down to 1 / $65.00 and 0 / $0.00, always matching the mini-cart, with a single header block. Each response includes the div.spacious-woocommerce-cart-views fragment.
  • The first-render header cart HTML is identical to the original when whitespace runs are collapsed.
  • Compatibility, tested with a temporary must-use plugin (since removed): with a simulated child-theme override of spacious_cart_icon() the page renders the override and the theme sends no header fragment; a site's own fragment for the same selector is preserved.
  • Cart icon setting off: WooCommerce's fragments contain only its own div.widget_shopping_cart_content.
  • Translations: the moved Total string changed its source line, so languages/spacious.pot was regenerated with the makepot options from Gruntfile.js. The diff is only the creation date and source line references.
  • PHPCS (WordPress standard) on inc/header-functions.php: no violations added or removed (42 errors and 8 warnings before and after, all in older code). PHPCompatibilityWP 7.4-: clean.

🤖 Generated with Claude Code

rajatgautam755421 and others added 2 commits September 25, 2026 10:25
The header cart block was rendered once at page load and never registered
as a WooCommerce cart fragment, so it only refreshed on a full reload
while the mini-cart updated. Move the block into a shared function used
by both the initial render and a woocommerce_add_to_cart_fragments
filter. The filter is registered with the default spacious_cart_icon(),
so a child theme overriding it keeps its own markup, and it never
overwrites a fragment already supplied for the same selector.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Regenerated with the makepot options from Gruntfile.js. Only the creation
date and source line references change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@rajatgautam755421 rajatgautam755421 self-assigned this Sep 25, 2026
@rajatgautam755421 rajatgautam755421 added bug severity:minor Works, but wrongly, with a workaround labels Sep 25, 2026
@tg-autopilot
tg-autopilot requested a lite review from Copilot September 25, 2026 04:40
@github-actions

Copy link
Copy Markdown

QA suite — refused, no regression spec

This PR changes product source but adds no spec, so the suite was
refused before booting WordPress — running it just to report the same
thing at the end costs runner minutes for nothing. Run
/claudegrill:verify-fix locally and let write-spec add the guard
to this branch, then push again.

Source files changed with no matching spec
inc/header-functions.php

@rajatgautam755421

Copy link
Copy Markdown
Author

Screenshots

Free Spacious theme, Shop page, cart started empty, then one product added with the AJAX Add to cart button (no page reload). The outline marks the header cart icon and the "View cart" link that appears once the add has succeeded.

Before (develop): header badge still 0 After (this PR): header badge is 1 straight away
before-fix-after-ajax-add after-fix-after-ajax-add

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🔵 Needs a closer look

Adjust fragment registration priority to preserve later-added fragments.

Review effort: Lite
Findings: None

What changed in this PR

Fixes stale WooCommerce header cart counts and totals after AJAX cart updates.

Changes:

  • Adds reusable header cart rendering and WooCommerce fragment support.
  • Preserves existing overrides and fragments.
  • Regenerates translation references.
File Summary
inc/​header-functions.php Adds shared rendering and AJAX fragment synchronization; callback priority needs adjustment to prevent later providers from overwriting the fragment.
languages/​spacious.pot Updates generated translation metadata and source references.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@rajatgautam755421

Copy link
Copy Markdown
Author

@tg-autopilot review

This ports the final, Copilot-approved version of themegrill/spacious-pro#189 to the free theme. Please review.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟢 Approval recommended

No unresolved review issues remain.

Review effort: Lite
Findings: None

@rajatgautam755421

Copy link
Copy Markdown
Author

Note on the first automated review ("adjust fragment registration priority to preserve later-added fragments"): no change needed. Plugins and child themes load before the parent theme's functions.php, so their woocommerce_add_to_cart_fragments filters at the default priority run before ours, and spacious_cart_icon_fragment() leaves an existing fragment for the same selector untouched. Filters added later, or at a later priority, run after ours and replace our value. Either way the other fragment is preserved, which is why the follow-up review reports no unresolved issues. This matches themegrill/spacious-pro#189.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug severity:minor Works, but wrongly, with a workaround

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants