Automattic\WooCommerce\Internal\DataStores\Orders
OrdersTableDataStore::get_post_orders_for_ids
Helper function to get posts data for an order in bulk. 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.
Returns
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() OrdersTableDataStore::get post orders for ids code WC 10.7.0
private function get_post_orders_for_ids( array $orders ): array {
$order_ids = array_keys( $orders );
foreach ( $order_ids as $order_id ) {
// Exclude orders where the CPT version is a placeholder post.
$post_type = get_post_type( $order_id );
if ( ! $post_type || DataSynchronizer::PLACEHOLDER_ORDER_POST_TYPE === $post_type ) {
unset( $orders[ $order_id ] );
continue;
}
// We have to bust meta cache, otherwise we will just get the meta cached by OrderTableDataStore.
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();
if ( ! $cpt_data_store ) {
throw new \Exception( sprintf( 'No CPT data store found for order %d.', absint( $order_id ) ) );
}
$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;
}