Skip to content

Add WooCommerce 10.4+ Interactivity API Mini Cart compatibility#529

Merged
lysyjan merged 3 commits intotrunkfrom
fix-woocommerce-10-4-compatibility
Jan 7, 2026
Merged

Add WooCommerce 10.4+ Interactivity API Mini Cart compatibility#529
lysyjan merged 3 commits intotrunkfrom
fix-woocommerce-10-4-compatibility

Conversation

@lysyjan
Copy link
Contributor

@lysyjan lysyjan commented Nov 30, 2025

Description

WooCommerce 10.4 introduces a new Mini Cart component built with the Interactivity API, replacing the previous React-based implementation. This change breaks two analytics tracking features:

  1. remove_from_cart event not firing - The new Mini Cart doesn't fire the experimental__woocommerce_blocks-cart-remove-item action hook
  2. begin_checkout event with empty cart data - The checkout hook fires before cart data is loaded from the Store API

Changes

Mini Cart removal tracking

Implemented a click event listener as a fallback for tracking item removals from the Interactivity API Mini Cart. The implementation uses multiple data sources with graceful fallbacks:

  1. Interactivity API context (data-wp-context attributes)
  2. WooCommerce Blocks store (wp.data.select('wc/store/cart'))
  3. Server-rendered cart data (window.ga4w.data.cart)
  4. DOM extraction (product name, price, quantity from visible elements)

A flag mechanism prevents duplicate events when the original hook does fire (for backwards compatibility with older WooCommerce versions).

Checkout event fallback

Added fallback to server-rendered cart data when the Store API cart data is empty or not yet loaded.

Testing

Requires WooCommerce 10.4.0 Beta 1 or later.

  1. Add a product to cart from the shop page (without page reload)
  2. Open the Mini Cart
  3. Remove the item → remove_from_cart event should fire
  4. Add item again and proceed to checkout → begin_checkout event should include cart items

Note

This is a workaround for missing hook support in WooCommerce's new Interactivity API Mini Cart. Ideally, WooCommerce should fire the experimental__woocommerce_blocks-cart-remove-item action for backwards compatibility.

@NeosinneR
Copy link
Member

@lysyjan is this ready for review or waiting for more work? The CI is misleading, as the required PHP jobs won't start as there are no PHP changes, so you can ignore that (I'll try to take a look at how to configure this properly sometime this week).

@lysyjan
Copy link
Contributor Author

lysyjan commented Dec 9, 2025

@NeosinneR I mentioned it in the Porter report, as I am not sure about the implementation at all. It was more like a quick exploration of the possible solution for why acceptance tests are failing with Woo 10.4+

@NeosinneR
Copy link
Member

@lysyjan does the plugin break in WooCommerce 10.4 without this change? 10.4 is aimed to be released tomorrow, but because the PR is in draft and no one is assigned, I don't see any action needed to be done by anyone. But this is the last week for plugin releases before holiday, so if this is needed, we should review, merge and release it ideally this week. If it's not required, it can wait, but it would be still good to know what are next steps with this PR.

@lysyjan
Copy link
Contributor Author

lysyjan commented Dec 9, 2025

does the plugin break in WooCommerce 10.4 without this change?

I don't think so. Based on the code, it looks like the tracking in the mini cart will stop working.

I haven't found this issue during manual smoke tests. I can take a closer look later today, or you can check it manually.
I have noticed that those e2e tests are not very stable.

@lysyjan lysyjan marked this pull request as ready for review December 19, 2025 12:57
@lysyjan lysyjan closed this Dec 19, 2025
@lysyjan lysyjan reopened this Dec 19, 2025
 - Add click listener fallback for remove_from_cart event tracking
 - Add cart data fallback for begin_checkout event
 - Update tests for cross-version compatibility
@lysyjan lysyjan force-pushed the fix-woocommerce-10-4-compatibility branch from 4889dcb to 1f6f875 Compare December 19, 2025 14:27
Copy link
Member

@eason9487 eason9487 left a comment

Choose a reason for hiding this comment

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

Thanks for the work! I would like to clarify some conditions of logical branches.

In addition, thegetProductFromDOM function may not handle common price formats well.

I haven't further verified whether the data from JSON.parse( document.querySelector( '#wp-script-module-data-\\@wordpress\\/interactivity' ).innerText ) could replace some of the processing, but if not already considered, it might be a potential cart data source.

… lookup

- Remove getProductFromInteractivityContext() which never worked
  (WooCommerce Mini Cart uses data-wp-each binding, not data-wp-context)
- Add parsePriceFromDOM() using accounting.unformat() for proper
  handling of international price formats (e.g., "1.234,56 €")
- Fix hardcoded currency_minor_unit (was always 2, now uses actual
  store settings from cart data or wcSettings.currency.precision)
- Add getWcSettings() helper to reduce duplicate lookups
- Add E2E tests for DOM fallback with US and European price formats
@lysyjan
Copy link
Contributor Author

lysyjan commented Jan 5, 2026

@eason9487 Thank you for your great review.

I added a commit updating parsing prices and added an E2E test. I also removed redundant code and updated comments.

@lysyjan lysyjan assigned eason9487 and unassigned lysyjan Jan 5, 2026
Copy link
Member

@eason9487 eason9487 left a comment

Choose a reason for hiding this comment

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

Thanks for the update!

There are still a few issues to resolve regarding the parsing of the price.

@eason9487 eason9487 assigned lysyjan and unassigned eason9487 Jan 6, 2026
- Add currency settings (decimalSeparator, thousandSeparator, precision)
 to window.ga4w.settings from WooCommerce core functions
- Simplify price parsing in blocks.js to use ga4w.settings.currency
- Remove dependency on wcSettings and heuristic separator detection
- Consolidate E2E tests for DOM fallback price parsing
@lysyjan
Copy link
Contributor Author

lysyjan commented Jan 6, 2026

I didn't know that wcSettings can be undefined and it can depend on the theme.
I decided to change the architecture to avoid missing currency settings and added them as a part of the script from the plugin.

@lysyjan lysyjan removed their assignment Jan 6, 2026
Copy link
Member

@eason9487 eason9487 left a comment

Choose a reason for hiding this comment

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

Thanks for the work! Tested several scenarios and all functioned normally.

@lysyjan lysyjan merged commit bd6f285 into trunk Jan 7, 2026
8 checks passed
@lysyjan lysyjan deleted the fix-woocommerce-10-4-compatibility branch January 7, 2026 07:37
@eason9487 eason9487 added the changelog: fix Took care of something that wasn't working. label Feb 10, 2026
@eason9487 eason9487 mentioned this pull request Feb 10, 2026
20 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changelog: fix Took care of something that wasn't working.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants