From f446315f325352c6546db5ffbdcdd9550897acbe Mon Sep 17 00:00:00 2001 From: "Jorge A. Torres" Date: Fri, 22 Sep 2023 00:44:22 +0100 Subject: [PATCH] Make sure orders are always saved with addresses indexes set (#40332) * Make sure orders are saved with addresses indexes set. * Add changelog --- .../woocommerce/changelog/fix-always-set-address-indexes | 4 ++++ .../src/Internal/DataStores/Orders/OrdersTableDataStore.php | 6 ++++-- 2 files changed, 8 insertions(+), 2 deletions(-) create mode 100644 plugins/woocommerce/changelog/fix-always-set-address-indexes diff --git a/plugins/woocommerce/changelog/fix-always-set-address-indexes b/plugins/woocommerce/changelog/fix-always-set-address-indexes new file mode 100644 index 00000000000..3294b511960 --- /dev/null +++ b/plugins/woocommerce/changelog/fix-always-set-address-indexes @@ -0,0 +1,4 @@ +Significance: patch +Type: fix + +Always generate address metadata indexes for HPOS orders. diff --git a/plugins/woocommerce/src/Internal/DataStores/Orders/OrdersTableDataStore.php b/plugins/woocommerce/src/Internal/DataStores/Orders/OrdersTableDataStore.php index 71d66846787..b8baed86f82 100644 --- a/plugins/woocommerce/src/Internal/DataStores/Orders/OrdersTableDataStore.php +++ b/plugins/woocommerce/src/Internal/DataStores/Orders/OrdersTableDataStore.php @@ -2580,8 +2580,10 @@ FROM $order_meta_table private function update_address_index_meta( $order, $changes ) { // If address changed, store concatenated version to make searches faster. foreach ( array( 'billing', 'shipping' ) as $address_type ) { - if ( isset( $changes[ $address_type ] ) ) { - $order->update_meta_data( "_{$address_type}_address_index", implode( ' ', $order->get_address( $address_type ) ) ); + $index_meta_key = "_{$address_type}_address_index"; + + if ( isset( $changes[ $address_type ] ) || ( is_a( $order, 'WC_Order' ) && empty( $order->get_meta( $index_meta_key ) ) ) ) { + $order->update_meta_data( $index_meta_key, implode( ' ', $order->get_address( $address_type ) ) ); } } }