HPOS: Ensure refund meta data is saved correctly (alternative approach) (#39700)

This commit is contained in:
Barry Hughes 2023-08-17 16:35:57 -07:00 committed by GitHub
commit 3ed285c7c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 6 deletions

View File

@ -0,0 +1,4 @@
Significance: minor
Type: fix
Ensure refund meta data is saved correctly when HPOS is enabled.

View File

@ -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();

View File

@ -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;
}

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() {
$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'] );
}
}