Honor WooCommerce currency decimals in conversion events.#13141
Open
faisalahammad wants to merge 1 commit into
Open
Honor WooCommerce currency decimals in conversion events.#13141faisalahammad wants to merge 1 commit into
faisalahammad wants to merge 1 commit into
Conversation
The JS helper that converts the integer minor-unit price back into a decimal string was hard-coded to divide by 100. When a store set the WooCommerce 'Number of decimals' option to 4 (or 0, or 3), the value pushed to GA4 for add_to_cart and purchase events was off by 100x (or 10x, etc). Read wc_get_price_decimals() on the PHP side and expose it on the window._googlesitekit.wcdata global. Thread the value through the formatPrice helper on the JS side so both the top-level value and per-item price use the correct minor-unit count. Fixes google#12986
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
WooCommerce
add_to_cartandpurchaseevents sent to GA4 now reflect the store's configured currency precision. Previously the JS helper that converted the integer minor-unit price back into a decimal value was hard-coded to divide by 100, so any store that set WooCommerce → Settings → General → Currency options → Number of decimals to anything other than 2 (e.g. 4 for BHD, 0 for JPY, 3 for KWD/OMR/TND) saw the wrong value arrive in GA4.Fixes #12986
Changes
includes/Core/Conversion_Tracking/Conversion_Event_Providers/WooCommerce.phpBefore: the
wp_footerandmaybe_add_purchase_inline_scriptinline scripts emitted thewcdataglobal with currency, products, add_to_cart, purchase, and eventsToTrack fields only. The decimal conversion happened downstream in JS, which had no way to read the store's minor-unit count.After: both inline scripts also emit
window._googlesitekit.wcdata.currency_minor_unit = <absint wc_get_price_decimals()>;so JS can recover the correct decimal precision.assets/js/event-providers/woocommerce.jsBefore:
formatPricedefaulted tocurrencyMinorUnit = 2and the value was hard-coded at every call site.After:
formatPricekeeps= 2as a defensive fallback but is now always called with the value read from thewcdataglobal. The newglobalCurrencyMinorUnitis destructured alongsideglobalCurrencyand passed intoformatEventData/formatProductData, which in turn pass it toformatPricefor both the top-levelvalueand eachitems[].price.Testing
Test 1: PHP price formatter honors the decimals option
composer test -- --filter WooCommerceTest::test_get_formatted_price_returns_correct_minor_unitswoocommerce_price_decimalsto 0, 2, and 4 and verifiesget_formatted_price()produces the expected integer minor-unit count (e.g. 1.2345 with decimals=4 yields 12345, 500 with decimals=0 yields 500).Result: 6 dataset cases pass.
Test 2: inline script exposes the decimals value to JS
composer test -- --filter WooCommerceTest::test_inline_script_emits_currency_minor_unitwp_footer, and asserts the rendered output containswcdata.currency_minor_unit = 4;after settingwoocommerce_price_decimals = 4.Result: passes.
Test 3: JS event provider formats values per currency decimals
BABEL_ENV=test NODE_ENV=test node_modules/.bin/jest --config tests/js/jest.config.js --rootDir . --passWithNoTests assets/js/event-providers/woocommerce.test.jsadd_to_cartandpurchaseevents, plus the fallback whencurrency_minor_unitis missing fromwcdata.Result: 8 tests pass.
Test 4: lint clean
composer lint -- includes/Core/Conversion_Tracking/Conversion_Event_Providers/WooCommerce.php tests/phpunit/integration/Core/Conversion_Tracking/Conversion_Event_Providers/WooCommerceTest.php tests/phpunit/includes/wc-functions-stub.phpnode_modules/.bin/eslint --no-eslintrc --config .eslintrc.json --ignore-path .eslintignore assets/js/event-providers/woocommerce.js assets/js/event-providers/woocommerce.test.jsResult: clean.
Manual reproduction (before this fix)
Number of decimalsto 4 under WooCommerce → Settings → General → Currency options.dataLayershowsvalue: 123.45anditems[0].price: 123.45. After the fix: both show1.2345.