Automattic\WooCommerce\Internal\DataStores\Orders

DataSynchronizer::get_ids_of_orders_pending_sync()publicWC 1.0

Get a list of ids of orders than are out of sync.

Valid values for $type are:

ID_TYPE_MISSING_IN_ORDERS_TABLE: orders that exist in posts table but not in orders table. ID_TYPE_MISSING_IN_POSTS_TABLE: orders that exist in orders table but not in posts table (the corresponding post entries are placeholders). ID_TYPE_DIFFERENT_UPDATE_DATE: orders that exist in both tables but have different last update dates.

{} It's a method of the class: DataSynchronizer{}

No Hooks.

Return

Array. An array of order ids.

Usage

$DataSynchronizer = new DataSynchronizer();
$DataSynchronizer->get_ids_of_orders_pending_sync( $type, $limit );
$type(int) (required)
One of ID_TYPE_MISSING_IN_ORDERS_TABLE, ID_TYPE_MISSING_IN_POSTS_TABLE, ID_TYPE_DIFFERENT_UPDATE_DATE.
$limit(int) (required)
Maximum number of ids to return.

Code of DataSynchronizer::get_ids_of_orders_pending_sync() WC 7.1.0

public function get_ids_of_orders_pending_sync( int $type, int $limit ) {
	global $wpdb;

	if ( $limit < 1 ) {
		throw new \Exception( '$limit must be at least 1' );
	}

	$orders_table                 = $this->data_store::get_orders_table_name();
	$order_post_types             = wc_get_order_types( 'cot-migration' );
	$order_post_type_placeholders = implode( ', ', array_fill( 0, count( $order_post_types ), '%s' ) );

	// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
	switch ( $type ) {
		case self::ID_TYPE_MISSING_IN_ORDERS_TABLE:
			// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare -- $order_post_type_placeholders is prepared.
			$sql = $wpdb->prepare(
				"
SELECT posts.ID FROM $wpdb->posts posts
LEFT JOIN $orders_table orders ON posts.ID = orders.id
WHERE
  posts.post_type IN ($order_post_type_placeholders)
  AND posts.post_status != 'auto-draft'
  AND orders.id IS NULL",
				$order_post_types
			);
			break;
		case self::ID_TYPE_MISSING_IN_POSTS_TABLE:
			$sql = "
SELECT posts.ID FROM $wpdb->posts posts
INNER JOIN $orders_table orders ON posts.id=orders.id
WHERE posts.post_type = '" . self::PLACEHOLDER_ORDER_POST_TYPE . "'
AND orders.status not in ( 'auto-draft' )
";
			break;
		case self::ID_TYPE_DIFFERENT_UPDATE_DATE:
			$operator = $this->custom_orders_table_is_authoritative() ? '>' : '<';
			// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare -- $order_post_type_placeholders is prepared.
			$sql = $wpdb->prepare(
				"
SELECT orders.id FROM $orders_table orders
JOIN $wpdb->posts posts on posts.ID = orders.id
WHERE
  posts.post_type IN ($order_post_type_placeholders)
  AND orders.date_updated_gmt $operator posts.post_modified_gmt
",
				$order_post_types
			);
			// phpcs:enable
			break;
		default:
			throw new \Exception( 'Invalid $type, must be one of the ID_TYPE_... constants.' );
	}
	// phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared

	// phpcs:ignore WordPress.DB
	return array_map( 'intval', $wpdb->get_col( $sql . " LIMIT $limit" ) );
}