From d4141958b23abd180a0ebd41b39197f36f29315d Mon Sep 17 00:00:00 2001 From: Vedanshu Jain Date: Tue, 8 Aug 2023 21:31:00 +0530 Subject: [PATCH] Set the orders table exist option if its not set. (#39616) * Set the orders table exist option if its not set. * Add changelog. * Must use yoda conditions. * Remove unnecessary option set. --- .../woocommerce/changelog/fix-sync-upgrade | 4 +++ .../DataStores/Orders/DataSynchronizer.php | 35 +++++++++++++------ 2 files changed, 28 insertions(+), 11 deletions(-) create mode 100644 plugins/woocommerce/changelog/fix-sync-upgrade diff --git a/plugins/woocommerce/changelog/fix-sync-upgrade b/plugins/woocommerce/changelog/fix-sync-upgrade new file mode 100644 index 00000000000..8b6243f1402 --- /dev/null +++ b/plugins/woocommerce/changelog/fix-sync-upgrade @@ -0,0 +1,4 @@ +Significance: patch +Type: fix + +Set the order table exist options value when its not present for smooth upgradation from lower WC versions. diff --git a/plugins/woocommerce/src/Internal/DataStores/Orders/DataSynchronizer.php b/plugins/woocommerce/src/Internal/DataStores/Orders/DataSynchronizer.php index 496a49743b5..770d232dfeb 100644 --- a/plugins/woocommerce/src/Internal/DataStores/Orders/DataSynchronizer.php +++ b/plugins/woocommerce/src/Internal/DataStores/Orders/DataSynchronizer.php @@ -128,8 +128,26 @@ class DataSynchronizer implements BatchProcessorInterface { if ( count( $missing_tables ) === 0 ) { update_option( self::ORDERS_TABLE_CREATED, 'yes' ); return true; + } else { + update_option( self::ORDERS_TABLE_CREATED, 'no' ); + return false; + } + } + + /** + * Returns the value of the orders table created option. If it's not set, then it checks the orders table and set it accordingly. + * + * @return bool Whether orders table exists. + */ + public function get_table_exists(): bool { + $table_exists = get_option( self::ORDERS_TABLE_CREATED ); + switch ( $table_exists ) { + case 'no': + case 'yes': + return 'yes' === $table_exists; + default: + return $this->check_orders_table_exists(); } - return false; } /** @@ -137,10 +155,7 @@ class DataSynchronizer implements BatchProcessorInterface { */ public function create_database_tables() { $this->database_util->dbdelta( $this->data_store->get_database_schema() ); - if ( ! $this->check_orders_table_exists() ) { - return; - } - update_option( self::ORDERS_TABLE_CREATED, 'yes' ); + $this->check_orders_table_exists(); } /** @@ -223,7 +238,7 @@ class DataSynchronizer implements BatchProcessorInterface { return 0; } - if ( 'yes' !== get_option( self::ORDERS_TABLE_CREATED ) ) { + if ( ! $this->get_table_exists() ) { $count = $wpdb->get_var( // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare -- $order_post_type_placeholder is prepared. $wpdb->prepare( @@ -652,12 +667,10 @@ ORDER BY orders.id ASC return; } - if ( 'yes' !== get_option( self::ORDERS_TABLE_CREATED ) ) { - return; - } - if ( $this->data_sync_is_enabled() ) { - $this->data_store->delete_order_data_from_custom_order_tables( $postid ); + if ( $this->get_table_exists() ) { + $this->data_store->delete_order_data_from_custom_order_tables( $postid ); + } } elseif ( $this->custom_orders_table_is_authoritative() ) { return; }