Automattic\WooCommerce\Internal\DataStores\Orders

OrdersTableDataStore::untrash_order()publicWC 1.0

Attempts to restore the specified order back to its original status (after having been trashed).

Method of the class: OrdersTableDataStore{}

Hooks from the method

Return

true|false. If the operation was successful.

Usage

$OrdersTableDataStore = new OrdersTableDataStore();
$OrdersTableDataStore->untrash_order( $order ): bool;
$order(WC_Order) (required)
The order to be untrashed.

OrdersTableDataStore::untrash_order() code WC 8.7.0

public function untrash_order( WC_Order $order ): bool {
	$id     = $order->get_id();
	$status = $order->get_status();

	if ( 'trash' !== $status ) {
		wc_get_logger()->warning(
			sprintf(
				/* translators: 1: order ID, 2: order status */
				__( 'Order %1$d cannot be restored from the trash: it has already been restored to status "%2$s".', 'woocommerce' ),
				$id,
				$status
			)
		);
		return false;
	}

	$previous_status           = $order->get_meta( '_wp_trash_meta_status' );
	$valid_statuses            = wc_get_order_statuses();
	$previous_state_is_invalid = ! array_key_exists( $previous_status, $valid_statuses );
	$pending_is_valid_status   = array_key_exists( 'wc-pending', $valid_statuses );

	if ( $previous_state_is_invalid && $pending_is_valid_status ) {
		// If the previous status is no longer valid, let's try to restore it to "pending" instead.
		wc_get_logger()->warning(
			sprintf(
				/* translators: 1: order ID, 2: order status */
				__( 'The previous status of order %1$d ("%2$s") is invalid. It has been restored to "pending" status instead.', 'woocommerce' ),
				$id,
				$previous_status
			)
		);

		$previous_status = 'pending';
	} elseif ( $previous_state_is_invalid ) {
		// If we cannot restore to pending, we should probably stand back and let the merchant intervene some other way.
		wc_get_logger()->warning(
			sprintf(
				/* translators: 1: order ID, 2: order status */
				__( 'The previous status of order %1$d ("%2$s") is invalid. It could not be restored.', 'woocommerce' ),
				$id,
				$previous_status
			)
		);

		return false;
	}

	/**
	 * Fires before an order is restored from the trash.
	 *
	 * @since 7.2.0
	 *
	 * @param int    $order_id        Order ID.
	 * @param string $previous_status The status of the order before it was trashed.
	 */
	do_action( 'woocommerce_untrash_order', $order->get_id(), $previous_status );

	$order->set_status( $previous_status );
	$order->save();

	// Was the status successfully restored? Let's clean up the meta and indicate success...
	if ( 'wc-' . $order->get_status() === $previous_status ) {
		$order->delete_meta_data( '_wp_trash_meta_status' );
		$order->delete_meta_data( '_wp_trash_meta_time' );
		$order->delete_meta_data( '_wp_trash_meta_comments_status' );
		$order->save_meta_data();

		return true;
	}

	// ...Or log a warning and bail.
	wc_get_logger()->warning(
		sprintf(
			/* translators: 1: order ID, 2: order status */
			__( 'Something went wrong when trying to restore order %d from the trash. It could not be restored.', 'woocommerce' ),
			$id
		)
	);

	return false;
}