Commit c247382858 for woocommerce
commit c2473828581d2c600c346875af7c2247b776ea2a
Author: Vladimir Reznichenko <kalessil@gmail.com>
Date: Sat Feb 21 10:05:05 2026 +0100
[Performance] Orders: reduce the number of SQLs required to persist an order (#63372)
Added guarding conditions for updating CoGS meta when persisting an order to reduce the number of SQLs on the checkout page.
diff --git a/plugins/woocommerce/changelog/performance-63118-reduce-sqls-number-iteration-5 b/plugins/woocommerce/changelog/performance-63118-reduce-sqls-number-iteration-5
new file mode 100644
index 0000000000..32079f5575
--- /dev/null
+++ b/plugins/woocommerce/changelog/performance-63118-reduce-sqls-number-iteration-5
@@ -0,0 +1,4 @@
+Significance: minor
+Type: performance
+
+Performance: reduced the number of SQL queries required to persist a draft order during checkout (with CoGS enabled).
diff --git a/plugins/woocommerce/phpstan-baseline.neon b/plugins/woocommerce/phpstan-baseline.neon
index 81fbfe9a0d..16c8c7774a 100644
--- a/plugins/woocommerce/phpstan-baseline.neon
+++ b/plugins/woocommerce/phpstan-baseline.neon
@@ -64041,12 +64041,6 @@ parameters:
count: 1
path: src/Internal/DataStores/Orders/OrdersTableDataStore.php
- -
- message: '#^Method Automattic\\WooCommerce\\Internal\\DataStores\\Orders\\OrdersTableDataStore\:\:save_cogs_data\(\) has no return type specified\.$#'
- identifier: missingType.return
- count: 1
- path: src/Internal/DataStores/Orders/OrdersTableDataStore.php
-
-
message: '#^Method Automattic\\WooCommerce\\Internal\\DataStores\\Orders\\OrdersTableDataStore\:\:set_download_permissions_granted\(\) has no return type specified\.$#'
identifier: missingType.return
diff --git a/plugins/woocommerce/src/Internal/DataStores/Orders/OrdersTableDataStore.php b/plugins/woocommerce/src/Internal/DataStores/Orders/OrdersTableDataStore.php
index 12de72fd3d..e30bc27b5d 100644
--- a/plugins/woocommerce/src/Internal/DataStores/Orders/OrdersTableDataStore.php
+++ b/plugins/woocommerce/src/Internal/DataStores/Orders/OrdersTableDataStore.php
@@ -2140,7 +2140,7 @@ FROM $order_meta_table
$this->set_custom_taxonomies( $order, $default_taxonomies );
if ( $order->has_cogs() && $this->cogs_is_enabled() ) {
- $this->save_cogs_data( $order );
+ $this->save_cogs_data( $order, ! $only_changes || array_key_exists( 'cogs_total_value', $changes ) );
}
$this->clear_cached_data( array( $order->get_id() ) );
@@ -2149,10 +2149,11 @@ FROM $order_meta_table
/**
* Save the Cost of Goods Sold value of a given order to the database.
*
- * @param WC_Abstract_Order $order The order to save the COGS value for.
+ * @param WC_Abstract_Order $order The order to save the COGS value for.
+ * @param bool $cogs_value_changed Whether the CoGS value was changed through the order object API.
*/
- private function save_cogs_data( WC_Abstract_Order $order ) {
- $cogs_value = $order->get_cogs_total_value();
+ private function save_cogs_data( WC_Abstract_Order $order, bool $cogs_value_changed ): void {
+ $cogs_value_original = $order->get_cogs_total_value();
/**
* Filter to customize the Cost of Goods Sold value that gets saved for a given order,
@@ -2160,29 +2161,31 @@ FROM $order_meta_table
*
* @since 9.5.0
*
- * @param float|null $cogs_value The value to be written to the database. If returned as null, nothing will be written.
- * @param WC_Abstract_Order $item The order for which the value is being saved.
+ * @param float $cogs_value The value to be written to the database. If returned as null, nothing will be written.
+ * @param WC_Abstract_Order $order The order for which the value is being saved.
*/
- $cogs_value = apply_filters( 'woocommerce_save_order_cogs_value', $cogs_value, $order );
- if ( is_null( $cogs_value ) ) {
+ $cogs_value = apply_filters( 'woocommerce_save_order_cogs_value', $cogs_value_original, $order );
+ if ( null === $cogs_value ) {
return;
}
- $existing_meta = $this->data_store_meta->get_metadata_by_key( $order, '_cogs_total_value' );
-
- if ( 0.0 === $cogs_value && $existing_meta ) {
- $existing_meta = current( $existing_meta );
- $this->data_store_meta->delete_meta( $order, $existing_meta );
- } elseif ( $existing_meta ) {
+ $sync_meta = $cogs_value_changed || $cogs_value_original !== (float) $cogs_value;
+ if ( $sync_meta ) {
+ $existing_meta = $this->data_store_meta->get_metadata_by_key( $order, '_cogs_total_value' );
+ if ( 0.0 === $cogs_value && $existing_meta ) {
+ $existing_meta = current( $existing_meta );
+ $this->data_store_meta->delete_meta( $order, $existing_meta );
+ } elseif ( $existing_meta ) {
$existing_meta = current( $existing_meta );
$existing_meta->key = '_cogs_total_value';
$existing_meta->value = $cogs_value;
$this->data_store_meta->update_meta( $order, $existing_meta );
- } else {
- $meta = new \WC_Meta_Data();
- $meta->key = '_cogs_total_value';
- $meta->value = $cogs_value;
- $this->data_store_meta->add_meta( $order, $meta );
+ } else {
+ $meta = new \WC_Meta_Data();
+ $meta->key = '_cogs_total_value';
+ $meta->value = $cogs_value;
+ $this->data_store_meta->add_meta( $order, $meta );
+ }
}
}