Multiple fixes for reading order data.

Some meta items cause issues when testing, such as having trailing zeros. This commit fixes, along with adding compatibility with WC < 2.6.
This commit is contained in:
vedanshujain 2022-05-04 14:09:06 +05:30
parent 37a41423fe
commit ad30c61463
5 changed files with 50 additions and 28 deletions

View File

@ -635,7 +635,7 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order {
* @throws WC_Data_Exception Exception may be thrown if value is invalid.
*/
public function set_discount_total( $value ) {
$this->set_prop( 'discount_total', wc_format_decimal( $value ) );
$this->set_prop( 'discount_total', wc_format_decimal( $value, false, true ) );
}
/**
@ -645,7 +645,7 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order {
* @throws WC_Data_Exception Exception may be thrown if value is invalid.
*/
public function set_discount_tax( $value ) {
$this->set_prop( 'discount_tax', wc_format_decimal( $value ) );
$this->set_prop( 'discount_tax', wc_format_decimal( $value, false, true ) );
}
/**
@ -655,7 +655,7 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order {
* @throws WC_Data_Exception Exception may be thrown if value is invalid.
*/
public function set_shipping_total( $value ) {
$this->set_prop( 'shipping_total', wc_format_decimal( $value ) );
$this->set_prop( 'shipping_total', wc_format_decimal( $value, false, true ) );
}
/**
@ -665,7 +665,7 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order {
* @throws WC_Data_Exception Exception may be thrown if value is invalid.
*/
public function set_shipping_tax( $value ) {
$this->set_prop( 'shipping_tax', wc_format_decimal( $value ) );
$this->set_prop( 'shipping_tax', wc_format_decimal( $value, false, true ) );
$this->set_total_tax( (float) $this->get_cart_tax() + (float) $this->get_shipping_tax() );
}
@ -676,7 +676,7 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order {
* @throws WC_Data_Exception Exception may be thrown if value is invalid.
*/
public function set_cart_tax( $value ) {
$this->set_prop( 'cart_tax', wc_format_decimal( $value ) );
$this->set_prop( 'cart_tax', wc_format_decimal( $value, false, true ) );
$this->set_total_tax( (float) $this->get_cart_tax() + (float) $this->get_shipping_tax() );
}

View File

@ -115,10 +115,18 @@ class PostToOrderOpTableMigrator extends MetaToCustomTableMigrator {
'type' => 'date_epoch',
'destination' => 'date_paid_gmt',
),
'_paid_date' => array( // For compatibility with WC < 2.6
'type' => 'date',
'destination' => 'date_paid_gmt'
),
'_date_completed' => array(
'type' => 'date_epoch',
'destination' => 'date_completed_gmt',
),
'_completed_date' => array( // For compatibility with WC < 2.6
'type' => 'date',
'destination' => 'date_completed_gmt',
),
'_order_shipping_tax' => array(
'type' => 'decimal',
'destination' => 'shipping_tax_amount',

View File

@ -311,7 +311,7 @@ class OrdersTableDataStore extends \Abstract_WC_Order_Data_Store_CPT implements
),
'discount_total_amount' => array(
'type' => 'decimal',
'name' => 'discount_total_amount',
'name' => 'discount_total',
),
'recorded_sales' => array( 'type' => 'bool' ),
);
@ -450,7 +450,7 @@ class OrdersTableDataStore extends \Abstract_WC_Order_Data_Store_CPT implements
* @param bool $set True or false.
*/
public function set_email_sent( $order, $set ) {
return $order->update_meta_data( '_new_order_email_sent', wc_string_to_bool( $set ) );
return $order->update_meta_data( '_new_order_email_sent', wc_bool_to_string( $set ) );
}
/**
@ -559,10 +559,7 @@ class OrdersTableDataStore extends \Abstract_WC_Order_Data_Store_CPT implements
if ( ! $order->get_id() ) {
throw new \Exception( __( 'ID must be set for an order to be read', 'woocommerce' ) );
}
$meta_data = $this->read_meta( $order );
foreach ( $meta_data as $row ) {
$order->add_meta_data( $row->meta_key, $row->meta_value, false );
}
$order->read_meta_data();
$order_data = $this->get_order_data_for_id( $order->get_id() );
foreach ( $this->get_all_order_column_mappings() as $table_name => $column_mapping ) {
foreach ( $column_mapping as $column_name => $prop_details ) {
@ -595,10 +592,10 @@ class OrdersTableDataStore extends \Abstract_WC_Order_Data_Store_CPT implements
$raw_meta_data = $wpdb->get_results(
$wpdb->prepare(
"
SELECT id, meta_key, meta_value
SELECT id as meta_id, meta_key, meta_value
FROM $meta_table
WHERE order_id = %d
ORDER BY id;
ORDER BY meta_id;
",
$order->get_id()
)

View File

@ -1,4 +1,5 @@
<?php // phpcs:ignore
/**
* Orders helper.
*
@ -47,13 +48,13 @@ class OrderHelper {
/**
* Create a order.
*
* @since 2.4
* @version 3.0 New parameter $product.
*
* @param int $customer_id Customer ID.
* @param WC_Product $product Product object.
* @param int $customer_id Customer ID.
* @param WC_Product $product Product object.
*
* @return WC_Order WC_Order object.
* @version 3.0 New parameter $product.
*
* @since 2.4
*/
public static function create_order( $customer_id = 1, $product = null ) {
@ -132,6 +133,20 @@ class OrderHelper {
return $order;
}
/**
* Helper method to drop custom tables if present.
*/
public static function delete_order_custom_tables() {
$order_table_controller = wc_get_container()
->get( CustomOrdersTableController::class );
$order_table_controller->show_feature();
$synchronizer = wc_get_container()
->get( DataSynchronizer::class );
if ( $synchronizer->check_orders_table_exists() ) {
$synchronizer->delete_database_tables();
}
}
/**
* Helper method to create custom tables if not present.
*/

View File

@ -35,13 +35,18 @@ class OrdersTableDataStoreTests extends WC_Unit_Test_Case {
// Remove the Test Suites use of temporary tables https://wordpress.stackexchange.com/a/220308.
remove_filter( 'query', array( $this, '_create_temporary_tables' ) );
remove_filter( 'query', array( $this, '_drop_temporary_tables' ) );
OrderHelper::delete_order_custom_tables(); // We need this since non-temporary tables won't drop automatically.
OrderHelper::create_order_custom_table_if_not_exist();
$this->sut = wc_get_container()->get( OrdersTableDataStore::class );
$this->migrator = wc_get_container()->get( PostsToOrdersMigrationController::class );
$this->cpt_data_store = new WC_Order_Data_Store_CPT();
}
public function tearDown(): void {
// Add back removed filter.
add_filter( 'query', array( $this, '_create_temporary_tables' ) );
add_filter( 'query', array( $this, '_drop_temporary_tables' ) );
parent::tearDown();
}
/**
@ -51,24 +56,21 @@ class OrdersTableDataStoreTests extends WC_Unit_Test_Case {
$post_order_id = OrderHelper::create_complex_wp_post_order();
$this->migrator->migrate_orders( array( $post_order_id ) );
$switch_to_data_store = function ( $data_store ) {
$this->data_store = $data_store;
};
$cot_order = new WC_Order();
$cot_order->set_id( $post_order_id );
$switch_to_data_store->call( $cot_order, $this->sut );
$this->sut->read( $cot_order );
$post_order = new WC_Order();
$post_order->set_id( $post_order_id );
$switch_to_data_store->call( $post_order, $this->cpt_data_store );
$this->cpt_data_store->read( $post_order );
$post_order_data = $post_order->get_data();
$string_to_num_keys = array( 'discount_total', 'discount_tax', 'shipping_total', 'shipping_tax', 'cart_tax' );
array_walk(
$post_order_data,
function ( &$data, $key ) use ( $string_to_num_keys ) {
if ( in_array( $key, $string_to_num_keys, true ) ) {
$data = (float) $data;
}
}
);
$this->assertEquals( $post_order_data, $cot_order->get_data() );
}
@ -84,7 +86,7 @@ class OrdersTableDataStoreTests extends WC_Unit_Test_Case {
$post_meta_data = get_post_meta( $post_order_id );
// TODO: Remove `_recorded_sales` from exempted keys after https://github.com/woocommerce/woocommerce/issues/32843.
$exempted_keys = array( 'post_modified', 'post_modified_gmt', '_recorded_sales' );
$convert_to_float_keys = array( '_cart_discount_tax', '_order_shipping', '_order_shipping_tax', '_order_tax' );
$convert_to_float_keys = array( '_cart_discount_tax', '_order_shipping', '_order_shipping_tax', '_order_tax', '_cart_discount', 'cart_tax' );
$exempted_keys = array_flip( array_merge( $exempted_keys, $convert_to_float_keys ) );
$post_data_float = array_intersect_key( $post_data, array_flip( $convert_to_float_keys ) );