Commit 6952cdc5148 for woocommerce

commit 6952cdc5148a54d2df8fb3af7c2392ac4ab385b7
Author: Anand Rajaram <anandrajaram21@gmail.com>
Date:   Wed Nov 18 01:45:09 2026 +0530

     Fix stock notification email management links for registered customers (#69702)

    * Fix stock notification management links for registered customers

    * Add coverage for stock notification email management links

    * Add changelog entry for stock notification email links

    * Fix stock notification links when account endpoint is disabled

    * Strengthen stock notification email link assertions

    * Add management links to plain-text stock notification emails

    * test: save notification in footer link test to persist unsubscribe key

    * chore: bump stock notification email templates to @version 11.4.0

    ---------

    Co-authored-by: Chris Lilitsas <1105590+xristos3490@users.noreply.github.com>

diff --git a/plugins/woocommerce/changelog/fix-stock-notification-email-account-link b/plugins/woocommerce/changelog/fix-stock-notification-email-account-link
new file mode 100644
index 00000000000..56ae3d75522
--- /dev/null
+++ b/plugins/woocommerce/changelog/fix-stock-notification-email-account-link
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Open account management instead of unsubscribing registered customers from stock notification email links.
diff --git a/plugins/woocommerce/src/Internal/StockNotifications/Emails/CustomerStockNotificationEmail.php b/plugins/woocommerce/src/Internal/StockNotifications/Emails/CustomerStockNotificationEmail.php
index 3e937fafbdf..d616ac02af6 100644
--- a/plugins/woocommerce/src/Internal/StockNotifications/Emails/CustomerStockNotificationEmail.php
+++ b/plugins/woocommerce/src/Internal/StockNotifications/Emails/CustomerStockNotificationEmail.php
@@ -6,6 +6,7 @@ namespace Automattic\WooCommerce\Internal\StockNotifications\Emails;

 use Automattic\WooCommerce\Internal\StockNotifications\Notification;
 use Automattic\WooCommerce\Internal\StockNotifications\Factory;
+use Automattic\WooCommerce\Internal\StockNotifications\Frontend\MyAccountEndpoint;
 use Automattic\WooCommerce\Internal\StockNotifications\Utilities\UtmHelper;
 use WC_Email;

@@ -177,12 +178,17 @@ class CustomerStockNotificationEmail extends WC_Email {
 			$product
 		);

-		$user     = $notification instanceof Notification && $notification->get_user_id() ? get_user_by( 'id', $notification->get_user_id() ) : false;
-		$is_guest = ! is_a( $user, 'WP_User' );
+		$user         = $notification instanceof Notification && $notification->get_user_id() ? get_user_by( 'id', $notification->get_user_id() ) : false;
+		$is_guest     = ! is_a( $user, 'WP_User' );
+		$account_link = '';
+		if ( ! $is_guest && '' !== MyAccountEndpoint::get_endpoint_slug() ) {
+			$account_link = UtmHelper::add_email_utm_params( MyAccountEndpoint::get_endpoint_url() );
+		}

 		return array(
 			'button_text'      => $button_text,
 			'button_link'      => $button_link,
+			'account_link'     => $account_link,
 			'unsubscribe_link' => UtmHelper::add_email_utm_params(
 				add_query_arg(
 					array(
diff --git a/plugins/woocommerce/src/Internal/StockNotifications/Emails/CustomerStockNotificationVerifiedEmail.php b/plugins/woocommerce/src/Internal/StockNotifications/Emails/CustomerStockNotificationVerifiedEmail.php
index baa4cc94ad4..5e0bf9aadc6 100644
--- a/plugins/woocommerce/src/Internal/StockNotifications/Emails/CustomerStockNotificationVerifiedEmail.php
+++ b/plugins/woocommerce/src/Internal/StockNotifications/Emails/CustomerStockNotificationVerifiedEmail.php
@@ -6,6 +6,7 @@ namespace Automattic\WooCommerce\Internal\StockNotifications\Emails;

 use Automattic\WooCommerce\Internal\StockNotifications\Notification;
 use Automattic\WooCommerce\Internal\StockNotifications\Factory;
+use Automattic\WooCommerce\Internal\StockNotifications\Frontend\MyAccountEndpoint;
 use Automattic\WooCommerce\Internal\StockNotifications\Utilities\UtmHelper;
 use WC_Email;

@@ -145,9 +146,14 @@ class CustomerStockNotificationVerifiedEmail extends WC_Email {
 		$unsubscribe_key = $notification->get_unsubscribe_key( true );
 		$user            = $notification instanceof Notification && $notification->get_user_id() ? get_user_by( 'id', $notification->get_user_id() ) : false;
 		$is_guest        = ! is_a( $user, 'WP_User' );
+		$account_link    = '';
+		if ( ! $is_guest && '' !== MyAccountEndpoint::get_endpoint_slug() ) {
+			$account_link = UtmHelper::add_email_utm_params( MyAccountEndpoint::get_endpoint_url() );
+		}

 		return array(
 			'is_guest'         => $is_guest,
+			'account_link'     => $account_link,
 			'unsubscribe_link' => UtmHelper::add_email_utm_params(
 				add_query_arg(
 					array(
diff --git a/plugins/woocommerce/templates/emails/customer-stock-notification-verified.php b/plugins/woocommerce/templates/emails/customer-stock-notification-verified.php
index 202cb47d65e..9615d60bd38 100644
--- a/plugins/woocommerce/templates/emails/customer-stock-notification-verified.php
+++ b/plugins/woocommerce/templates/emails/customer-stock-notification-verified.php
@@ -12,7 +12,7 @@
  *
  * @see https://woocommerce.com/document/template-structure/
  * @package WooCommerce\Templates\Emails
- * @version 11.3.0
+ * @version 11.4.0
  */

 // Exit if accessed directly.
@@ -58,11 +58,12 @@ do_action( 'woocommerce_email_header', $email_heading, $email );
 		esc_html_e( 'You have received this message because your e-mail address was used to sign up for stock notifications on our store.', 'woocommerce' );
 		echo ' ';

-		if ( ! $is_guest ) {
-			// translators: %1$s placeholder is the unsubscribe link, %2$s placeholder is the Unsubscribe text link.
-			$unsubscribe_link_tag = sprintf( '<a href="%1$s" id="notification__unsubscribe_link">%2$s</a>', esc_url( $unsubscribe_link ), _x( 'click here', 'unsubscribe cta for stock notifications for existing customers', 'woocommerce' ) );
+		// Retain the legacy link ID for email styles and template overrides.
+		if ( ! $is_guest && $account_link ) {
+			// translators: %1$s placeholder is the account link, %2$s placeholder is the link text.
+			$account_link_tag = sprintf( '<a href="%1$s" id="notification__unsubscribe_link">%2$s</a>', esc_url( $account_link ), _x( 'click here', 'account management cta for stock notifications for existing customers', 'woocommerce' ) );
 			// translators: %s placeholder is the text part from above.
-			echo wp_kses_post( sprintf( __( 'To manage your notifications, %s to log in to your account.', 'woocommerce' ), $unsubscribe_link_tag ) );
+			echo wp_kses_post( sprintf( __( 'To manage your notifications, %s to log in to your account.', 'woocommerce' ), $account_link_tag ) );
 		} else {
 			// translators: %1$s placeholder is the unsubscribe link, %2$s placeholder is the Unsubscribe text link.
 			$unsubscribe_link_tag = sprintf( '<a href="%1$s" id="notification__unsubscribe_link">%2$s</a>', esc_url( $unsubscribe_link ), _x( 'click here', 'unsubscribe cta for stock notifications for guests', 'woocommerce' ) );
diff --git a/plugins/woocommerce/templates/emails/customer-stock-notification.php b/plugins/woocommerce/templates/emails/customer-stock-notification.php
index a97c061c2ba..6c8254d8908 100644
--- a/plugins/woocommerce/templates/emails/customer-stock-notification.php
+++ b/plugins/woocommerce/templates/emails/customer-stock-notification.php
@@ -12,7 +12,7 @@
  *
  * @see https://woocommerce.com/document/template-structure/
  * @package WooCommerce\Templates\Emails
- * @version 11.3.0
+ * @version 11.4.0
  */

 // Exit if accessed directly.
@@ -63,11 +63,12 @@ do_action( 'woocommerce_email_header', $email_heading, $email );
 		echo esc_html( __( 'You have received this message because your e-mail address was used to sign up for stock notifications on our store.', 'woocommerce' ) );
 		echo ' ';

-		if ( ! $is_guest ) {
-			// translators: %1$s placeholder is the unsubscribe link, %2$s placeholder is the Unsubscribe text link.
-			$unsubscribe_link_tag = sprintf( '<a href="%1$s" id="notification__unsubscribe_link">%2$s</a>', esc_url( $unsubscribe_link ), _x( 'click here', 'unsubscribe cta for stock notifications for existing customers', 'woocommerce' ) );
+		// Retain the legacy link ID for email styles and template overrides.
+		if ( ! $is_guest && $account_link ) {
+			// translators: %1$s placeholder is the account link, %2$s placeholder is the link text.
+			$account_link_tag = sprintf( '<a href="%1$s" id="notification__unsubscribe_link">%2$s</a>', esc_url( $account_link ), _x( 'click here', 'account management cta for stock notifications for existing customers', 'woocommerce' ) );
 			// translators: %s placeholder is the text part from above.
-			echo wp_kses_post( sprintf( __( 'To manage your notifications, %s to log in to your account.', 'woocommerce' ), $unsubscribe_link_tag ) );
+			echo wp_kses_post( sprintf( __( 'To manage your notifications, %s to log in to your account.', 'woocommerce' ), $account_link_tag ) );
 		} else {
 			// translators: %1$s placeholder is the unsubscribe link, %2$s placeholder is the Unsubscribe text link.
 			$unsubscribe_link_tag = sprintf( '<a href="%1$s" id="notification__unsubscribe_link">%2$s</a>', esc_url( $unsubscribe_link ), _x( 'click here', 'unsubscribe cta for stock notifications for guests', 'woocommerce' ) );
diff --git a/plugins/woocommerce/templates/emails/plain/customer-stock-notification-verified.php b/plugins/woocommerce/templates/emails/plain/customer-stock-notification-verified.php
index 6132a99803d..1cf122abd65 100644
--- a/plugins/woocommerce/templates/emails/plain/customer-stock-notification-verified.php
+++ b/plugins/woocommerce/templates/emails/plain/customer-stock-notification-verified.php
@@ -12,7 +12,7 @@
  *
  * @see https://woocommerce.com/document/template-structure/
  * @package WooCommerce\Templates\Emails\Plain
- * @version 10.2.0
+ * @version 11.4.0
  */

 // Exit if accessed directly.
@@ -36,6 +36,16 @@ echo "\n\n----------------------------------------\n\n";
  */
 do_action( 'woocommerce_email_stock_notification_product', $product, $notification, true, $email );

+echo "\n\n";
+if ( ! $is_guest && $account_link ) {
+	// translators: %s: Account management URL.
+	printf( esc_html__( 'To manage your notifications, log in to your account: %s', 'woocommerce' ), esc_url( $account_link ) );
+} else {
+	// translators: %s: Unsubscribe URL.
+	printf( esc_html__( 'To stop receiving these messages, unsubscribe: %s', 'woocommerce' ), esc_url( $unsubscribe_link ) );
+}
+echo "\n\n----------------------------------------\n\n";
+
 /**
  * Show user-defined additional content - this is set in each email's settings.
  */
diff --git a/plugins/woocommerce/templates/emails/plain/customer-stock-notification.php b/plugins/woocommerce/templates/emails/plain/customer-stock-notification.php
index f9020f97959..3802abf04cf 100644
--- a/plugins/woocommerce/templates/emails/plain/customer-stock-notification.php
+++ b/plugins/woocommerce/templates/emails/plain/customer-stock-notification.php
@@ -12,7 +12,7 @@
  *
  * @see https://woocommerce.com/document/template-structure/
  * @package WooCommerce\Templates\Emails\Plain
- * @version 10.2.0
+ * @version 11.4.0
  */

 // Exit if accessed directly.
@@ -36,6 +36,16 @@ echo "\n\n----------------------------------------\n\n";
  */
 do_action( 'woocommerce_email_stock_notification_product', $product, $notification, true, $email );

+echo "\n\n";
+if ( ! $is_guest && $account_link ) {
+	// translators: %s: Account management URL.
+	printf( esc_html__( 'To manage your notifications, log in to your account: %s', 'woocommerce' ), esc_url( $account_link ) );
+} else {
+	// translators: %s: Unsubscribe URL.
+	printf( esc_html__( 'To stop receiving these messages, unsubscribe: %s', 'woocommerce' ), esc_url( $unsubscribe_link ) );
+}
+echo "\n\n----------------------------------------\n\n";
+
 /**
  * Show user-defined additional content - this is set in each email's settings.
  */
diff --git a/plugins/woocommerce/tests/e2e/tests/back-in-stock-notifications/receiving-confirmations.spec.ts b/plugins/woocommerce/tests/e2e/tests/back-in-stock-notifications/receiving-confirmations.spec.ts
index a75727dd2ac..74a094859ae 100644
--- a/plugins/woocommerce/tests/e2e/tests/back-in-stock-notifications/receiving-confirmations.spec.ts
+++ b/plugins/woocommerce/tests/e2e/tests/back-in-stock-notifications/receiving-confirmations.spec.ts
@@ -235,12 +235,18 @@ test.describe(
 			await expect( body ).toContainText( BIS_EMAIL_FOOTER.loggedIn );
 			await expect( body ).not.toContainText( BIS_EMAIL_FOOTER.guest );

-			// The wording changes; the link it wraps still has to be there.
-			await expect(
-				bisEmailBody( page ).locator(
-					`a${ BIS_EMAIL_LINKS.unsubscribe }`
-				)
-			).toHaveAttribute( 'href', /email_link_action=unsubscribe/ );
+			// Account management must not cancel the notification.
+			const accountLink = bisEmailBody( page ).locator(
+				`a${ BIS_EMAIL_LINKS.unsubscribe }`
+			);
+			await expect( accountLink ).toHaveAttribute(
+				'href',
+				/\/my-account\/stock-notifications\//
+			);
+			await expect( accountLink ).not.toHaveAttribute(
+				'href',
+				/email_link_action=unsubscribe/
+			);
 		} );

 		test( 'a verify link with a tampered key leaves the signup pending', async ( {
diff --git a/plugins/woocommerce/tests/e2e/tests/back-in-stock-notifications/receiving-notifications.spec.ts b/plugins/woocommerce/tests/e2e/tests/back-in-stock-notifications/receiving-notifications.spec.ts
index 6500a0d1c75..21d82e0288d 100644
--- a/plugins/woocommerce/tests/e2e/tests/back-in-stock-notifications/receiving-notifications.spec.ts
+++ b/plugins/woocommerce/tests/e2e/tests/back-in-stock-notifications/receiving-notifications.spec.ts
@@ -135,12 +135,18 @@ test.describe(
 			await expect( body ).toContainText( BIS_EMAIL_FOOTER.loggedIn );
 			await expect( body ).not.toContainText( BIS_EMAIL_FOOTER.guest );

-			// The wording changes; the link it wraps still has to be there.
-			await expect(
-				bisEmailBody( page ).locator(
-					`a${ BIS_EMAIL_LINKS.unsubscribe }`
-				)
-			).toHaveAttribute( 'href', /email_link_action=unsubscribe/ );
+			// Account management must not cancel the notification.
+			const accountLink = bisEmailBody( page ).locator(
+				`a${ BIS_EMAIL_LINKS.unsubscribe }`
+			);
+			await expect( accountLink ).toHaveAttribute(
+				'href',
+				/\/my-account\/stock-notifications\//
+			);
+			await expect( accountLink ).not.toHaveAttribute(
+				'href',
+				/email_link_action=unsubscribe/
+			);
 		} );

 		test( 'unsubscribe link in the back-in-stock email leaves the sent notification unchanged', async ( {
diff --git a/plugins/woocommerce/tests/php/src/Internal/StockNotifications/Emails/EmailManagerTests.php b/plugins/woocommerce/tests/php/src/Internal/StockNotifications/Emails/EmailManagerTests.php
index a99d882ee8f..61995a39624 100644
--- a/plugins/woocommerce/tests/php/src/Internal/StockNotifications/Emails/EmailManagerTests.php
+++ b/plugins/woocommerce/tests/php/src/Internal/StockNotifications/Emails/EmailManagerTests.php
@@ -5,6 +5,7 @@ namespace Automattic\WooCommerce\Tests\Internal\StockNotifications\Emails;

 use Automattic\WooCommerce\Internal\StockNotifications\Emails\EmailManager;
 use Automattic\WooCommerce\Internal\StockNotifications\Notification;
+use Automattic\WooCommerce\Internal\StockNotifications\Frontend\MyAccountEndpoint;
 use Automattic\WooCommerce\Internal\StockNotifications\Enums\NotificationStatus;
 use Automattic\WooCommerce\Tests\Internal\StockNotifications\StockNotificationsFeatureTrait;
 use WC_Helper_Product;
@@ -78,6 +79,7 @@ class EmailManagerTests extends \WC_Unit_Test_Case {
 		// whatever filters are active for them, rather than inheriting this test's list.
 		$this->reset_email_singleton();
 		parent::tearDown();
+		WC()->query->init_query_vars();
 	}

 	/**
@@ -356,6 +358,71 @@ class EmailManagerTests extends \WC_Unit_Test_Case {
 		);
 	}

+	/**
+	 * @testdox Should link registered recipients to account management and preserve guest unsubscribe links.
+	 * @testWith [false, false, "stock-notifications"]
+	 *           [true, false, "stock-notifications"]
+	 *           [false, false, "restock-alerts"]
+	 *           [true, false, "restock-alerts"]
+	 *           [false, true, "stock-notifications"]
+	 *           [true, true, "stock-notifications"]
+	 *           [false, false, ""]
+	 *           [true, false, ""]
+	 *           [false, true, ""]
+	 *           [true, true, ""]
+	 *           [false, false, "stock-notifications", true]
+	 *           [true, false, "stock-notifications", true]
+	 *           [false, false, "restock-alerts", true]
+	 *           [true, false, "restock-alerts", true]
+	 *           [false, true, "stock-notifications", true]
+	 *           [true, true, "stock-notifications", true]
+	 *           [false, false, "", true]
+	 *           [true, false, "", true]
+	 *           [false, true, "", true]
+	 *           [true, true, "", true]
+	 *
+	 * @param bool   $verified Whether to render the confirmation email.
+	 * @param bool   $guest Whether the signup belongs to a guest.
+	 * @param string $endpoint The configured account endpoint.
+	 * @param bool   $plain_text Whether to render the plain-text email instead of HTML.
+	 */
+	public function test_email_footer_links( bool $verified, bool $guest, string $endpoint, bool $plain_text = false ): void {
+		update_option( MyAccountEndpoint::ENDPOINT_OPTION, $endpoint );
+		new MyAccountEndpoint();
+		WC()->query->init_query_vars();
+		$notification = $this->build_notification();
+		if ( ! $guest ) {
+			$notification->set_user_id( self::factory()->user->create( array( 'role' => 'customer' ) ) );
+			$notification->save();
+		}
+
+		$emails = WC()->mailer()->get_emails();
+		$sut    = $emails[ $verified ? 'WC_Email_Customer_Stock_Notification_Verified' : 'WC_Email_Customer_Stock_Notification' ];
+		$sut->prepare_email( $notification );
+		$notification->save();
+		$content = $plain_text ? $sut->get_content_plain() : $sut->get_content_html();
+		$pattern = $plain_text ? '/^.*: (https?:\/\/\S+)$/m' : '/href="([^"]+)" id="notification__unsubscribe_link"/';
+		$this->assertSame( 1, preg_match( $pattern, $content, $matches ), 'The footer must contain its management link.' );
+		$link = html_entity_decode( $matches[1], ENT_QUOTES );
+		parse_str( (string) wp_parse_url( $link, PHP_URL_QUERY ), $query );
+
+		$this->assertSame( 'back-in-stock-notifications', $query['utm_source'] );
+		$this->assertSame( 'email', $query['utm_medium'] );
+		if ( $guest || '' === $endpoint ) {
+			$this->assertSame( 'unsubscribe', $query['email_link_action'] );
+			$this->assertTrue( ( new Notification( $notification->get_id() ) )->check_unsubscribe_key( $query['email_link_action_key'] ), 'The unsubscribe link must contain the persisted unsubscribe key.' );
+			$this->assertSame( (string) $notification->get_id(), $query['notification_id'] );
+			$this->assertStringContainsString( 'To stop receiving these messages,', $content );
+			$this->assertStringNotContainsString( 'To manage your notifications,', $content );
+		} else {
+			$this->assertSame( wc_get_account_endpoint_url( $endpoint ), remove_query_arg( array( 'utm_source', 'utm_medium' ), $link ) );
+			$this->assertArrayNotHasKey( 'email_link_action', $query );
+			$this->assertArrayNotHasKey( 'email_link_action_key', $query );
+			$this->assertStringContainsString( 'To manage your notifications,', $content );
+			$this->assertStringNotContainsString( 'To stop receiving these messages,', $content );
+		}
+	}
+
 	/**
 	 * Build a persisted notification for an in-stock simple product.
 	 *