Automattic\WooCommerce\Database\Migrations\CustomOrderTable
CLIRunner::backfill
Backfills an order from either the HPOS or the posts datastore.
OPTIONS
- <order_id>
- The ID of the order.
- --from=<datastore>
- Source datastore. Either
'hpos'or'posts'.
--- options:
- hpos
- posts
- --to=<datastore>
- Destination datastore. Either
'hpos'or'posts'.
--- options:
- hpos
- posts
- [--meta_keys=<meta_keys>]
- Comma-separated list of meta keys to backfill.
- [--props=<props>]
- Comma-separated list of order properties to backfill.
Method of the class: CLIRunner{}
No Hooks.
Returns
null. Nothing (null).
Usage
$CLIRunner = new CLIRunner(); $CLIRunner->backfill( $args, $assoc_args );
- $args(array)
- Positional arguments passed to the command.
Default:array() - $assoc_args(array)
- Associative arguments (options) passed to the command.
Default:array()
Changelog
| Since 8.6.0 | Introduced. |
CLIRunner::backfill() CLIRunner::backfill code WC 10.5.0
public function backfill( array $args = array(), array $assoc_args = array() ) {
$legacy_handler = wc_get_container()->get( LegacyDataHandler::class );
$from = $assoc_args['from'] ?? '';
$to = $assoc_args['to'] ?? '';
$order_id = absint( $args[0] );
if ( ! $order_id ) {
WP_CLI::error( __( 'Please provide a valid order ID.', 'woocommerce' ) );
}
foreach ( array( 'from', 'to' ) as $datastore ) {
if ( ! in_array( ${"$datastore"}, array( 'posts', 'hpos' ), true ) ) {
// translators: %s is a shell argument representing a datastore name.
WP_CLI::error( sprintf( __( '\'%s\' is not a valid datastore.', 'woocommerce' ), ${"$datastore"} ) );
}
}
if ( $from === $to ) {
WP_CLI::error( __( 'Please use different source (--from) and destination (--to) datastores.', 'woocommerce' ) );
}
$fields = array_intersect_key( $assoc_args, array_flip( array( 'meta_keys', 'props' ) ) );
foreach ( $fields as &$field_names ) {
$field_names = is_string( $field_names ) ? array_map( 'trim', explode( ',', $field_names ) ) : $field_names;
$field_names = array_unique( array_filter( array_filter( $field_names, 'is_string' ) ) );
}
try {
$legacy_handler->backfill_order_to_datastore( $order_id, $from, $to, $fields );
} catch ( \Exception $e ) {
WP_CLI::error(
sprintf(
// translators: %1$d is an order ID, %2$s and %3$s are datastore names, %4$s is an error message.
__( 'An error occurred while backfilling order %1$d from %2$s to %3$s: %4$s', 'woocommerce' ),
$order_id,
$from,
$to,
$e->getMessage()
)
);
}
WP_CLI::success(
sprintf(
// translators: %1$d is an order ID, %2$s and %3$s are datastore names ("hpos" or "posts" for example).
__( 'Order %1$d backfilled from %2$s to %3$s.', 'woocommerce' ),
$order_id,
$from,
$to
)
);
}