Automattic\WooCommerce\Internal\DataStores\Orders
OrdersTableDataStore::read_multiple
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() OrdersTableDataStore::read multiple code WC 10.6.2
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();
if ( $data_sync_enabled ) {
// We prefer not syncing-on-read if we are inside a webhook delivery or importing orders, as those events are likely triggered after the order is written
// and we don't want to possibly create loops of sync-on-read.
$should_sync_on_read = ! doing_action( 'woocommerce_deliver_webhook_async' ) && ! doing_action( 'wc-admin_import_orders' );
/**
* Allow opportunity to disable sync on read, while keeping sync on write enabled. This adds another step as a large shop progresses from full sync to no sync with HPOS authoritative.
* This filter is only executed if data sync is enabled from settings in the first place as it's meant to be a step between full sync -> no sync, rather than be a control for enabling just the sync on read. Sync on read without sync on write is problematic as any update will reset on the next read, but sync on write without sync on read is fine.
*
* @param bool $read_on_sync_enabled Whether to sync on read.
*
* @since 8.1.0
*/
$data_sync_enabled = apply_filters( 'woocommerce_hpos_enable_sync_on_read', $should_sync_on_read );
}
$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() ] );
}
}
}