Refresh order to account for DB changes from post hooks.

This commit is contained in:
Vedanshu Jain 2023-07-31 16:05:06 +05:30
parent 221a0768cc
commit 294a3f19b9
2 changed files with 29 additions and 2 deletions

View File

@ -571,7 +571,6 @@ class OrdersTableDataStore extends \Abstract_WC_Order_Data_Store_CPT implements
self::$backfilling_order_ids[] = $order->get_id();
$cpt_data_store->update_order_from_object( $order );
self::$backfilling_order_ids = array_diff( self::$backfilling_order_ids, array( $order->get_id() ) );
foreach ( $cpt_data_store->get_internal_data_store_key_getters() as $key => $getter_name ) {
if (
is_callable( array( $cpt_data_store, "set_$getter_name" ) ) &&
@ -589,6 +588,7 @@ class OrdersTableDataStore extends \Abstract_WC_Order_Data_Store_CPT implements
);
}
}
self::$backfilling_order_ids = array_diff( self::$backfilling_order_ids, array( $order->get_id() ) );
}
/**
@ -1668,7 +1668,8 @@ FROM $order_meta_table
'post_type' => $data_sync->data_sync_is_enabled() ? $order->get_type() : $data_sync::PLACEHOLDER_ORDER_POST_TYPE,
'post_status' => 'draft',
'post_parent' => $order->get_changes()['parent_id'] ?? $order->get_data()['parent_id'] ?? 0,
'post_date_gmt' => current_time( 'mysql', 1 ), // We set the date to prevent invalid date errors when using MySQL strict mode.
'post_date' => gmdate( 'Y-m-d H:i:s', $order->get_date_created( 'edit' )->getOffsetTimestamp() ),
'post_date_gmt' => gmdate( 'Y-m-d H:i:s', $order->get_date_created( 'edit' )->getTimestamp() ),
)
);
@ -2309,7 +2310,10 @@ FROM $order_meta_table
$order->apply_changes();
if ( $backfill ) {
self::$backfilling_order_ids[] = $order->get_id();
$order = wc_get_order( $order->get_id() ); // Refresh order to account for DB changes from post hooks.
$this->maybe_backfill_post_record( $order );
self::$backfilling_order_ids = array_diff( self::$backfilling_order_ids, array( $order->get_id() ) );
}
$this->clear_caches( $order );

View File

@ -1276,6 +1276,7 @@ class OrdersTableDataStoreTests extends HposTestCase {
* @testDox Test that we are able to correctly detect when order and post are out of sync.
*/
public function test_is_post_different_from_order() {
$this->toggle_cot_feature_and_usage( true );
$this->enable_cot_sync();
$order = $this->create_complex_cot_order();
$post_order_comparison_closure = function ( $order ) {
@ -2564,6 +2565,28 @@ class OrdersTableDataStoreTests extends HposTestCase {
$order->save_meta_data();
$order->add_meta_data( 'test_key_3', 'test_value_3' );
$order->save();
echo 'ehere';
}
}
/**
* @testDox When creating a new order, test that we are not backfilling stale data when there is a postmeta hooks that modifies data on the order.
*/
public function test_backfill_does_not_trigger_when_creating_orders_with_filter() {
$this->toggle_cot_feature_and_usage( true );
$this->enable_cot_sync();
add_filter( 'added_post_meta', array( $this, 'add_meta_when_meta_added' ), 10, 4 );
$order = new WC_Order();
$order->set_customer_id( 1 );
$order->add_meta_data( 'test_key', 'test_value' );
$order->save();
$r_order = wc_get_order( $order->get_id() );
$this->assertEquals( 'test_value', $r_order->get_meta( 'test_key', true ) );
$this->assertEquals( 'test_value_2', $r_order->get_meta( 'test_key_2', true ) );
$this->assertEquals( 'test_value_3', $r_order->get_meta( 'test_key_3', true ) );
$this->assertEquals( 1, $r_order->get_customer_id() );
remove_filter( 'added_post_meta', array( $this, 'add_meta_when_meta_added' ) );
}
}