diff --git a/plugins/woocommerce/src/Internal/DataStores/Orders/OrdersTableDataStore.php b/plugins/woocommerce/src/Internal/DataStores/Orders/OrdersTableDataStore.php index 41f912bd3fa..d76c1b8a358 100644 --- a/plugins/woocommerce/src/Internal/DataStores/Orders/OrdersTableDataStore.php +++ b/plugins/woocommerce/src/Internal/DataStores/Orders/OrdersTableDataStore.php @@ -2358,10 +2358,10 @@ FROM $order_meta_table $order->set_date_modified( current_time( 'mysql' ) ); } - $this->update_order_meta( $order ); - $this->persist_order_to_db( $order, $force_all_fields ); + $this->update_order_meta( $order ); + $order->save_meta_data(); $order->apply_changes(); diff --git a/plugins/woocommerce/src/Internal/DataStores/Orders/OrdersTableRefundDataStore.php b/plugins/woocommerce/src/Internal/DataStores/Orders/OrdersTableRefundDataStore.php index fab7f3640f7..6d532a93ce3 100644 --- a/plugins/woocommerce/src/Internal/DataStores/Orders/OrdersTableRefundDataStore.php +++ b/plugins/woocommerce/src/Internal/DataStores/Orders/OrdersTableRefundDataStore.php @@ -6,6 +6,8 @@ namespace Automattic\WooCommerce\Internal\DataStores\Orders; +use WC_Meta_Data; + /** * Class OrdersTableRefundDataStore. */ @@ -159,8 +161,17 @@ class OrdersTableRefundDataStore extends OrdersTableDataStore { $props_to_update = $this->get_props_to_update( $refund, $meta_key_to_props ); foreach ( $props_to_update as $meta_key => $prop ) { - $value = $refund->{"get_$prop"}( 'edit' ); - $refund->update_meta_data( $meta_key, $value ); + $meta_object = new WC_Meta_Data(); + $meta_object->key = $meta_key; + $meta_object->value = $refund->{"get_$prop"}( 'edit' ); + $existing_meta = $this->data_store_meta->get_metadata_by_key( $refund, $meta_key ); + if ( $existing_meta ) { + $existing_meta = $existing_meta[0]; + $meta_object->id = $existing_meta->id; + $this->update_meta( $refund, $meta_object ); + } else { + $this->add_meta( $refund, $meta_object ); + } $updated_props[] = $prop; } diff --git a/plugins/woocommerce/tests/php/src/Internal/DataStores/Orders/OrdersTableRefundDataStoreTests.php b/plugins/woocommerce/tests/php/src/Internal/DataStores/Orders/OrdersTableRefundDataStoreTests.php index f82c62b45bc..e3d00bd5fb8 100644 --- a/plugins/woocommerce/tests/php/src/Internal/DataStores/Orders/OrdersTableRefundDataStoreTests.php +++ b/plugins/woocommerce/tests/php/src/Internal/DataStores/Orders/OrdersTableRefundDataStoreTests.php @@ -102,10 +102,15 @@ class OrdersTableRefundDataStoreTests extends WC_Unit_Test_Case { } /** - * @testDox Test that refund props are set as expected. + * @testDox Test that refund props are set as expected with HPOS enabled. */ public function test_refund_data_is_set() { - $order = OrderHelper::create_order(); + $this->toggle_cot_feature_and_usage( true ); + + $order = OrderHelper::create_order(); + $user = $this->factory()->user->create_and_get( array( 'role' => 'administrator' ) ); + wp_set_current_user( $user->ID ); + $refund = wc_create_refund( array( 'order_id' => $order->get_id(), @@ -119,6 +124,7 @@ class OrdersTableRefundDataStoreTests extends WC_Unit_Test_Case { $this->assertEquals( $refund->get_id(), $refreshed_refund->get_id() ); $this->assertEquals( 10, $refreshed_refund->get_data()['amount'] ); $this->assertEquals( 'Test', $refreshed_refund->get_data()['reason'] ); + $this->assertEquals( $user->ID, $refreshed_refund->get_data()['refunded_by'] ); } }