Remove address indexes from list of internal keys. (#35192)

* Override filter_meta_data method, since it should be a no-op anyway.

* Add changelog.

* Not include address indexes from filtered data.

* Applied coding standards.
This commit is contained in:
Vedanshu Jain 2022-10-28 00:06:50 +05:30 committed by GitHub
parent 8d73a03f97
commit ec9ef7458e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 61 additions and 0 deletions

View File

@ -0,0 +1,4 @@
Significance: patch
Type: fix
Override filter_meta_data method, since it should be a no-op anyway.

View File

@ -1031,6 +1031,35 @@ WHERE
$order->set_object_read( true );
}
/**
* For post based data stores, this was used to filter internal meta data. For custom tables, technically there is no internal meta data,
* (i.e. we store all core data as properties for the order, and not in meta data). So this method is a no-op.
*
* Except that some meta such as billing_address_index and shipping_address_index are infact stored in meta data, so we need to filter those out.
*
* However, declaring $internal_meta_keys is still required so that our backfill and other comparison checks works as expected.
*
* @param \WC_Data $object Object to filter meta data for.
* @param array $raw_meta_data Raw meta data.
*
* @return array Filtered meta data.
*/
public function filter_raw_meta_data( &$object, $raw_meta_data ) {
$filtered_meta_data = parent::filter_raw_meta_data( $object, $raw_meta_data );
$allowed_keys = array(
'_billing_address_index',
'_shipping_address_index',
);
$allowed_meta = array_filter(
$raw_meta_data,
function( $meta ) use ( $allowed_keys ) {
return in_array( $meta->meta_key, $allowed_keys, true );
}
);
return array_merge( $allowed_meta, $filtered_meta_data );
}
/**
* Sync order to/from posts tables if we are able to detect difference between order and posts but the sync is enabled.
*

View File

@ -1867,4 +1867,32 @@ class OrdersTableDataStoreTests extends WC_Unit_Test_Case {
$product->save();
$this->assertFalse( wc_get_order( $product->get_id() ) );
}
/**
* @testDox Test that we are not duplicating address indexing when updating.
*/
public function test_address_index_saved_on_update() {
global $wpdb;
$this->toggle_cot( true );
$this->disable_cot_sync();
$order = new WC_Order();
$order->set_billing_address_1( '123 Main St' );
$order->save();
$this->assertTrue( false !== strpos( $order->get_meta( '_billing_address_index', true ), '123 Main St' ) );
$order = wc_get_order( $order->get_id() );
$order->set_billing_address_2( 'Apt 1' );
$order->save();
$order_meta_table = $this->sut::get_meta_table_name();
// Assert that we are not duplicating address indexes.
$result = $wpdb->get_var(
$wpdb->prepare(
"SELECT COUNT(*) FROM {$order_meta_table} WHERE order_id = %d AND meta_key = '_billing_address_index'", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
$order->get_id()
)
);
$this->assertEquals( 1, $result );
}
}