Convert null and '' to 0 before verification. (#33453)
During migration $wpdb->prepare would force null and empty values to be zero for %f placeholder. This was causing verification logic to fail, which is being addressed in this commit. The alternative was to insert null values without running them via $wpdb->prepare, but that seemed less safer than converting to zero because it would have to done manually since $wpdb->prepare wouldn't support it.
This commit is contained in:
parent
71456382c4
commit
87ccf73da7
|
@ -0,0 +1,4 @@
|
|||
Significance: patch
|
||||
Type: fix
|
||||
|
||||
Convert null and '' to 0 before verification to mimic $wpdb->prepare.
|
|
@ -563,7 +563,7 @@ WHERE
|
|||
private function validate_data( $value, string $type ) {
|
||||
switch ( $type ) {
|
||||
case 'decimal':
|
||||
$value = (float) $value;
|
||||
$value = wc_format_decimal( $value, false, true );
|
||||
break;
|
||||
case 'int':
|
||||
$value = (int) $value;
|
||||
|
@ -778,6 +778,9 @@ WHERE $where_clause
|
|||
*/
|
||||
private function pre_process_row( $row, $schema, $alias, $destination_alias ) {
|
||||
if ( in_array( $schema['type'], array( 'int', 'decimal' ), true ) ) {
|
||||
if ( '' === $row[ $alias ] || null === $row[ $alias ] ) {
|
||||
$row[ $alias ] = 0; // $wpdb->prepare forces empty values to 0.
|
||||
}
|
||||
$row[ $alias ] = wc_format_decimal( $row[ $alias ], false, true );
|
||||
$row[ $destination_alias ] = wc_format_decimal( $row[ $destination_alias ], false, true );
|
||||
}
|
||||
|
|
|
@ -726,4 +726,22 @@ WHERE order_id = {$order_id} AND meta_key = 'non_unique_key_1' AND meta_value in
|
|||
|
||||
return $wpdb_mock;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that orders are migrated and verified without errors.
|
||||
*/
|
||||
public function test_verify_migrated_orders() {
|
||||
$order = wc_get_order( OrderHelper::create_complex_wp_post_order() );
|
||||
$this->clear_all_orders();
|
||||
|
||||
// Additional test to assert null values are converted properly.
|
||||
delete_post_meta( $order->get_id(), '_cart_discount_tax' );
|
||||
|
||||
$this->assertEquals( '', get_post_meta( $order->get_id(), '_cart_discount_tax', true ) );
|
||||
|
||||
$this->sut->migrate_order( $order->get_id() );
|
||||
$errors = $this->sut->verify_migrated_orders( array( $order->get_id() ) );
|
||||
|
||||
$this->assertEmpty( $errors );
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue