-
Notifications
You must be signed in to change notification settings - Fork 844
WooCommerce Analytics: Fix product_purchase event not tracking for shortcode checkout #46467
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WooCommerce Analytics: Fix product_purchase event not tracking for shortcode checkout #46467
Conversation
…ortcode checkout The `woocommerce_checkout_order_processed` hook passes an integer order ID, but `order_process()` was checking `is_string()` which returns false for integers. This caused the code to treat the integer as an order object, failing the `instanceof WC_Order` check and silently dropping events. Changed to `is_numeric()` to properly handle both integer and string order IDs. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <[email protected]>
|
Are you an Automattician? Please test your changes on all WordPress.com environments to help mitigate accidental explosions.
Interested in more tips and information?
|
|
Thank you for your PR! When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:
This comment will be updated as you work on your PR and make changes. If you think that some of those checks are not needed for your PR, please explain why you think so. Thanks for cooperation 🤖 Follow this PR Review Process:
If you have questions about anything, reach out in #jetpack-developers for guidance! Jetpack plugin: No scheduled milestone found for this plugin. If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR fixes a bug where the product_purchase analytics event was not being tracked for WooCommerce shortcode checkout. The issue stemmed from an incorrect type check that expected a string order ID but received an integer from the woocommerce_checkout_order_processed hook.
Key changes:
- Changed the type check in
order_process()fromis_string()tois_numeric()to handle both integer and string order IDs - Updated the PHPDoc to reflect that the parameter can be
int|string|WC_Order
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
projects/packages/woocommerce-analytics/src/class-universal.php |
Fixed type check to use is_numeric() instead of is_string() and updated PHPDoc to include int type |
projects/packages/woocommerce-analytics/changelog/fix-product-purchase-order-id-type-check |
Added changelog entry documenting the bug fix |
The fix is correct and well-implemented. The change from is_string() to is_numeric() properly handles order IDs passed as either integers (from woocommerce_checkout_order_processed) or strings, while still allowing WC_Order objects to fall through to the else branch. The subsequent instanceof WC_Order check ensures that invalid inputs are safely rejected.
Code Coverage SummaryCoverage changed in 2 files.
|
Tests verify that order_process correctly handles different input types: - Integer order ID (from woocommerce_checkout_order_processed hook) - String order ID - WC_Order object (from woocommerce_store_api_checkout_order_processed hook) Also includes WooCommerce mock classes and functions for isolated testing. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
Copilot reviewed 8 out of 8 changed files in this pull request and generated 1 comment.
projects/packages/woocommerce-analytics/tests/php/Universal_Test.php
Outdated
Show resolved
Hide resolved
🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <[email protected]>
Add exclude_file_regex for tests/php/mocks/ directory to avoid PhanRedefinedClassReference errors from mock WooCommerce classes. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
Copilot reviewed 10 out of 11 changed files in this pull request and generated 3 comments.
projects/plugins/jetpack/changelog/fix-woocommerce-analytics-product-purchase-order-id-type
Show resolved
Hide resolved
projects/plugins/jetpack/changelog/fix-woocommerce-analytics-product-purchase-order-id-type
Show resolved
Hide resolved
🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <[email protected]>
Remove reference to mocking record_event since the test doesn't actually verify that. Update assertion message to accurately describe what the test validates. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
Copilot reviewed 10 out of 11 changed files in this pull request and generated no new comments.
kangzj
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It works as described and nice work adding more tests to cover the cases here!
Fixes WOOA7S-929
Proposed changes:
product_purchaseevent not tracking for shortcode checkout due to incorrect order ID type checkRoot Cause Analysis
The
order_process()method handles two different hooks:woocommerce_checkout_order_processed(shortcode checkout) - passes integer order IDwoocommerce_store_api_checkout_order_processed(block checkout) - passes WC_Order objectThe code was checking:
When the shortcode checkout hook fires with an integer (e.g.,
12345),is_string(12345)returnsfalse, so the code takes the else branch and treats the integer as the order object. This then fails theinstanceof WC_Ordercheck and the function returns early, silently dropping all product_purchase events for shortcode checkout.Fix
Simplified the code by directly using
wc_get_order()which already handles int, string, and WC_Order inputs:Other information:
Jetpack product discussion
N/A
Does this pull request change what data or activity we track or use?
No - this fixes a bug where events were being silently dropped.
Testing instructions:
functions.phpor use a code snippet plugin:tail -f wp-content/debug.logto monitor debug logsproduct_purchaseevent appears in the debug logBefore this fix: No
product_purchaseevent would be logged for shortcode checkoutAfter this fix: The
product_purchaseevent is properly tracked🤖 Generated with Claude Code