Automattic\WooCommerce\Internal\DataStores\Orders

OrdersTableDataStore::read_multiplepublicWC 6.9.0

Reads multiple orders from custom tables in one pass.

Method of the class: OrdersTableDataStore{}

Hooks from the method

Returns

null. Nothing (null).

Usage

$OrdersTableDataStore = new OrdersTableDataStore();
$OrdersTableDataStore->read_multiple( $orders );
$orders(array[\WC_Order]) (required) (passed by reference — &)
Order objects.

Changelog

Since 6.9.0 Introduced.

OrdersTableDataStore::read_multiple() code WC 10.7.0

public function read_multiple( &$orders ) {
	$order_ids = array_keys( $orders );
	$data      = $this->get_order_data_for_ids( $order_ids );

	if ( count( $data ) !== count( $order_ids ) ) {
		throw new \Exception( esc_html__( 'Invalid order IDs in call to read_multiple()', 'woocommerce' ) );
	}

	$data_synchronizer = wc_get_container()->get( DataSynchronizer::class );
	if ( ! $data_synchronizer instanceof DataSynchronizer ) {
		return;
	}

	$data_sync_enabled = $data_synchronizer->data_sync_is_enabled()
		&& ! doing_action( 'woocommerce_deliver_webhook_async' )
		&& ! doing_action( 'wc-admin_import_orders' );

	if ( $data_sync_enabled ) {
		/**
		 * Filters whether to sync order data from posts on read.
		 *
		 * Defaults to false because sync-on-read can be dangerous when HPOS is
		 * authoritative and running correctly, as it allows the posts data store
		 * to override HPOS data.
		 *
		 * @param bool $sync_on_read_enabled Whether to sync on read.
		 *
		 * @since 8.1.0
		 */
		$data_sync_enabled = apply_filters( 'woocommerce_hpos_enable_sync_on_read', false );
	}

	$load_posts_for = array_diff( $order_ids, array_merge( self::$reading_order_ids, self::$backfilling_order_ids ) );

	$post_orders = array();
	if ( $data_sync_enabled ) {
		global $wpdb;

		// Exclude orders that do not exist in the posts table.
		if ( $load_posts_for ) {
			$order_ids_placeholder = implode( ', ', array_fill( 0, count( $load_posts_for ), '%d' ) );
			$load_posts_for        = array_map( 'absint', $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE ID IN ( $order_ids_placeholder )", ...$load_posts_for ) ) ); // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
		}

		$post_orders = $this->get_post_orders_for_ids( array_intersect_key( $orders, array_flip( $load_posts_for ) ) );
	}

	$cogs_is_enabled = $this->cogs_is_enabled();

	foreach ( $data as $order_data ) {
		$order_id = absint( $order_data->id );
		$order    = $orders[ $order_id ];

		$this->init_order_record( $order, $order_id, $order_data );

		if ( $cogs_is_enabled && $order->has_cogs() ) {
			$this->read_cogs_data( $order, $order_data->meta_data );
		}

		if ( $data_sync_enabled && isset( $post_orders[ $order_id ] ) && $this->should_sync_order( $order ) ) {
			self::$reading_order_ids[] = $order_id;
			$this->maybe_sync_order( $order, $post_orders[ $order->get_id() ] );
		}
	}
}