Commit 4b342e2e9db for woocommerce

commit 4b342e2e9dbcd4fe077c9770a23e07bac6ca580a
Author: Chris Lilitsas <1105590+xristos3490@users.noreply.github.com>
Date:   Wed Jul 15 12:53:08 2026 +0300

    Fix product_import_complete Tracks event never firing (#66556)

    * fix: fire product_import_complete Tracks event by checking _wpnonce

    * fix: scope phpcs suppressions to single lines in importer tracking tests

    * fix: verify importer completion nonce instead of checking presence

    * revert: defer importer nonce verification to a dedicated follow-up PR

diff --git a/plugins/woocommerce/changelog/41512-fix-product-import-complete-tracks-event b/plugins/woocommerce/changelog/41512-fix-product-import-complete-tracks-event
new file mode 100644
index 00000000000..fa8a591a93a
--- /dev/null
+++ b/plugins/woocommerce/changelog/41512-fix-product-import-complete-tracks-event
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Fix the product_import_complete Tracks event never firing by checking for the actual `_wpnonce` request parameter present on the importer's done step instead of a non-existent `nonce` parameter.
diff --git a/plugins/woocommerce/includes/tracks/events/class-wc-importer-tracking.php b/plugins/woocommerce/includes/tracks/events/class-wc-importer-tracking.php
index fbffe91b4ba..8cd852a1569 100644
--- a/plugins/woocommerce/includes/tracks/events/class-wc-importer-tracking.php
+++ b/plugins/woocommerce/includes/tracks/events/class-wc-importer-tracking.php
@@ -66,7 +66,7 @@ class WC_Importer_Tracking {
 	 */
 	public function track_product_importer_complete() {
 		// phpcs:disable WordPress.Security.NonceVerification.Recommended
-		if ( ! isset( $_REQUEST['nonce'] ) ) {
+		if ( ! isset( $_REQUEST['_wpnonce'] ) ) {
 			return;
 		}

diff --git a/plugins/woocommerce/tests/php/includes/class-wc-importer-tracking-test.php b/plugins/woocommerce/tests/php/includes/class-wc-importer-tracking-test.php
index dca06944fd4..7c79e63574a 100644
--- a/plugins/woocommerce/tests/php/includes/class-wc-importer-tracking-test.php
+++ b/plugins/woocommerce/tests/php/includes/class-wc-importer-tracking-test.php
@@ -26,17 +26,68 @@ class WC_Importer_Tracking_Test extends \WC_Unit_Test_Case {
 	 */
 	public function tearDown(): void {
 		update_option( 'woocommerce_allow_tracking', 'no' );
+		unset(
+			$_REQUEST['step'],
+			$_REQUEST['_wpnonce'],
+			$_REQUEST['nonce'],
+			$_GET['products-imported'],
+			$_GET['products-imported-variations'],
+			$_GET['products-updated'],
+			$_GET['products-failed'],
+			$_GET['products-skipped']
+		);
 		parent::tearDown();
 	}

 	/**
-	 * Test wcadmin_product_import_complete Tracks event
+	 * The completion Tracks event should fire on the done step, which the
+	 * product importer reaches via a URL carrying the `_wpnonce` query arg.
 	 */
-	public function test_import_complete() {
-		$_REQUEST['step']  = 'done';
-		$_REQUEST['nonce'] = 'nonce';
-		/* phpcs:disable WooCommerce.Commenting.CommentHooks.MissingHookComment */
+	public function test_import_complete_is_recorded_with_wpnonce() {
+		$_REQUEST['step']     = 'done';
+		$_REQUEST['_wpnonce'] = wp_create_nonce( 'woocommerce-csv-importer' );
+
+		// phpcs:ignore WooCommerce.Commenting.CommentHooks.MissingHookComment
 		do_action( 'product_page_product_importer' );
+
 		$this->assertRecordedTracksEvent( 'wcadmin_product_import_complete' );
 	}
+
+	/**
+	 * The completion Tracks event should record the import result counts taken
+	 * from the query args appended to the done URL.
+	 */
+	public function test_import_complete_records_result_counts() {
+		$_REQUEST['step']                     = 'done';
+		$_REQUEST['_wpnonce']                 = wp_create_nonce( 'woocommerce-csv-importer' );
+		$_GET['products-imported']            = '5';
+		$_GET['products-imported-variations'] = '3';
+		$_GET['products-updated']             = '2';
+		$_GET['products-failed']              = '1';
+		$_GET['products-skipped']             = '4';
+
+		// phpcs:ignore WooCommerce.Commenting.CommentHooks.MissingHookComment
+		do_action( 'product_page_product_importer' );
+
+		$events = $this->get_tracks_events( 'wcadmin_product_import_complete' );
+		$this->assertNotEmpty( $events );
+		$event = end( $events );
+		$this->assertEquals( 5, $event->imported );
+		$this->assertEquals( 3, $event->imported_variations );
+		$this->assertEquals( 2, $event->updated );
+		$this->assertEquals( 1, $event->failed );
+		$this->assertEquals( 4, $event->skipped );
+	}
+
+	/**
+	 * Without a nonce on the request the completion event must not fire.
+	 */
+	public function test_import_complete_not_recorded_without_nonce() {
+		$_REQUEST['step'] = 'done';
+
+		// phpcs:ignore WooCommerce.Commenting.CommentHooks.MissingHookComment
+		do_action( 'product_page_product_importer' );
+
+		$this->assertNotRecordedTracksEvent( 'wcadmin_product_import_complete' );
+	}
 }