Skip to content

Conversation

@chihsuan
Copy link
Member

@chihsuan chihsuan commented Jan 7, 2026

Fixes WOOA7S-929

Proposed changes:

  • Fix product_purchase event not tracking for shortcode checkout due to incorrect order ID type check

Root Cause Analysis

The order_process() method handles two different hooks:

  • woocommerce_checkout_order_processed (shortcode checkout) - passes integer order ID
  • woocommerce_store_api_checkout_order_processed (block checkout) - passes WC_Order object

The code was checking:

if ( is_string( $order_id_or_order ) ) {
    $order = wc_get_order( $order_id_or_order );
} else {
    $order = $order_id_or_order;
}

When the shortcode checkout hook fires with an integer (e.g., 12345), is_string(12345) returns false, so the code takes the else branch and treats the integer as the order object. This then fails the instanceof WC_Order check 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:

$order = wc_get_order( $order_id_or_order );

if ( ! $order ) {
    return;
}

Other information:

  • Have you written new tests for your changes, if applicable?
  • Have you checked the E2E test CI results, and verified that your changes do not break them?
  • Have you tested your changes on WordPress.com, if applicable (if so, you'll see a generated comment below with a script to run)?

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:

  1. Set up a WooCommerce store with Jetpack connected and WooCommerce Analytics enabled
  2. Add the following code snippet to your theme's functions.php or use a code snippet plugin:
add_filter( 'jetpack_woocommerce_analytics_event_props', function( $event_properties, $event_name ) {
    error_log( 'Tracks event: ' . $event_name . ' ' . print_r( $event_properties, true ) );
    return $event_properties;
}, 10, 2 );
  1. Open a terminal and navigate to your WordPress site root directory. Run tail -f wp-content/debug.log to monitor debug logs
  2. Complete a checkout using the shortcode checkout (not block checkout)
  3. Verify that the product_purchase event appears in the debug log

Before this fix: No product_purchase event would be logged for shortcode checkout
After this fix: The product_purchase event is properly tracked

🤖 Generated with Claude Code

…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]>
Copilot AI review requested due to automatic review settings January 7, 2026 08:58
@github-actions
Copy link
Contributor

github-actions bot commented Jan 7, 2026

Are you an Automattician? Please test your changes on all WordPress.com environments to help mitigate accidental explosions.

  • To test on WoA, go to the Plugins menu on a WoA dev site. Click on the "Upload" button and follow the upgrade flow to be able to upload, install, and activate the Jetpack Beta plugin. Once the plugin is active, go to Jetpack > Jetpack Beta, select your plugin (Jetpack), and enable the fix/woocommerce-analytics-product-purchase-order-id-type branch.
  • To test on Simple, run the following command on your sandbox:
bin/jetpack-downloader test jetpack fix/woocommerce-analytics-product-purchase-order-id-type

Interested in more tips and information?

  • In your local development environment, use the jetpack rsync command to sync your changes to a WoA dev blog.
  • Read more about our development workflow here: PCYsg-eg0-p2
  • Figure out when your changes will be shipped to customers here: PCYsg-eg5-p2

@github-actions
Copy link
Contributor

github-actions bot commented Jan 7, 2026

Thank you for your PR!

When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:

  • ✅ Include a description of your PR changes.
  • ✅ Add a "[Status]" label (In Progress, Needs Review, ...).
  • ✅ Add a "[Type]" label (Bug, Enhancement, Janitorial, Task).
  • ✅ Add testing instructions.
  • ✅ Specify whether this PR includes any changes to data or privacy.
  • ✅ Add changelog entries to affected projects

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:

  1. Ensure all required checks appearing at the bottom of this PR are passing.
  2. Make sure to test your changes on all platforms that it applies to. You're responsible for the quality of the code you ship.
  3. You can use GitHub's Reviewers functionality to request a review.
  4. When it's reviewed and merged, you will be pinged in Slack to deploy the changes to WordPress.com simple once the build is done.

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.

@github-actions github-actions bot added the [Status] Needs Author Reply We need more details from you. This label will be auto-added until the PR meets all requirements. label Jan 7, 2026
Copy link
Contributor

Copilot AI left a 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() from is_string() to is_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.

@jp-launch-control
Copy link

jp-launch-control bot commented Jan 7, 2026

Code Coverage Summary

Coverage changed in 2 files.

File Coverage Δ% Δ Uncovered
projects/packages/woocommerce-analytics/src/class-woo-analytics-trait.php 0/267 (0.00%) 0.00% -1 💚
projects/packages/woocommerce-analytics/src/class-universal.php 3/360 (0.83%) 0.83% -11 💚

Full summary · PHP report · JS report

@chihsuan chihsuan added [Type] Bug When a feature is broken and / or not performing as intended [Status] Needs Review This PR is ready for review. and removed [Status] Needs Author Reply We need more details from you. This label will be auto-added until the PR meets all requirements. labels Jan 7, 2026
@chihsuan chihsuan requested review from a team January 7, 2026 09:24
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]>
Copilot AI review requested due to automatic review settings January 7, 2026 09:30
Copy link
Contributor

Copilot AI left a 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.

@github-actions github-actions bot added the [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ label Jan 7, 2026
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]>
Copilot AI review requested due to automatic review settings January 7, 2026 11:38
Copy link
Contributor

Copilot AI left a 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.

chihsuan and others added 2 commits January 7, 2026 19:50
🤖 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]>
Copilot AI review requested due to automatic review settings January 7, 2026 12:02
Copy link
Contributor

Copilot AI left a 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.

Copy link
Contributor

@kangzj kangzj left a 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!

@chihsuan chihsuan merged commit ad40de6 into trunk Jan 8, 2026
77 checks passed
@chihsuan chihsuan deleted the fix/woocommerce-analytics-product-purchase-order-id-type branch January 8, 2026 03:16
@github-actions github-actions bot removed the [Status] Needs Review This PR is ready for review. label Jan 8, 2026
@github-actions github-actions bot added this to the jetpack/15.5 milestone Jan 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Feature] WooCommerce Analytics [Package] WooCommerce Analytics Enhanced analytics for WooCommerce users [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ [Tests] Includes Tests [Type] Bug When a feature is broken and / or not performing as intended

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants