diff --git a/plugins/woocommerce/changelog/fix-33147 b/plugins/woocommerce/changelog/fix-33147 new file mode 100644 index 00000000000..7f81a38c793 --- /dev/null +++ b/plugins/woocommerce/changelog/fix-33147 @@ -0,0 +1,4 @@ +Significance: patch +Type: fix + +Convert null and '' to 0 before verification to mimic $wpdb->prepare. diff --git a/plugins/woocommerce/src/Database/Migrations/MetaToCustomTableMigrator.php b/plugins/woocommerce/src/Database/Migrations/MetaToCustomTableMigrator.php index 0037375f28c..728cdaa70bb 100644 --- a/plugins/woocommerce/src/Database/Migrations/MetaToCustomTableMigrator.php +++ b/plugins/woocommerce/src/Database/Migrations/MetaToCustomTableMigrator.php @@ -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 ); } diff --git a/plugins/woocommerce/tests/php/src/Database/Migrations/CustomOrderTable/PostsToOrdersMigrationControllerTest.php b/plugins/woocommerce/tests/php/src/Database/Migrations/CustomOrderTable/PostsToOrdersMigrationControllerTest.php index f4948390a6c..49ac176aebf 100644 --- a/plugins/woocommerce/tests/php/src/Database/Migrations/CustomOrderTable/PostsToOrdersMigrationControllerTest.php +++ b/plugins/woocommerce/tests/php/src/Database/Migrations/CustomOrderTable/PostsToOrdersMigrationControllerTest.php @@ -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 ); + } }