Skip to content
This repository has been archived by the owner on Jun 15, 2022. It is now read-only.

Commit

Permalink
If wc_get_order() fails, skip it in the migration.
Browse files Browse the repository at this point in the history
Based on @zacscott's work in #44, add a test case and a simpler work-around for handling corrupted orders.

Fixes #43, closes #44. Props @zacscott.
  • Loading branch information
stevegrunwell committed Feb 15, 2018
1 parent 7923624 commit 8e87bc4
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 10 deletions.
33 changes: 23 additions & 10 deletions includes/class-woocommerce-custom-orders-table-cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,19 +89,32 @@ public function migrate( $args = array(), $assoc_args = array() ) {
array_merge( $order_types, array( $assoc_args['batch-size'] ) )
);
$order_data = $wpdb->get_col( $order_query ); // WPCS: Unprepared SQL ok, DB call ok.
$skipped = array();

while ( ! empty( $order_data ) ) {
while ( ! empty( array_diff( $order_data, $skipped ) ) ) {
foreach ( $order_data as $order_id ) {
$order = wc_get_order( $order_id );
$result = $order->get_data_store()->populate_from_meta( $order );

if ( is_wp_error( $result ) ) {
return WP_CLI::error( sprintf(
/* Translators: %1$d is the order ID, %2$s is the error message. */
'A database error occurred while migrating order %1$d: %2$s.',
$order_id,
$result->get_error_message()
$order = wc_get_order( $order_id );

if ( false === $order ) {
$skipped[] = $order_id;

WP_CLI::warning( sprintf(
/* Translators: %1$d is the order ID. */
__( 'Unable to retrieve order with ID %1$d', 'woocommerce-custom-orders-table' ),
$order_id
) );

} else {
$result = $order->get_data_store()->populate_from_meta( $order );

if ( is_wp_error( $result ) ) {
return WP_CLI::error( sprintf(
/* Translators: %1$d is the order ID, %2$s is the error message. */
'A database error occurred while migrating order %1$d: %2$s.',
$order_id,
$result->get_error_message()
) );
}
}

$processed++;
Expand Down
21 changes: 21 additions & 0 deletions tests/test-cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,28 @@ public function test_migrate_stops_on_database_error() {

$error = array_pop( WP_CLI::$__logger );
$this->assertEquals( 'error', $error['level'], 'Expected to see a call to WP_CLI::error().' );
}

/**
* @link https://github.com/liquidweb/woocommerce-custom-orders-table/issues/43
*/
public function test_migrate_handles_errors_with_wc_get_order() {
$this->toggle_use_custom_table( false );
$order_ids = $this->generate_orders( 3 );
$this->toggle_use_custom_table( true );

// For the first item, cause wc_get_order() to break due to a non-existent class.
add_filter( 'woocommerce_order_class', function ( $classname, $order_type, $order_id ) use ( $order_ids ) {
return (int) $order_id === $order_ids[0] ? 'SomeNonExistentClassName' : $classname;
}, 10, 3 );

$this->cli->migrate();

$this->assertEquals(
2,
$this->count_orders_in_table_with_ids( $order_ids ),
'Expected to only see two orders in the custom table.'
);
}

public function test_backfill() {
Expand Down

0 comments on commit 8e87bc4

Please sign in to comment.