Commit 76e750047f for woocommerce

commit 76e750047f32d22a6acb7bb7b4623092891101b3
Author: Vladimir Reznichenko <kalessil@gmail.com>
Date:   Tue Jan 27 17:53:04 2026 +0100

    [Performance] Products data store: SQL performance tweaks for updating product stock and total sales (#62930)

    Updating meta-value via SQL performs slightly faster when we provide a pre-formatted string value rather than a float in the SQL itself (the DB doesn't need to do type conversion).

diff --git a/plugins/woocommerce/changelog/performance-41798-product-data-store-SQLs b/plugins/woocommerce/changelog/performance-41798-product-data-store-SQLs
new file mode 100644
index 0000000000..0cb47288dd
--- /dev/null
+++ b/plugins/woocommerce/changelog/performance-41798-product-data-store-SQLs
@@ -0,0 +1,4 @@
+Significance: patch
+Type: performance
+
+Products: performance tweaks for data store SQLs setting stock and total sales.
diff --git a/plugins/woocommerce/includes/data-stores/class-wc-product-data-store-cpt.php b/plugins/woocommerce/includes/data-stores/class-wc-product-data-store-cpt.php
index 0670793d38..19e379759d 100644
--- a/plugins/woocommerce/includes/data-stores/class-wc-product-data-store-cpt.php
+++ b/plugins/woocommerce/includes/data-stores/class-wc-product-data-store-cpt.php
@@ -1664,8 +1664,8 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da

 		// Generate SQL.
 		$sql = $wpdb->prepare(
-			"UPDATE {$wpdb->postmeta} SET meta_value = %f WHERE post_id = %d AND meta_key='_stock'",
-			$stock_quantity,
+			"UPDATE {$wpdb->postmeta} SET meta_value = %s WHERE post_id = %d AND meta_key='_stock'",
+			number_format( (float) $stock_quantity, WC_ROUNDING_PRECISION, '.', '' ),
 			$product_id_with_stock
 		);

@@ -1704,8 +1704,8 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da

 			// Generate SQL.
 			$sql = $wpdb->prepare(
-				"UPDATE {$wpdb->postmeta} SET meta_value = %f WHERE post_id = %d AND meta_key='_stock'",
-				$new_stock,
+				"UPDATE {$wpdb->postmeta} SET meta_value = %s WHERE post_id = %d AND meta_key='_stock'",
+				number_format( (float) $new_stock, WC_ROUNDING_PRECISION, '.', '' ),
 				$product_id_with_stock
 			);
 		} else {
@@ -1799,8 +1799,8 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da
 				// phpcs:ignore WordPress.VIP.DirectDatabaseQuery.DirectQuery
 				$wpdb->query(
 					$wpdb->prepare(
-						"UPDATE {$wpdb->postmeta} SET meta_value = %f WHERE post_id = %d AND meta_key='total_sales'",
-						$quantity,
+						"UPDATE {$wpdb->postmeta} SET meta_value = %s WHERE post_id = %d AND meta_key='total_sales'",
+						number_format( (float) $quantity, WC_ROUNDING_PRECISION, '.', '' ),
 						$product_id
 					)
 				);
diff --git a/plugins/woocommerce/tests/php/includes/data-stores/class-wc-product-data-store-cpt-test.php b/plugins/woocommerce/tests/php/includes/data-stores/class-wc-product-data-store-cpt-test.php
index e8085730a2..a33051f623 100644
--- a/plugins/woocommerce/tests/php/includes/data-stores/class-wc-product-data-store-cpt-test.php
+++ b/plugins/woocommerce/tests/php/includes/data-stores/class-wc-product-data-store-cpt-test.php
@@ -336,4 +336,46 @@ class WC_Product_Data_Store_CPT_Test extends WC_Unit_Test_Case {

 		$this->assertEquals( '12.34', get_post_meta( $product->get_id(), '_cogs_total_value', true ) );
 	}
+
+	/**
+	 * Test update_product_stock updates on the meta-entry.
+	 */
+	public function test_update_product_stock_meta_update(): void {
+		/** @var WC_Product_Data_Store_CPT $store */
+		$store = WC_Data_Store::load( 'product' );
+
+		$product = new WC_Product();
+		$product->save();
+		$product_id = $product->get_id();
+
+		$store->update_product_stock( $product_id, null, 'set' );
+		$this->assertSame( '0.000000', get_post_meta( $product_id, '_stock', true ) );
+		$store->update_product_stock( $product_id, 10, 'set' );
+		$this->assertSame( '10.000000', get_post_meta( $product_id, '_stock', true ) );
+		$store->update_product_stock( $product_id, 20.0, 'set' );
+		$this->assertSame( '20.000000', get_post_meta( $product_id, '_stock', true ) );
+		$store->update_product_stock( $product_id, 30.5, 'set' );
+		$this->assertSame( '30.000000', get_post_meta( $product_id, '_stock', true ) );
+	}
+
+	/**
+	 * Test update_product_sales updates on the meta-entry.
+	 */
+	public function test_update_product_sales_meta_update(): void {
+		/** @var WC_Product_Data_Store_CPT $store */
+		$store = WC_Data_Store::load( 'product' );
+
+		$product = new WC_Product();
+		$product->save();
+		$product_id = $product->get_id();
+
+		$store->update_product_sales( $product_id, null, 'set' );
+		$this->assertSame( '0.000000', get_post_meta( $product_id, 'total_sales', true ) );
+		$store->update_product_sales( $product_id, 10, 'set' );
+		$this->assertSame( '10.000000', get_post_meta( $product_id, 'total_sales', true ) );
+		$store->update_product_sales( $product_id, 20.0, 'set' );
+		$this->assertSame( '20.000000', get_post_meta( $product_id, 'total_sales', true ) );
+		$store->update_product_sales( $product_id, 30.5, 'set' );
+		$this->assertSame( '30.500000', get_post_meta( $product_id, 'total_sales', true ) );
+	}
 }