Update biling and shipping address indexes. (#35121)

* Update biling and shipping address indexes.

* Add changelog.

* Code standard fixes.

* Add unit test for search after update.

* PHPCS fixes.
This commit is contained in:
Vedanshu Jain 2022-10-19 16:21:42 +05:30 committed by GitHub
parent a2076f59a5
commit 5b6ddf0b88
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 89 additions and 35 deletions

View File

@ -0,0 +1,4 @@
Significance: patch
Type: fix
Add billing and shipping address indexes on order update.

View File

@ -1008,8 +1008,8 @@ WHERE
* @return bool Whether the order should be synced.
*/
private function should_sync_order( \WC_Abstract_Order $order ) : bool {
$draft_order = in_array( $order->get_status(), array( 'draft', 'auto-draft' ) );
$already_synced = in_array( $order->get_id(), self::$reading_order_ids );
$draft_order = in_array( $order->get_status(), array( 'draft', 'auto-draft' ), true );
$already_synced = in_array( $order->get_id(), self::$reading_order_ids, true );
return ! $draft_order && ! $already_synced;
}
@ -1544,6 +1544,9 @@ FROM $order_meta_table
throw new \Exception( sprintf( __( 'Could not persist order to database table "%s".', 'woocommerce' ), $update['table'] ) );
}
}
$changes = $order->get_changes();
$this->update_address_index_meta( $order, $changes );
}
/**
@ -2025,7 +2028,17 @@ FROM $order_meta_table
*/
public function update_order_meta( &$order ) {
$changes = $order->get_changes();
$this->update_address_index_meta( $order, $changes );
}
/**
* Helper function to update billing and shipping address metadata.
* @param \WC_Abstract_Order $order Order Object
* @param array $changes Array of changes.
*
* @return void
*/
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 ] ) ) {

View File

@ -187,10 +187,12 @@ class OrdersTableDataStoreTests extends WC_Unit_Test_Case {
'status' => 'on-hold',
'cart_hash' => 'YET-ANOTHER-CART-HASH',
);
static $datastore_updates = array(
'email_sent' => true,
'order_stock_reduced' => true,
);
static $meta_to_update = array(
'my_meta_key' => array( 'my', 'custom', 'meta' ),
);
@ -1003,10 +1005,14 @@ class OrdersTableDataStoreTests extends WC_Unit_Test_Case {
* @testDox Test `get_unpaid_orders()`.
*/
public function test_get_unpaid_orders(): void {
// phpcs:ignore WordPress.DateTime.CurrentTimeTimestamp.Requested -- Intentional usage since timezone is changed for this file.
$now = current_time( 'timestamp' );
// Create a few orders.
$orders_by_status = array( 'wc-completed' => 3, 'wc-pending' => 2 );
$orders_by_status = array(
'wc-completed' => 3,
'wc-pending' => 2,
);
$unpaid_ids = array();
foreach ( $orders_by_status as $order_status => $order_count ) {
foreach ( range( 1, $order_count ) as $_ ) {
@ -1228,6 +1234,7 @@ class OrdersTableDataStoreTests extends WC_Unit_Test_Case {
$order_1 = new WC_Order();
$order_1->set_billing_city( 'Fort Quality' );
$this->switch_data_store( $order_1, $this->sut );
$this->disable_cot_sync();
$order_1->save();
$product = new WC_Product_Simple();
@ -1258,13 +1265,42 @@ class OrdersTableDataStoreTests extends WC_Unit_Test_Case {
// Order 1's billing address references "Quality" and so does one of Order 2's order items.
$query = new OrdersTableQuery( array( 's' => 'Quality' ) );
$orders_array = $query->orders;
sort( $orders_array );
$this->assertEquals(
array( $order_1->get_id(), $order_2->get_id() ),
$query->orders,
$orders_array,
'Search terms match against address data as well as order item names.'
);
}
/**
* @testDox Ensure search works as expected on updated orders.
*/
public function test_cot_query_search_update() {
$order_1 = new WC_Order();
$this->switch_data_store( $order_1, $this->sut );
$this->disable_cot_sync();
$order_1->save();
$order_1->set_billing_city( 'New Cybertron' );
$order_1->save();
$order_2 = new WC_Order();
$this->switch_data_store( $order_2, $this->sut );
$order_2->save();
$order_2->set_billing_city( 'Gigantian City' );
$order_2->save();
$query = new OrdersTableQuery( array( 's' => 'Cybertron' ) );
$this->assertEquals(
array( $order_1->get_id() ),
$query->orders,
'Search terms match against updated address data.'
);
}
/**
* Test methods get_total_tax_refunded and get_total_shipping_refunded.
*/
@ -1374,7 +1410,7 @@ class OrdersTableDataStoreTests extends WC_Unit_Test_Case {
array(
'field' => 'order_key',
'value' => 'planck_1',
)
),
);
$query = new OrdersTableQuery( array( 'field_query' => $field_query ) );
$this->assertEqualsCanonicalizing( array( $order_ids[0], $order_ids[1] ), $query->orders );
@ -1408,8 +1444,8 @@ class OrdersTableDataStoreTests extends WC_Unit_Test_Case {
array(
'field' => 'order_key',
'value' => '[0-9]$',
'compare' => 'RLIKE'
)
'compare' => 'RLIKE',
),
);
$query = new OrdersTableQuery( array( 'field_query' => $field_query ) );
$this->assertEqualsCanonicalizing( $order_ids, $query->orders );
@ -1419,8 +1455,8 @@ class OrdersTableDataStoreTests extends WC_Unit_Test_Case {
array(
'field' => 'order_key',
'value' => '[^0-9]$',
'compare' => 'NOT RLIKE'
)
'compare' => 'NOT RLIKE',
),
);
$query = new OrdersTableQuery( array( 'field_query' => $field_query ) );
$this->assertCount( 0, $query->posts );
@ -1432,7 +1468,7 @@ class OrdersTableDataStoreTests extends WC_Unit_Test_Case {
'value' => '10.0',
'compare' => '<=',
'type' => 'NUMERIC',
)
),
);
$query = new OrdersTableQuery( array( 'field_query' => $field_query ) );
$this->assertEqualsCanonicalizing( array( $order_ids[2] ), $query->orders );
@ -1442,7 +1478,7 @@ class OrdersTableDataStoreTests extends WC_Unit_Test_Case {
array(
'field' => 'non_existing_field',
'value' => 'any-value',
)
),
);
$query = new OrdersTableQuery( array( 'field_query' => $field_query ) );
$this->assertCount( 0, $query->posts );
@ -1453,7 +1489,7 @@ class OrdersTableDataStoreTests extends WC_Unit_Test_Case {
'field' => 'wc_orders.total_amount',
'value' => 5.5,
'compare' => 'IN',
)
),
);
$query = new OrdersTableQuery( array( 'field_query' => $field_query ) );
$this->assertCount( 0, $query->posts );
@ -1464,7 +1500,7 @@ class OrdersTableDataStoreTests extends WC_Unit_Test_Case {
'field' => 'wc_orders.total_amount',
'value' => 10.0,
'compare' => 'EXOSTS',
)
),
);
$query = new OrdersTableQuery( array( 'field_query' => $field_query ) );
$this->assertCount( 0, $query->posts );
@ -1475,7 +1511,7 @@ class OrdersTableDataStoreTests extends WC_Unit_Test_Case {
'field' => 'total',
'compare' => 'BETWEEN',
'value' => 10.0,
)
),
);
$query = new OrdersTableQuery( array( 'field_query' => $field_query ) );
$this->assertCount( 0, $query->posts );
@ -1486,12 +1522,12 @@ class OrdersTableDataStoreTests extends WC_Unit_Test_Case {
'field' => 'total',
'compare' => 'NOT BETWEEN',
'value' => array( 1.0 ),
)
),
);
$query = new OrdersTableQuery( array( 'field_query' => $field_query ) );
$this->assertCount( 0, $query->posts );
// Test combinations of field_query with regular query args:
// Test combinations of field_query with regular query args.
$args = array(
'id' => array( $order_ids[0], $order_ids[1] ),
);
@ -1506,7 +1542,7 @@ class OrdersTableDataStoreTests extends WC_Unit_Test_Case {
array(
'field' => 'id',
'value' => $order_ids[1],
)
),
);
$query = new OrdersTableQuery( $args );
$this->assertEqualsCanonicalizing( array( $order_ids[1] ), $query->orders );
@ -1520,7 +1556,7 @@ class OrdersTableDataStoreTests extends WC_Unit_Test_Case {
$query = new OrdersTableQuery( $args );
$this->assertCount( 0, $query->orders );
// Now a more complex query with meta_query and date_query:
// Now a more complex query with meta_query and date_query.
$args = array(
'shipping_address' => 'The Universe',
'field_query' => array(
@ -1537,12 +1573,13 @@ class OrdersTableDataStoreTests extends WC_Unit_Test_Case {
$this->assertEqualsCanonicalizing( array( $order_ids[0], $order_ids[1] ), $query->orders );
// ... but only Planck is more than 80 years old.
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query -- Intentional usage for test.
$args['meta_query'] = array(
array(
'key' => 'customer_age',
'value' => 80,
'compare' => '>='
)
'compare' => '>=',
),
);
$query = new OrdersTableQuery( $args );
$this->assertEqualsCanonicalizing( array( $order_ids[1] ), $query->orders );