Update refund meta directly instead of using setters

This is an alternative to #39466, as a way to ensure that refund meta
data gets saved correctly.

Fixes #39215
This commit is contained in:
Corey McKrill 2023-08-11 13:56:17 -07:00
parent 2afba8b8fe
commit 11a31e8b64
No known key found for this signature in database
GPG Key ID: 84BBFE669C4D97B8
3 changed files with 23 additions and 6 deletions

View File

@ -2358,10 +2358,10 @@ FROM $order_meta_table
$order->set_date_modified( current_time( 'mysql' ) ); $order->set_date_modified( current_time( 'mysql' ) );
} }
$this->update_order_meta( $order );
$this->persist_order_to_db( $order, $force_all_fields ); $this->persist_order_to_db( $order, $force_all_fields );
$this->update_order_meta( $order );
$order->save_meta_data(); $order->save_meta_data();
$order->apply_changes(); $order->apply_changes();

View File

@ -6,6 +6,8 @@
namespace Automattic\WooCommerce\Internal\DataStores\Orders; namespace Automattic\WooCommerce\Internal\DataStores\Orders;
use WC_Meta_Data;
/** /**
* Class OrdersTableRefundDataStore. * Class OrdersTableRefundDataStore.
*/ */
@ -159,8 +161,17 @@ class OrdersTableRefundDataStore extends OrdersTableDataStore {
$props_to_update = $this->get_props_to_update( $refund, $meta_key_to_props ); $props_to_update = $this->get_props_to_update( $refund, $meta_key_to_props );
foreach ( $props_to_update as $meta_key => $prop ) { foreach ( $props_to_update as $meta_key => $prop ) {
$value = $refund->{"get_$prop"}( 'edit' ); $meta_object = new WC_Meta_Data();
$refund->update_meta_data( $meta_key, $value ); $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; $updated_props[] = $prop;
} }

View File

@ -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() { 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( $refund = wc_create_refund(
array( array(
'order_id' => $order->get_id(), '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( $refund->get_id(), $refreshed_refund->get_id() );
$this->assertEquals( 10, $refreshed_refund->get_data()['amount'] ); $this->assertEquals( 10, $refreshed_refund->get_data()['amount'] );
$this->assertEquals( 'Test', $refreshed_refund->get_data()['reason'] ); $this->assertEquals( 'Test', $refreshed_refund->get_data()['reason'] );
$this->assertEquals( $user->ID, $refreshed_refund->get_data()['refunded_by'] );
} }
} }