Automattic\WooCommerce\Internal\DataStores\Orders

OrdersTableDataStore::get_post_orders_for_ids()privateWC 1.0

Helper function to get posts data for an order in bullk. We use to this to compute posts object in bulk so that we can compare it with COT data.

Method of the class: OrdersTableDataStore{}

No Hooks.

Return

Array. List of posts.

Usage

// private - for code of main (parent) class only
$result = $this->get_post_orders_for_ids( $orders ): array;
$orders(array) (required)
List of orders mapped by $order_id.

OrdersTableDataStore::get_post_orders_for_ids() code WC 9.3.3

private function get_post_orders_for_ids( array $orders ): array {
	$order_ids = array_keys( $orders );
	// We have to bust meta cache, otherwise we will just get the meta cached by OrderTableDataStore.
	foreach ( $order_ids as $order_id ) {
		wp_cache_delete( WC_Order::generate_meta_cache_key( $order_id, 'orders' ), 'orders' );
	}

	$cpt_stores       = array();
	$cpt_store_orders = array();
	foreach ( $orders as $order_id => $order ) {
		$table_data_store     = $order->get_data_store();
		$cpt_data_store       = $table_data_store->get_cpt_data_store_instance();
		$cpt_store_class_name = get_class( $cpt_data_store );
		if ( ! isset( $cpt_stores[ $cpt_store_class_name ] ) ) {
			$cpt_stores[ $cpt_store_class_name ]       = $cpt_data_store;
			$cpt_store_orders[ $cpt_store_class_name ] = array();
		}
		$cpt_store_orders[ $cpt_store_class_name ][ $order_id ] = $order;
	}

	$cpt_orders = array();
	foreach ( $cpt_stores as $cpt_store_name => $cpt_store ) {
		// Prime caches if we can.
		if ( method_exists( $cpt_store, 'prime_caches_for_orders' ) ) {
			$cpt_store->prime_caches_for_orders( array_keys( $cpt_store_orders[ $cpt_store_name ] ), array() );
		}

		foreach ( $cpt_store_orders[ $cpt_store_name ] as $order_id => $order ) {
			$cpt_order_class_name = wc_get_order_type( $order->get_type() )['class_name'];
			$cpt_order            = new $cpt_order_class_name();

			try {
				$cpt_order->set_id( $order_id );
				$cpt_store->read( $cpt_order );
				$cpt_orders[ $order_id ] = $cpt_order;
			} catch ( Exception $e ) {
				// If the post record has been deleted (for instance, by direct query) then an exception may be thrown.
				$this->error_logger->warning(
					sprintf(
						/* translators: %1$d order ID. */
						__( 'Unable to load the post record for order %1$d', 'woocommerce' ),
						$order_id
					),
					array(
						'exception_code' => $e->getCode(),
						'exception_msg'  => $e->getMessage(),
						'origin'         => __METHOD__,
					)
				);
			}
		}
	}
	return $cpt_orders;
}