Commit f0f0aab3e90 for woocommerce

commit f0f0aab3e90d8a81247406d58d6d5ea6f889a828
Author: samuel mbabhazi <111171386+samuelmbabhazi@users.noreply.github.com>
Date:   Fri Nov 27 12:31:31 2026 +0200

    Keep an order line as sold when a REST update names its own product (#70197)

    * Keep an order line as sold when a REST update names its own product

    `WC_REST_Orders_V2_Controller::prepare_line_items()`, its v1 twin and the
    v4 `UpdateUtils::prepare_line_item_data()` guard `set_product()` with
    `$product !== $item->get_product()`. Both sides are loaded afresh, so the
    objects never match and `set_product()` runs on every update that sends
    `product_id`, `variation_id` or `sku`, copying the product's current
    name, tax class and variation onto the line. The mobile apps send
    `product_id` for every line on every order edit, and REST line edits
    are not limited to editable orders, so a product moved to another tax
    class after the sale changed the class of its line on paid orders the
    next time the apps touched them (#70046).

    An update that names the line's own product (its variation, or the
    parent alone, as #67343 already recognised to keep the variation) now
    leaves the line alone. The explicit `variation_id` of 0 still demotes
    the line, and a variation that no longer exists still goes through the
    restore path, which demotes it to the parent and logs it, as before.
    `get_product()` is still called first, so extensions on
    `woocommerce_get_product_from_item` see the same sequence.

    Two #67343 examples asserted the "resynchronization" of the name and tax
    class on an unchanged parent; they now assert the opposite. The example
    that rethrew an extension veto on restoring a valid variation no longer
    reaches that setter: the update succeeds and the line keeps its
    variation. New examples cover a simple product on a completed order, a
    switch to another product (which does take the new product's name and
    tax class), an echoed variation, and the parent-only payload in v4.

    * v4: keep a variation line echoed back as the response spells it

    The v4 response names the variation in `product_id` and, when the
    variation inherits one, the parent's SKU. A client that echoes that
    line sends `product_id` and `variation_id` set to the variation and a
    `sku` that resolves to the parent, so the first version of the guard
    did not recognise its own parent and `set_product()` demoted the line.
    The parent-only check now accepts the stored variation in `product_id`
    and a `variation_id` that matches it, as the v2 restore path does.

    Two v4 examples: the line as it reads back keeps its variation, name
    and tax class; an explicit `variation_id` of 0 still demotes it.

    * Refactor REST order line product preservation checks

    * Remove V4 order line changes from the fix

    ---------

    Co-authored-by: Tom Cafferkey <tjcafferkey@gmail.com>

diff --git a/plugins/woocommerce/changelog/70046-rest-order-line-unchanged-product b/plugins/woocommerce/changelog/70046-rest-order-line-unchanged-product
new file mode 100644
index 00000000000..06fa5bafbc7
--- /dev/null
+++ b/plugins/woocommerce/changelog/70046-rest-order-line-unchanged-product
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Updating an order line over the REST API with its own product no longer resets the line's name, tax class and variation to the product's current values.
diff --git a/plugins/woocommerce/includes/rest-api/Controllers/Version1/class-wc-rest-orders-v1-controller.php b/plugins/woocommerce/includes/rest-api/Controllers/Version1/class-wc-rest-orders-v1-controller.php
index 31356c47e93..2ffbe02c6aa 100644
--- a/plugins/woocommerce/includes/rest-api/Controllers/Version1/class-wc-rest-orders-v1-controller.php
+++ b/plugins/woocommerce/includes/rest-api/Controllers/Version1/class-wc-rest-orders-v1-controller.php
@@ -720,7 +720,19 @@ class WC_REST_Orders_V1_Controller extends WC_REST_Posts_Controller {
 			$product = wc_get_product( $current_product_id );
 		}

-		if ( $product && $product !== $item->get_product() ) {
+		$current_product = $item->get_product();
+
+		if (
+			$product
+			&& ! $this->should_preserve_line_item_product(
+				$action,
+				$product,
+				$current_product_id,
+				$current_variation_id,
+				$restore_variation_id
+			)
+			&& $product !== $current_product
+		) {
 			$item->set_product( $product );
 			if ( $restore_variation_id ) {
 				try {
@@ -763,6 +775,44 @@ class WC_REST_Orders_V1_Controller extends WC_REST_Posts_Controller {

 	// phpcs:enable Squiz.Commenting.FunctionCommentThrowTag.WrongNumber

+	/**
+	 * Whether an update should preserve the line's recorded product details.
+	 *
+	 * @param string           $action               Create or update.
+	 * @param WC_Product|false $product              Product resolved from the request.
+	 * @param int              $current_product_id   Stored parent or simple product ID.
+	 * @param int              $current_variation_id Stored variation ID, or zero.
+	 * @param bool             $restore_variation_id Whether the request preserves the existing variation.
+	 * @return bool
+	 */
+	private function should_preserve_line_item_product(
+		$action,
+		$product,
+		$current_product_id,
+		$current_variation_id,
+		$restore_variation_id
+	) {
+		if ( 'update' !== $action || ! $product instanceof WC_Product ) {
+			return false;
+		}
+
+		// Leave deleted variations to the existing fallback handling.
+		if (
+			$current_variation_id
+			&& 'product_variation' !== get_post_type( $current_variation_id )
+		) {
+			return false;
+		}
+
+		if ( $restore_variation_id ) {
+			return true;
+		}
+
+		$current_id = $current_variation_id ? $current_variation_id : $current_product_id;
+
+		return $product->get_id() === $current_id;
+	}
+
 	/**
 	 * Create or update an order shipping method.
 	 *
diff --git a/plugins/woocommerce/includes/rest-api/Controllers/Version2/class-wc-rest-orders-v2-controller.php b/plugins/woocommerce/includes/rest-api/Controllers/Version2/class-wc-rest-orders-v2-controller.php
index b362d0d9ea2..605066f2dcb 100644
--- a/plugins/woocommerce/includes/rest-api/Controllers/Version2/class-wc-rest-orders-v2-controller.php
+++ b/plugins/woocommerce/includes/rest-api/Controllers/Version2/class-wc-rest-orders-v2-controller.php
@@ -989,7 +989,19 @@ class WC_REST_Orders_V2_Controller extends WC_REST_CRUD_Controller {
 			$product = wc_get_product( $current_product_id );
 		}

-		if ( $product && $product !== $item->get_product() ) {
+		$current_product = $item->get_product();
+
+		if (
+			$product
+			&& ! $this->should_preserve_line_item_product(
+				$action,
+				$product,
+				$current_product_id,
+				$current_variation_id,
+				$restore_variation_id
+			)
+			&& $product !== $current_product
+		) {
 			$item->set_product( $product );
 			if ( $restore_variation_id && $product_item ) {
 				try {
@@ -1046,6 +1058,44 @@ class WC_REST_Orders_V2_Controller extends WC_REST_CRUD_Controller {

 	// phpcs:enable Squiz.Commenting.FunctionCommentThrowTag.WrongNumber

+	/**
+	 * Whether an update should preserve the line's recorded product details.
+	 *
+	 * @param string           $action               Create or update.
+	 * @param WC_Product|false $product              Product resolved from the request.
+	 * @param int              $current_product_id   Stored parent or simple product ID.
+	 * @param int              $current_variation_id Stored variation ID, or zero.
+	 * @param bool             $restore_variation_id Whether the request preserves the existing variation.
+	 * @return bool
+	 */
+	private function should_preserve_line_item_product(
+		$action,
+		$product,
+		$current_product_id,
+		$current_variation_id,
+		$restore_variation_id
+	) {
+		if ( 'update' !== $action || ! $product instanceof WC_Product ) {
+			return false;
+		}
+
+		// Leave deleted variations to the existing fallback handling.
+		if (
+			$current_variation_id
+			&& 'product_variation' !== get_post_type( $current_variation_id )
+		) {
+			return false;
+		}
+
+		if ( $restore_variation_id ) {
+			return true;
+		}
+
+		$current_id = $current_variation_id ? $current_variation_id : $current_product_id;
+
+		return $product->get_id() === $current_id;
+	}
+
 	/**
 	 * Create or update an order shipping method.
 	 *
diff --git a/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version1/class-wc-rest-orders-v1-controller-tests.php b/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version1/class-wc-rest-orders-v1-controller-tests.php
index b57d6e7ec07..51dfa8988bb 100644
--- a/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version1/class-wc-rest-orders-v1-controller-tests.php
+++ b/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version1/class-wc-rest-orders-v1-controller-tests.php
@@ -296,8 +296,40 @@ class WC_REST_Orders_V1_Controller_Tests extends WC_REST_Unit_Test_Case {
 		$reloaded = new WC_Order_Item_Product( $item->get_id() );
 		$this->assertSame( $variation->get_id(), $reloaded->get_variation_id(), 'The existing variation ID should be preserved.' );
 		$this->assertSame( $variation->get_id(), $reloaded->get_product()->get_id(), 'The line item should continue to resolve to the variation.' );
-		$this->assertSame( $parent->get_name(), $reloaded->get_name(), 'The line-item name should retain its pre-regression resynchronization behavior.' );
-		$this->assertSame( $parent->get_tax_class(), $reloaded->get_tax_class(), 'The line-item tax class should retain its pre-regression resynchronization behavior.' );
+		$this->assertSame( 'Historical line item name', $reloaded->get_name(), 'Naming the line\'s own parent should leave the name it was sold under alone.' );
+		$this->assertSame( '', $reloaded->get_tax_class(), 'Naming the line\'s own parent should leave the tax class it was sold under alone.' );
+	}
+
+	/**
+	 * @testdox Updating a line with its own product keeps the name and tax class it was sold with.
+	 */
+	public function test_update_line_item_with_unchanged_product_keeps_name_and_tax_class(): void {
+		$product = WC_Helper_Product::create_simple_product();
+		$order   = WC_Helper_Order::create_order( 1, $product );
+		$item    = current( $order->get_items() );
+		$product->set_name( 'Renamed after the sale' );
+		$product->set_tax_class( 'reduced-rate' );
+		$product->save();
+
+		$request = new WP_REST_Request( 'PUT', '/wc/v1/orders/' . $order->get_id() );
+		$request->set_body_params(
+			array(
+				'line_items' => array(
+					array(
+						'id'         => $item->get_id(),
+						'product_id' => $product->get_id(),
+						'quantity'   => 3,
+					),
+				),
+			)
+		);
+		$response = $this->server->dispatch( $request );
+		$this->assertSame( 200, $response->get_status(), 'An update naming the line\'s own product should succeed.' );
+
+		$reloaded = new WC_Order_Item_Product( $item->get_id() );
+		$this->assertSame( 3, $reloaded->get_quantity(), 'The posted quantity should be applied.' );
+		$this->assertSame( $item->get_name(), $reloaded->get_name(), 'The line should keep the name it was sold under.' );
+		$this->assertSame( $item->get_tax_class(), $reloaded->get_tax_class(), 'The line should keep the tax class it was sold under.' );
 	}

 	/**
diff --git a/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version3/class-wc-rest-orders-controller-tests.php b/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version3/class-wc-rest-orders-controller-tests.php
index fc6ab0704f1..9bf1436d23d 100644
--- a/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version3/class-wc-rest-orders-controller-tests.php
+++ b/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version3/class-wc-rest-orders-controller-tests.php
@@ -2199,8 +2199,100 @@ class WC_REST_Orders_Controller_Tests extends WC_REST_Unit_Test_Case {
 		$this->assertSame( $variation->get_id(), $reloaded->get_product()->get_id(), 'get_product() should continue to resolve to the variation.' );
 		$this->assertSame( $parent->get_id(), $response_item['product_id'], 'The response should retain the variable parent product ID.' );
 		$this->assertSame( $variation->get_id(), $response_item['variation_id'], 'The response should retain the variation ID.' );
-		$this->assertSame( $parent->get_name(), $reloaded->get_name(), 'The line-item name should retain its pre-regression resynchronization behavior.' );
-		$this->assertSame( $parent->get_tax_class(), $reloaded->get_tax_class(), 'The line-item tax class should retain its pre-regression resynchronization behavior.' );
+		$this->assertSame( 'Historical line item name', $reloaded->get_name(), 'Naming the line\'s own parent should leave the name it was sold under alone.' );
+		$this->assertSame( '', $reloaded->get_tax_class(), 'Naming the line\'s own parent should leave the tax class it was sold under alone.' );
+	}
+
+	/**
+	 * @testdox PUT /orders naming a line's own product keeps the name and tax class it was sold with, on a completed order too.
+	 */
+	public function test_update_line_item_with_unchanged_product_keeps_name_and_tax_class(): void {
+		$product = WC_Helper_Product::create_simple_product();
+		$order   = WC_Helper_Order::create_order( 1, $product );
+		$order->set_status( OrderStatus::COMPLETED );
+		$order->save();
+		$item           = current( $order->get_items() );
+		$sold_name      = $item->get_name();
+		$sold_tax_class = $item->get_tax_class();
+
+		$product->set_name( 'Renamed after the sale' );
+		$product->set_tax_class( 'reduced-rate' );
+		$product->save();
+		$this->assertSame( 'reduced-rate', $product->get_tax_class(), 'Precondition: the product moved to another tax class.' );
+
+		$response = $this->dispatch_line_item_update(
+			$order->get_id(),
+			array(
+				'id'         => $item->get_id(),
+				'product_id' => $product->get_id(),
+				'quantity'   => 3,
+			)
+		);
+		$this->assertSame( 200, $response->get_status(), 'An update naming the line\'s own product should succeed.' );
+
+		$reloaded = new WC_Order_Item_Product( $item->get_id() );
+		$this->assertSame( 3, $reloaded->get_quantity(), 'The posted quantity should be applied.' );
+		$this->assertSame( $sold_name, $reloaded->get_name(), 'The line should keep the name it was sold under.' );
+		$this->assertSame( $sold_tax_class, $reloaded->get_tax_class(), 'The line should keep the tax class it was sold under.' );
+		$this->assertSame( $sold_tax_class, $response->get_data()['line_items'][0]['tax_class'], 'The response should report the stored tax class.' );
+	}
+
+	/**
+	 * @testdox PUT /orders switching a line to another product takes that product's name and tax class.
+	 */
+	public function test_update_line_item_with_another_product_takes_its_name_and_tax_class(): void {
+		$product = WC_Helper_Product::create_simple_product();
+		$order   = WC_Helper_Order::create_order( 1, $product );
+		$item    = current( $order->get_items() );
+		$other   = WC_Helper_Product::create_simple_product();
+		$other->set_name( 'The other product' );
+		$other->set_tax_class( 'reduced-rate' );
+		$other->save();
+
+		$response = $this->dispatch_line_item_update(
+			$order->get_id(),
+			array(
+				'id'         => $item->get_id(),
+				'product_id' => $other->get_id(),
+			)
+		);
+		$this->assertSame( 200, $response->get_status(), 'Switching the line to another product should succeed.' );
+
+		$reloaded = new WC_Order_Item_Product( $item->get_id() );
+		$this->assertSame( $other->get_id(), $reloaded->get_product_id(), 'The line should point at the other product.' );
+		$this->assertSame( 'The other product', $reloaded->get_name(), 'The line should take the other product\'s name.' );
+		$this->assertSame( 'reduced-rate', $reloaded->get_tax_class(), 'The line should take the other product\'s tax class.' );
+	}
+
+	/**
+	 * @testdox PUT /orders echoing a line's variation keeps the name and tax class it was sold with.
+	 */
+	public function test_update_line_item_with_echoed_variation_keeps_name_and_tax_class(): void {
+		list( $parent, $variation ) = $this->create_variable_product_with_color_variation();
+		list( $order, $item_id )    = $this->create_order_with_variation_line_item( $variation );
+		$item                       = new WC_Order_Item_Product( $item_id );
+		$item->set_name( 'Historical line item name' );
+		$item->set_tax_class( '' );
+		$item->save();
+		$variation->set_tax_class( 'reduced-rate' );
+		$variation->save();
+
+		$response = $this->dispatch_line_item_update(
+			$order->get_id(),
+			array(
+				'id'           => $item_id,
+				'product_id'   => $parent->get_id(),
+				'variation_id' => $variation->get_id(),
+				'quantity'     => 2,
+			)
+		);
+		$this->assertSame( 200, $response->get_status(), 'Echoing the line\'s own variation should succeed.' );
+
+		$reloaded = new WC_Order_Item_Product( $item_id );
+		$this->assertSame( 2, $reloaded->get_quantity(), 'The posted quantity should be applied.' );
+		$this->assertSame( $variation->get_id(), $reloaded->get_variation_id(), 'The variation should be kept.' );
+		$this->assertSame( 'Historical line item name', $reloaded->get_name(), 'The line should keep the name it was sold under.' );
+		$this->assertSame( '', $reloaded->get_tax_class(), 'The line should keep the tax class it was sold under.' );
 	}

 	/**
@@ -2229,9 +2321,9 @@ class WC_REST_Orders_Controller_Tests extends WC_REST_Unit_Test_Case {
 	}

 	/**
-	 * @testdox PUT /orders rethrows extension validation errors while the existing variation is still valid.
+	 * @testdox PUT /orders with an unchanged parent leaves an existing variation alone, so an extension veto on restoring it is never consulted.
 	 */
-	public function test_update_line_item_rethrows_extension_validation_error_for_existing_variation(): void {
+	public function test_update_line_item_with_unchanged_parent_does_not_restore_an_existing_variation(): void {
 		list( $parent, $variation ) = $this->create_variable_product_with_color_variation();
 		list( $order, $item_id )    = $this->create_order_with_variation_line_item( $variation );
 		$extension_validation_armed = false;
@@ -2270,9 +2362,8 @@ class WC_REST_Orders_Controller_Tests extends WC_REST_Unit_Test_Case {

 		$this->assertSame( 'product_variation', get_post_type( $variation->get_id() ), 'Precondition: the stored variation still exists.' );
 		$this->assertTrue( $extension_validation_armed, 'Precondition: the filtered order item subclass handled the update.' );
-		$this->assertSame( 400, $response->get_status(), 'Extension validation should reject the update.' );
-		$this->assertSame( 'order_item_product_invalid_variation_id', $response->get_data()['code'], 'The extension validation error should be preserved.' );
-		$this->assertSame( $variation->get_id(), (int) wc_get_order_item_meta( $item_id, '_variation_id' ), 'The failed update should not demote the line item.' );
+		$this->assertSame( 200, $response->get_status(), 'Nothing is restored, so the extension veto has nothing to reject.' );
+		$this->assertSame( $variation->get_id(), (int) wc_get_order_item_meta( $item_id, '_variation_id' ), 'The line item should keep its variation.' );
 	}

 	/**