-
Notifications
You must be signed in to change notification settings - Fork 851
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
Merged
chihsuan
merged 7 commits into
trunk
from
fix/woocommerce-analytics-product-purchase-order-id-type
Jan 8, 2026
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4227b4f
WooCommerce Analytics: Fix product_purchase event not tracking for sh…
chihsuan 33e5b0b
WooCommerce Analytics: Simplify order processing logic in order_proce…
chihsuan b7d5bf8
Add unit tests for order_process method
chihsuan 9edc034
Update Jetpack plugin composer.lock for woocommerce-analytics changes
chihsuan 0496c58
Exclude test mocks from Phan analysis
chihsuan fc8de7a
Exclude all tests from Phan analysis
chihsuan 293ebbf
Fix misleading test comment
chihsuan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
4 changes: 4 additions & 0 deletions
4
projects/packages/woocommerce-analytics/changelog/fix-product-purchase-order-id-type-check
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| Significance: patch | ||
| Type: fixed | ||
|
|
||
| Fix product_purchase event not tracking for shortcode checkout due to incorrect order ID type check |
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
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
93 changes: 93 additions & 0 deletions
93
projects/packages/woocommerce-analytics/tests/php/Universal_Test.php
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| <?php | ||
| /** | ||
| * Tests for the Universal class. | ||
| * | ||
| * @package automattic/woocommerce-analytics | ||
| */ | ||
|
|
||
| namespace Automattic\Woocommerce_Analytics; | ||
|
|
||
| use WC_Order; | ||
| use WorDBless\BaseTestCase; | ||
|
|
||
| /** | ||
| * Tests for the Universal class. | ||
| */ | ||
| class Universal_Test extends BaseTestCase { | ||
|
|
||
| /** | ||
| * Reset global mocks before each test. | ||
| */ | ||
| public function set_up(): void { | ||
| parent::set_up(); | ||
| global $wc_get_order_calls, $wc_get_order_mock_return; | ||
| $wc_get_order_calls = array(); | ||
| $wc_get_order_mock_return = false; | ||
| } | ||
|
|
||
| /** | ||
| * Test that order_process calls wc_get_order with an integer order ID. | ||
| */ | ||
| public function test_order_process_handles_integer_order_id(): void { | ||
| global $wc_get_order_calls, $wc_get_order_mock_return; | ||
|
|
||
| // Set up mock to return false (order not found). | ||
| $wc_get_order_mock_return = false; | ||
|
|
||
| $universal = new Universal(); | ||
| $universal->order_process( 12345 ); | ||
|
|
||
| $this->assertCount( 1, $wc_get_order_calls, 'wc_get_order should be called once.' ); | ||
| $this->assertSame( 12345, $wc_get_order_calls[0], 'wc_get_order should receive the integer order ID.' ); | ||
| } | ||
|
|
||
| /** | ||
| * Test that order_process calls wc_get_order with a string order ID. | ||
| */ | ||
| public function test_order_process_handles_string_order_id(): void { | ||
| global $wc_get_order_calls, $wc_get_order_mock_return; | ||
|
|
||
| // Set up mock to return false (order not found). | ||
| $wc_get_order_mock_return = false; | ||
|
|
||
| $universal = new Universal(); | ||
| $universal->order_process( '12345' ); | ||
|
|
||
| $this->assertCount( 1, $wc_get_order_calls, 'wc_get_order should be called once.' ); | ||
| $this->assertSame( '12345', $wc_get_order_calls[0], 'wc_get_order should receive the string order ID.' ); | ||
| } | ||
|
|
||
| /** | ||
| * Test that order_process calls wc_get_order with a WC_Order object. | ||
| */ | ||
| public function test_order_process_handles_wc_order_object(): void { | ||
| global $wc_get_order_calls, $wc_get_order_mock_return; | ||
|
|
||
| // Set up mock to return false (order not found). | ||
| $wc_get_order_mock_return = false; | ||
|
|
||
| $order = new WC_Order(); | ||
|
|
||
| $universal = new Universal(); | ||
| $universal->order_process( $order ); | ||
|
|
||
| $this->assertCount( 1, $wc_get_order_calls, 'wc_get_order should be called once.' ); | ||
| $this->assertSame( $order, $wc_get_order_calls[0], 'wc_get_order should receive the WC_Order object.' ); | ||
| } | ||
|
|
||
| /** | ||
| * Test that order_process returns early when wc_get_order returns false. | ||
| */ | ||
| public function test_order_process_returns_early_when_order_not_found(): void { | ||
| global $wc_get_order_mock_return; | ||
|
|
||
| // Set up mock to return false. | ||
| $wc_get_order_mock_return = false; | ||
|
|
||
| $universal = new Universal(); | ||
| $universal->order_process( 12345 ); | ||
|
|
||
| // If we get here without errors, the method completed without processing a non-existent order. | ||
| $this->assertTrue( true, 'order_process should handle a missing order without throwing an exception.' ); | ||
| } | ||
| } |
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
160 changes: 160 additions & 0 deletions
160
projects/packages/woocommerce-analytics/tests/php/mocks/class-wc-order.php
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| <?php | ||
| /** | ||
| * Mock WC_Order class for testing. | ||
| * | ||
| * @package automattic/woocommerce-analytics | ||
| */ | ||
|
|
||
| if ( ! class_exists( 'WC_Order' ) ) { | ||
| /** | ||
| * Mock WC_Order class for testing. | ||
| */ | ||
| class WC_Order { | ||
| /** | ||
| * Order ID. | ||
| * | ||
| * @var int | ||
| */ | ||
| private $id = 123; | ||
|
|
||
| /** | ||
| * Order items. | ||
| * | ||
| * @var array | ||
| */ | ||
| private $items = array(); | ||
|
|
||
| /** | ||
| * Order coupons. | ||
| * | ||
| * @var array | ||
| */ | ||
| private $coupons = array(); | ||
|
|
||
| /** | ||
| * Get order ID. | ||
| * | ||
| * @return int | ||
| */ | ||
| public function get_id() { | ||
| return $this->id; | ||
| } | ||
|
|
||
| /** | ||
| * Get payment method. | ||
| * | ||
| * @return string | ||
| */ | ||
| public function get_payment_method() { | ||
| return 'stripe'; | ||
| } | ||
|
|
||
| /** | ||
| * Get payment method title. | ||
| * | ||
| * @return string | ||
| */ | ||
| public function get_payment_method_title() { | ||
| return 'Credit Card'; | ||
| } | ||
|
|
||
| /** | ||
| * Get user. | ||
| * | ||
| * @return object|false | ||
| */ | ||
| public function get_user() { | ||
| return false; | ||
| } | ||
|
|
||
| /** | ||
| * Get created via. | ||
| * | ||
| * @return string | ||
| */ | ||
| public function get_created_via() { | ||
| return 'checkout'; | ||
| } | ||
|
|
||
| /** | ||
| * Get items. | ||
| * | ||
| * @return array | ||
| */ | ||
| public function get_items() { | ||
| return $this->items; | ||
| } | ||
|
|
||
| /** | ||
| * Set items for testing. | ||
| * | ||
| * @param array $items Items to set. | ||
| */ | ||
| public function set_items( $items ) { | ||
| $this->items = $items; | ||
| } | ||
|
|
||
| /** | ||
| * Get coupons. | ||
| * | ||
| * @return array | ||
| */ | ||
| public function get_coupons() { | ||
| return $this->coupons; | ||
| } | ||
|
|
||
| /** | ||
| * Get order number. | ||
| * | ||
| * @return string | ||
| */ | ||
| public function get_order_number() { | ||
| return '123'; | ||
| } | ||
|
|
||
| /** | ||
| * Get subtotal. | ||
| * | ||
| * @return float | ||
| */ | ||
| public function get_subtotal() { | ||
| return 100.00; | ||
| } | ||
|
|
||
| /** | ||
| * Get total. | ||
| * | ||
| * @return float | ||
| */ | ||
| public function get_total() { | ||
| return 110.00; | ||
| } | ||
|
|
||
| /** | ||
| * Get discount total. | ||
| * | ||
| * @return float | ||
| */ | ||
| public function get_discount_total() { | ||
| return 0.00; | ||
| } | ||
|
|
||
| /** | ||
| * Get total tax. | ||
| * | ||
| * @return float | ||
| */ | ||
| public function get_total_tax() { | ||
| return 10.00; | ||
| } | ||
|
|
||
| /** | ||
| * Get shipping total. | ||
| * | ||
| * @return float | ||
| */ | ||
| public function get_shipping_total() { | ||
| return 5.00; | ||
| } | ||
| } | ||
| } |
15 changes: 15 additions & 0 deletions
15
projects/packages/woocommerce-analytics/tests/php/mocks/class-wc-tracks.php
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| <?php | ||
| /** | ||
| * Mock WC_Tracks class for testing. | ||
| * | ||
| * @package automattic/woocommerce-analytics | ||
| */ | ||
|
|
||
| if ( ! class_exists( 'WC_Tracks' ) ) { | ||
| /** | ||
| * Mock WC_Tracks class for testing. | ||
| */ | ||
| class WC_Tracks { | ||
| // Empty base class for WC_Analytics_Tracking to extend. | ||
| } | ||
| } |
56 changes: 56 additions & 0 deletions
56
projects/packages/woocommerce-analytics/tests/php/mocks/woocommerce-functions.php
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| <?php | ||
| /** | ||
| * WooCommerce function mocks for testing. | ||
| * | ||
| * @package automattic/woocommerce-analytics | ||
| */ | ||
|
|
||
| // phpcs:disable WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid | ||
|
|
||
| /** | ||
| * Global variable to store mock order for testing. | ||
| * | ||
| * @var mixed | ||
| */ | ||
| global $wc_get_order_mock_return; | ||
| $wc_get_order_mock_return = false; | ||
|
|
||
| /** | ||
| * Global variable to track wc_get_order calls. | ||
| * | ||
| * @var array | ||
| */ | ||
| global $wc_get_order_calls; | ||
| $wc_get_order_calls = array(); | ||
|
|
||
| if ( ! function_exists( 'wc_get_order' ) ) { | ||
| /** | ||
| * Mock wc_get_order function. | ||
| * | ||
| * @param mixed $the_order Post object or post ID of the order. | ||
| * @return mixed The mocked return value. | ||
| */ | ||
| function wc_get_order( $the_order = false ) { | ||
| global $wc_get_order_mock_return, $wc_get_order_calls; | ||
| $wc_get_order_calls[] = $the_order; | ||
| return $wc_get_order_mock_return; | ||
| } | ||
| } | ||
|
|
||
| if ( ! function_exists( 'WC' ) ) { | ||
| /** | ||
| * Mock WC function. | ||
| * | ||
| * @return object Mock WooCommerce object. | ||
| */ | ||
| function WC() { | ||
| return new class() { | ||
| /** | ||
| * Session property. | ||
| * | ||
| * @var object|null | ||
| */ | ||
| public $session = null; | ||
| }; | ||
| } | ||
| } |
4 changes: 4 additions & 0 deletions
4
projects/plugins/jetpack/changelog/fix-woocommerce-analytics-product-purchase-order-id-type
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| Significance: patch | ||
| Type: other | ||
|
|
||
| Updated package dependencies. | ||
chihsuan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.