Synchronize order meta data (between HPOS and CPT stores) upon a call to $order->save_meta_data.

This commit is contained in:
barryhughes 2023-01-24 17:34:13 -08:00 committed by Jorge A. Torres
parent 562749b60a
commit ed511dbb76
4 changed files with 86 additions and 4 deletions

View File

@ -0,0 +1,4 @@
Significance: patch
Type: fix
When order meta data is saved via HPOS, it should be backfilled to the CPT data store.

View File

@ -78,6 +78,8 @@ abstract class CustomMetaDataStore {
*
* @param WC_Data $object WC_Data object.
* @param stdClass $meta (containing at least ->id).
*
* @return bool
*/
public function delete_meta( &$object, $meta ) {
global $wpdb;
@ -127,6 +129,8 @@ abstract class CustomMetaDataStore {
*
* @param WC_Data $object WC_Data object.
* @param stdClass $meta (containing ->id, ->key and ->value).
*
* @return bool
*/
public function update_meta( &$object, $meta ) {
global $wpdb;

View File

@ -11,6 +11,7 @@ use Automattic\WooCommerce\Internal\Utilities\DatabaseUtil;
use Automattic\WooCommerce\Proxies\LegacyProxy;
use Automattic\WooCommerce\Utilities\ArrayUtil;
use Exception;
use WC_Abstract_Order;
use WC_Data;
use WC_Order;
@ -2457,9 +2458,17 @@ CREATE TABLE $meta_table (
*
* @param WC_Data $object WC_Data object.
* @param stdClass $meta (containing at least ->id).
*
* @return bool
*/
public function delete_meta( &$object, $meta ) {
return $this->data_store_meta->delete_meta( $object, $meta );
$delete_meta = $this->data_store_meta->delete_meta( $object, $meta );
if ( $object instanceof WC_Abstract_Order ) {
$this->maybe_backfill_post_record( $object );
}
return $delete_meta;
}
/**
@ -2467,10 +2476,17 @@ CREATE TABLE $meta_table (
*
* @param WC_Data $object WC_Data object.
* @param stdClass $meta (containing ->key and ->value).
* @return int meta ID
*
* @return int|bool meta ID or false on failure
*/
public function add_meta( &$object, $meta ) {
return $this->data_store_meta->add_meta( $object, $meta );
$add_meta = $this->data_store_meta->add_meta( $object, $meta );
if ( $object instanceof WC_Abstract_Order ) {
$this->maybe_backfill_post_record( $object );
}
return $add_meta;
}
/**
@ -2478,8 +2494,16 @@ CREATE TABLE $meta_table (
*
* @param WC_Data $object WC_Data object.
* @param stdClass $meta (containing ->id, ->key and ->value).
*
* @return bool
*/
public function update_meta( &$object, $meta ) {
return $this->data_store_meta->update_meta( $object, $meta );
$update_meta = $this->data_store_meta->update_meta( $object, $meta );
if ( $object instanceof WC_Abstract_Order ) {
$this->maybe_backfill_post_record( $object );
}
return $update_meta;
}
}

View File

@ -223,4 +223,54 @@ class DataSynchronizerTests extends WC_Unit_Test_Case {
'After the COT order record was deleted, the order was also deleted from the posts table.'
);
}
/**
* When sync is enabled, changes to meta data should propoagate from the Custom Orders Table to
* the post meta table whenever the order object's save_meta_data() method is called.
*
* @return void
*/
public function test_meta_data_changes_propagate_from_cot_to_cpt(): void {
// Sync enabled and COT authoritative.
update_option( $this->sut::ORDERS_DATA_SYNC_ENABLED_OPTION, 'yes' );
update_option( CustomOrdersTableController::CUSTOM_ORDERS_TABLE_USAGE_ENABLED_OPTION, 'yes' );
$order = OrderHelper::create_order();
$order->add_meta_data( 'foo', 'bar' );
$order->save_meta_data();
update_option( CustomOrdersTableController::CUSTOM_ORDERS_TABLE_USAGE_ENABLED_OPTION, 'no' );
$refreshed_order = wc_get_order( $order->get_id() );
$this->assertEquals(
$refreshed_order->get_meta( 'foo' ),
'bar',
'Meta data persisted via the HPOS datastore is accessible via the CPT datastore'
);
}
/**
* When sync is enabled, changes to meta data should propoagate from the post meta table to
* the Custom Orders Table whenever the order object's save_meta_data() method is called.
*
* @return void
*/
public function test_meta_data_changes_propagate_from_cpt_to_cot(): void {
// Sync enabled and COT authoritative.
update_option( $this->sut::ORDERS_DATA_SYNC_ENABLED_OPTION, 'yes' );
update_option( CustomOrdersTableController::CUSTOM_ORDERS_TABLE_USAGE_ENABLED_OPTION, 'no' );
$order = OrderHelper::create_order();
$order->add_meta_data( 'foo', 'bar' );
$order->save_meta_data();
update_option( CustomOrdersTableController::CUSTOM_ORDERS_TABLE_USAGE_ENABLED_OPTION, 'yes' );
$refreshed_order = wc_get_order( $order->get_id() );
$this->assertEquals(
$refreshed_order->get_meta( 'foo' ),
'bar',
'Meta data persisted via the CPT datastore is accessible via the HPOS datastore'
);
}
}