Commit 0c0dddeead1 for woocommerce

commit 0c0dddeead192a1e3d0b5c39c8e6cb1dd4b107be
Author: Tom Cafferkey <tjcafferkey@gmail.com>
Date:   Tue Dec 1 11:31:36 2026 +0000

    Fix order item sales updates with numeric-string order IDs (#71577)

    * Fix order item sales updates with numeric-string order IDs

    * Add changelog entry for filtered order ID fix

diff --git a/plugins/woocommerce/changelog/fix-71547-numeric-string-order-item-sales b/plugins/woocommerce/changelog/fix-71547-numeric-string-order-item-sales
new file mode 100644
index 00000000000..db391dcd23c
--- /dev/null
+++ b/plugins/woocommerce/changelog/fix-71547-numeric-string-order-item-sales
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Prevent errors when saving or deleting order items if an extension returns an order ID as a numeric string.
diff --git a/plugins/woocommerce/src/Internal/Orders/ProductSales.php b/plugins/woocommerce/src/Internal/Orders/ProductSales.php
index c40c2a0116d..0b3f0414a07 100644
--- a/plugins/woocommerce/src/Internal/Orders/ProductSales.php
+++ b/plugins/woocommerce/src/Internal/Orders/ProductSales.php
@@ -71,7 +71,7 @@ class ProductSales implements RegisterHooksInterface {
 		$this->item_snapshots_before_save[ $object_id ] = array(
 			'product_id' => $persisted_item instanceof WC_Order_Item_Product ? $persisted_item->get_product_id() : 0,
 			'quantity'   => $persisted_item instanceof WC_Order_Item_Product ? absint( $persisted_item->get_quantity() ) : 0,
-			'order_id'   => $persisted_item ? $persisted_item->get_order_id() : $item->get_order_id(),
+			'order_id'   => absint( $persisted_item ? $persisted_item->get_order_id() : $item->get_order_id() ),
 		);
 	}

@@ -94,8 +94,9 @@ class ProductSales implements RegisterHooksInterface {
 			return;
 		}

+		$current_order_id                  = absint( $item->get_order_id() );
 		$previous_order_has_recorded_sales = $this->has_recorded_sales( $item_before_save['order_id'] );
-		$current_order_has_recorded_sales  = $item_before_save['order_id'] === $item->get_order_id() ? $previous_order_has_recorded_sales : $this->has_recorded_sales( $item->get_order_id() );
+		$current_order_has_recorded_sales  = $item_before_save['order_id'] === $current_order_id ? $previous_order_has_recorded_sales : $this->has_recorded_sales( $current_order_id );
 		if ( ! $previous_order_has_recorded_sales && ! $current_order_has_recorded_sales ) {
 			return;
 		}
@@ -126,7 +127,7 @@ class ProductSales implements RegisterHooksInterface {
 		$item_id = absint( $item_id );
 		unset( $this->item_sales_before_delete[ $item_id ] );
 		$item = WC_Order_Factory::get_order_item( $item_id );
-		if ( ! $item instanceof WC_Order_Item_Product || ! $this->has_recorded_sales( $item->get_order_id() ) ) {
+		if ( ! $item instanceof WC_Order_Item_Product || ! $this->has_recorded_sales( absint( $item->get_order_id() ) ) ) {
 			return;
 		}

diff --git a/plugins/woocommerce/tests/php/src/Internal/Orders/ProductSalesTest.php b/plugins/woocommerce/tests/php/src/Internal/Orders/ProductSalesTest.php
index 2c7671a4bec..7ea87eb718f 100644
--- a/plugins/woocommerce/tests/php/src/Internal/Orders/ProductSalesTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/Orders/ProductSalesTest.php
@@ -45,6 +45,66 @@ class ProductSalesTest extends WC_Unit_Test_Case {
 		$this->assertSame( 0, wc_get_product( $product->get_id() )->get_total_sales(), 'CRUD deletion should also reverse sales.' );
 	}

+	/**
+	 * @testdox Reconciles sales when saved items have filtered numeric-string order IDs.
+	 */
+	public function test_item_saves_with_numeric_string_order_ids(): void {
+		$product = WC_Helper_Product::create_simple_product();
+		$order   = wc_create_order();
+		$pending = wc_create_order();
+		$item_id = $order->add_product( $product, 1 );
+		$order->update_status( OrderStatus::ON_HOLD );
+
+		$filter = static function ( $order_id ) {
+			return (string) $order_id;
+		};
+		add_filter( 'woocommerce_order_item_get_order_id', $filter );
+
+		try {
+			$item = new WC_Order_Item_Product( $item_id );
+			$this->assertSame( (string) $order->get_id(), $item->get_order_id(), 'The filter should return a numeric-string order ID.' );
+			$item->set_quantity( 2 );
+			$item->save();
+			$item->save();
+			$this->assertSame( 2, wc_get_product( $product->get_id() )->get_total_sales(), 'Repeated saves should count the quantity difference only once.' );
+
+			$order->add_product( $product, 3 );
+			$this->assertSame( 5, wc_get_product( $product->get_id() )->get_total_sales(), 'New items should also accept filtered order IDs.' );
+
+			$item->set_order_id( $pending->get_id() );
+			$item->save();
+			$this->assertSame( 3, wc_get_product( $product->get_id() )->get_total_sales(), 'Moving to an unrecorded order should reverse the item sales.' );
+
+			$item->set_order_id( $order->get_id() );
+			$item->save();
+			$this->assertSame( 5, wc_get_product( $product->get_id() )->get_total_sales(), 'Moving back should record the item sales again.' );
+		} finally {
+			remove_filter( 'woocommerce_order_item_get_order_id', $filter );
+		}
+	}
+
+	/**
+	 * @testdox Reverses sales when deleted items have filtered numeric-string order IDs.
+	 */
+	public function test_item_deletion_with_numeric_string_order_ids(): void {
+		$product = WC_Helper_Product::create_simple_product();
+		$order   = wc_create_order();
+		$item_id = $order->add_product( $product, 2 );
+		$order->update_status( OrderStatus::ON_HOLD );
+
+		$filter = static function ( $order_id ) {
+			return (string) $order_id;
+		};
+		add_filter( 'woocommerce_order_item_get_order_id', $filter );
+
+		try {
+			wc_delete_order_item( $item_id );
+			$this->assertSame( 0, wc_get_product( $product->get_id() )->get_total_sales(), 'Deleting an item should reverse its sales with a filtered order ID.' );
+		} finally {
+			remove_filter( 'woocommerce_order_item_get_order_id', $filter );
+		}
+	}
+
 	/**
 	 * @testdox Does not count edits to an order whose sales have not been recorded.
 	 */