Commit 48d302d9dcf for woocommerce

commit 48d302d9dcfc542e8a6a9d6a16e35787395d77a2
Author: Luigi Teschio <gigitux@gmail.com>
Date:   Fri Nov 20 17:41:18 2026 +0100

    Only copy the cart and coupons when cloning a shared session (#71204)

    * Only copy the cart and coupons when cloning a shared session

    (cherry picked from commit 5f59d6bec885622cb2379784b69fe61790c55af3)

    * Restore the original shared-session test import

    ---------

    Co-authored-by: Mike Jolley <mike.jolley@me.com>

diff --git a/plugins/woocommerce/includes/class-wc-session-handler.php b/plugins/woocommerce/includes/class-wc-session-handler.php
index b4572390f27..578f31c1c85 100644
--- a/plugins/woocommerce/includes/class-wc-session-handler.php
+++ b/plugins/woocommerce/includes/class-wc-session-handler.php
@@ -220,14 +220,21 @@ class WC_Session_Handler extends WC_Session {
 	}

 	/**
-	 * Clones a session to the current session. Exclude customer details for privacy reasons.
+	 * Clones the cart and applied coupons from a shared session into the current session.
+	 *
+	 * Other keys stay with the sharer: they can hold customer details, order IDs whose keys the sharer knows, or extension data.
 	 *
 	 * @param string $clone_from_customer_id The customer ID to clone from.
 	 */
 	private function clone_session_data( string $clone_from_customer_id ) {
-		$session_data                         = (array) $this->get_session( $clone_from_customer_id, array() );
+		$session_data                         = array_intersect_key(
+			(array) $this->get_session( $clone_from_customer_id, array() ),
+			array(
+				'cart'            => true,
+				'applied_coupons' => true,
+			)
+		);
 		$session_data['previous_customer_id'] = $clone_from_customer_id;
-		$session_data                         = array_diff_key( $session_data, array( 'customer' => true ) );
 		$this->_data                          = $session_data;
 		$this->_dirty                         = true;
 		$this->replace_session_on_save();
diff --git a/plugins/woocommerce/tests/legacy/unit-tests/session/class-wc-tests-session-handler.php b/plugins/woocommerce/tests/legacy/unit-tests/session/class-wc-tests-session-handler.php
index d34d911c4be..fd37c5d4555 100644
--- a/plugins/woocommerce/tests/legacy/unit-tests/session/class-wc-tests-session-handler.php
+++ b/plugins/woocommerce/tests/legacy/unit-tests/session/class-wc-tests-session-handler.php
@@ -8,6 +8,7 @@
 declare( strict_types=1 );

 use Automattic\WooCommerce\Internal\Features\FeaturesController;
+use Automattic\WooCommerce\StoreApi\Utilities\CartTokenUtils;

 /**
  * Tests for the WC_Session_Handler class.
@@ -318,6 +319,77 @@ class WC_Tests_Session_Handler extends WC_Unit_Test_Case {
 		$this->assertGreaterThanOrEqual( time() + 6 * DAY_IN_SECONDS, $cookie_expiration, 'Cookie expires in about a week.' );
 	}

+	/**
+	 * @testdox A session cloned from a ?session= token keeps only the sharer's cart and applied coupons.
+	 *
+	 * @testWith [0]
+	 *           [1]
+	 *
+	 * @param int $recipient_user_id User ID of the shopper who opens the link; 0 for a guest.
+	 */
+	public function test_clone_session_from_token_keeps_only_cart_and_coupons( int $recipient_user_id ) {
+		global $wpdb;
+
+		$sharer_session_id   = 't_' . wc_rand_hash( '', 30 );
+		$cart                = maybe_serialize( array( 'item_key' => array( 'product_id' => 123 ) ) );
+		$applied_coupons     = maybe_serialize( array( 'shared10' ) );
+		$sharer_session_data = array(
+			'cart'                    => $cart,
+			'applied_coupons'         => $applied_coupons,
+			'customer'                => maybe_serialize( array( 'email' => 'sharer@example.com' ) ),
+			'store_api_draft_order'   => 101,
+			'order_awaiting_payment'  => 102,
+			'store_api_customer_note' => 'Sharer note',
+			'chosen_payment_method'   => 'bacs',
+			'extension_session_data'  => maybe_serialize( array( 'secret' => 'sharer' ) ),
+		);
+
+		$wpdb->insert(
+			$wpdb->prefix . 'woocommerce_sessions',
+			array(
+				'session_key'    => $sharer_session_id,
+				'session_value'  => maybe_serialize( $sharer_session_data ),
+				'session_expiry' => time() + DAY_IN_SECONDS,
+			),
+			array( '%s', '%s', '%d' )
+		);
+
+		wp_set_current_user( $recipient_user_id );
+		$_GET['session'] = CartTokenUtils::get_cart_token( $sharer_session_id );
+		add_filter( 'woocommerce_set_cookie_enabled', '__return_false' );
+
+		try {
+			$sut = new WC_Session_Handler();
+			$sut->init();
+		} finally {
+			unset( $_GET['session'] );
+			remove_filter( 'woocommerce_set_cookie_enabled', '__return_false' );
+			wp_set_current_user( 0 );
+		}
+
+		$this->assertNotSame( $sharer_session_id, $sut->get_customer_id(), 'The recipient gets a session of their own.' );
+
+		// Read the stored row directly: get_session_data() would return the copy save_data() just cached.
+		$stored_session_data = maybe_unserialize(
+			$wpdb->get_var(
+				$wpdb->prepare(
+					"SELECT session_value FROM {$wpdb->prefix}woocommerce_sessions WHERE session_key = %s",
+					$sut->get_customer_id()
+				)
+			)
+		);
+
+		$this->assertEquals(
+			array(
+				'cart'                 => $cart,
+				'applied_coupons'      => $applied_coupons,
+				'previous_customer_id' => $sharer_session_id,
+			),
+			$stored_session_data,
+			'Only the cart, applied coupons and source session ID are stored for the recipient.'
+		);
+	}
+
 	/**
 	 * Test that method destroys session when all conditions are met.
 	 */