Automattic\WooCommerce\Database\Migrations\CustomOrderTable

CLIRunner::cleanup_post_data()publicWC 1.0

When HPOS is enabled, this command lets you remove redundant data from the postmeta table for migrated orders.

OPTIONS

<all|id|range>...
ID or range of orders to clean up.
[--batch-size=<batch-size>]
Number of orders to process per batch. Applies only to cleaning up of 'all' orders.
--- default: 500
---
[--force]
When true, post meta will be cleaned up even if the post appears to have been updated more recently than the order.
--- default: false
---

EXAMPLES

# Cleanup post data for order 314.
$ wp wc hpos cleanup 314
# Cleanup postmeta for orders with IDs between 10 and 100 and order 314.
$ wp wc hpos cleanup 10-100 314
# Cleanup postmeta for all orders.
wp wc hpos cleanup all
# Cleanup postmeta for all orders with a batch size of 200 (instead of the default 500).
wp wc hpos cleanup all --batch-size=200

Method of the class: CLIRunner{}

No Hooks.

Return

null. Nothing (null).

Usage

$CLIRunner = new CLIRunner();
$CLIRunner->cleanup_post_data( $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()

CLIRunner::cleanup_post_data() code WC 9.7.1

public function cleanup_post_data( array $args = array(), array $assoc_args = array() ) {
	if ( ! $this->synchronizer->custom_orders_table_is_authoritative() || $this->synchronizer->data_sync_is_enabled() ) {
		WP_CLI::error( __( 'Cleanup can only be performed when HPOS is active and compatibility mode is disabled.', 'woocommerce' ) );
	}
	$handler = wc_get_container()->get( LegacyDataHandler::class );

	$all_orders  = 'all' === $args[0];
	$force       = (bool) ( $assoc_args['force'] ?? false );
	$q_order_ids = $all_orders ? array() : $args;
	$q_limit     = $all_orders ? absint( $assoc_args['batch-size'] ?? 500 ) : 0; // Limit per batch.

	$order_count = $handler->count_orders_for_cleanup( $q_order_ids );
	if ( ! $order_count ) {
		WP_CLI::warning( __( 'No orders to cleanup.', 'woocommerce' ) );
		return;
	}

	$progress   = WP_CLI\Utils\make_progress_bar( __( 'HPOS cleanup', 'woocommerce' ), $order_count );
	$count      = 0;
	$failed_ids = array();

	// translators: %d is the number of orders to clean up.
	WP_CLI::log( sprintf( _n( 'Starting cleanup for %d order...', 'Starting cleanup for %d orders...', $order_count, 'woocommerce' ), $order_count ) );

	do {
		$failed_ids_in_batch = array();
		$order_ids           = $handler->get_orders_for_cleanup( $q_order_ids, $q_limit );

		if ( $failed_ids && empty( array_diff( $order_ids, $failed_ids ) ) ) {
			break;
		}

		$order_ids = array_diff( $order_ids, $failed_ids ); // Do not reattempt IDs that have already failed.

		foreach ( $order_ids as $order_id ) {
			try {
				$handler->cleanup_post_data( $order_id, $force );
				++$count;

				// translators: %d is an order ID.
				WP_CLI::debug( sprintf( __( 'Cleanup completed for order %d.', 'woocommerce' ), $order_id ) );
			} catch ( \Exception $e ) {
				// translators: %1$d is an order ID, %2$s is an error message.
				WP_CLI::warning( sprintf( __( 'An error occurred while cleaning up order %1$d: %2$s', 'woocommerce' ), $order_id, $e->getMessage() ) );
				$failed_ids_in_batch[] = $order_id;
			}

			$progress->tick();
		}

		$failed_ids = array_merge( $failed_ids, $failed_ids_in_batch );

		if ( ! $all_orders ) {
			break;
		}

		if ( $failed_ids_in_batch && ! array_diff( $order_ids, $failed_ids_in_batch ) ) {
			WP_CLI::warning( __( 'Failed to clean up all orders in a batch. Aborting.', 'woocommerce' ) );
			break;
		}

		$this->free_in_memory_usage();
	} while ( $order_ids );

	$progress->finish();

	if ( $failed_ids ) {
		return WP_CLI::error(
			sprintf(
				// translators: %d is the number of orders that were cleaned up.
				_n( 'Cleanup completed for %d order. Review errors above.', 'Cleanup completed for %d orders. Review errors above.', $count, 'woocommerce' ),
				$count
			)
		);
	}

	WP_CLI::success(
		sprintf(
			// translators: %d is the number of orders that were cleaned up.
			_n( 'Cleanup completed for %d order.', 'Cleanup completed for %d orders.', $count, 'woocommerce' ),
			$count
		)
	);
}