Commit 73349f0a48 for woocommerce
commit 73349f0a482d17df2fc6532c3fb9b21d98011687
Author: Joshua T Flowers <joshuatf@gmail.com>
Date: Wed Sep 17 09:50:02 2025 -0400
Fix index key length for status on wc_order_stats table (#60896)
* Fix cache key for status on wc_order_stats table
* Update order statuses cache to use WP_Cache instead of transient
* Add changelog entry
* Add back in the deprecated method with a warning for backwards compatibility
* Remove unnecessary return statement
* Performance: minor code tweaks.
* Update changelog type to performance
---------
Co-authored-by: Vladimir Reznichenko <kalessil@gmail.com>
diff --git a/plugins/woocommerce/changelog/fix-60790 b/plugins/woocommerce/changelog/fix-60790
new file mode 100644
index 0000000000..12801d9339
--- /dev/null
+++ b/plugins/woocommerce/changelog/fix-60790
@@ -0,0 +1,4 @@
+Significance: patch
+Type: performance
+
+Fix index key length for status on wc_order_stats table
diff --git a/plugins/woocommerce/includes/class-wc-install.php b/plugins/woocommerce/includes/class-wc-install.php
index a22844761e..9b8a9436cf 100644
--- a/plugins/woocommerce/includes/class-wc-install.php
+++ b/plugins/woocommerce/includes/class-wc-install.php
@@ -1946,12 +1946,12 @@ CREATE TABLE {$wpdb->prefix}wc_order_stats (
shipping_total double DEFAULT 0 NOT NULL,
net_total double DEFAULT 0 NOT NULL,
returning_customer tinyint(1) DEFAULT NULL,
- status varchar(200) NOT NULL,
+ status varchar(20) NOT NULL,
customer_id bigint(20) unsigned NOT NULL,
PRIMARY KEY (order_id),
KEY date_created (date_created),
KEY customer_id (customer_id),
- KEY status (status({$max_index_length}))
+ KEY status (status)
) $collate;
CREATE TABLE {$wpdb->prefix}wc_order_product_lookup (
order_item_id bigint(20) unsigned NOT NULL,
diff --git a/plugins/woocommerce/src/Admin/API/Reports/Orders/DataStore.php b/plugins/woocommerce/src/Admin/API/Reports/Orders/DataStore.php
index d7b7a58ed2..cd2c04c87b 100644
--- a/plugins/woocommerce/src/Admin/API/Reports/Orders/DataStore.php
+++ b/plugins/woocommerce/src/Admin/API/Reports/Orders/DataStore.php
@@ -22,9 +22,9 @@ class DataStore extends ReportsDataStore implements DataStoreInterface {
use OrderAttributionMeta;
/**
- * The transient name.
+ * The cache key for order statuses.
*/
- const ORDERS_STATUSES_ALL_TRANSIENT = 'woocommerce_analytics_orders_statuses_all';
+ const ORDERS_STATUSES_ALL_CACHE_KEY = 'woocommerce_analytics_orders_statuses_all';
/**
* Dynamically sets the date column name based on configuration
@@ -42,7 +42,7 @@ class DataStore extends ReportsDataStore implements DataStoreInterface {
* @internal
*/
final public static function init() {
- add_action( 'woocommerce_analytics_update_order_stats', array( __CLASS__, 'maybe_update_order_statuses_transient' ) );
+ add_action( 'woocommerce_analytics_update_order_stats', array( __CLASS__, 'maybe_update_order_statuses_cache' ) );
}
/**
@@ -620,21 +620,16 @@ class DataStore extends ReportsDataStore implements DataStoreInterface {
/**
* Get all statuses that have been synced.
*
- * @return array Unique order statuses.
+ * @return string[] Unique order statuses.
*/
public static function get_all_statuses() {
global $wpdb;
- $statuses = get_transient( self::ORDERS_STATUSES_ALL_TRANSIENT );
+ $statuses = wp_cache_get( self::ORDERS_STATUSES_ALL_CACHE_KEY, 'woocommerce_analytics' );
if ( false === $statuses ) {
- /* phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared */
$table_name = self::get_db_table_name();
- $statuses = $wpdb->get_col(
- "SELECT DISTINCT status FROM {$table_name}"
- );
- /* phpcs:enable */
-
- set_transient( self::ORDERS_STATUSES_ALL_TRANSIENT, $statuses, YEAR_IN_SECONDS );
+ $statuses = $wpdb->get_col( $wpdb->prepare( 'SELECT DISTINCT status FROM %i', $table_name ) );
+ wp_cache_set( self::ORDERS_STATUSES_ALL_CACHE_KEY, $statuses, 'woocommerce_analytics', YEAR_IN_SECONDS );
}
return $statuses;
@@ -645,19 +640,32 @@ class DataStore extends ReportsDataStore implements DataStoreInterface {
*
* @internal
* @param int $order_id Order ID.
+ * @return void
*/
- public static function maybe_update_order_statuses_transient( $order_id ) {
+ public static function maybe_update_order_statuses_cache( $order_id ) {
$order = wc_get_order( $order_id );
if ( $order ) {
$status = self::normalize_order_status( $order->get_status() );
$statuses = self::get_all_statuses();
if ( ! in_array( $status, $statuses, true ) ) {
$statuses[] = $status;
- set_transient( self::ORDERS_STATUSES_ALL_TRANSIENT, $statuses, YEAR_IN_SECONDS );
+ wp_cache_set( self::ORDERS_STATUSES_ALL_CACHE_KEY, $statuses, 'woocommerce_analytics', YEAR_IN_SECONDS );
}
}
}
+ /**
+ * Ensure the order status will present in `get_all_statuses` call result.
+ *
+ * @deprecated 10.3.0 Use maybe_update_order_statuses_cache().
+ * @param int $order_id Order ID.
+ * @return void
+ */
+ public static function maybe_update_order_statuses_transient( $order_id ) {
+ wc_deprecated_function( __METHOD__, '10.3.0', __CLASS__ . '::maybe_update_order_statuses_cache()' );
+ self::maybe_update_order_statuses_cache( $order_id );
+ }
+
/**
* Initialize query objects.
*/