Abstract_WC_Order_Data_Store_CPT::read_items()publicWC 1.0

Read order items of a specific type from the database for this order.

Method of the class: Abstract_WC_Order_Data_Store_CPT{}

No Hooks.

Return

Array.

Usage

$Abstract_WC_Order_Data_Store_CPT = new Abstract_WC_Order_Data_Store_CPT();
$Abstract_WC_Order_Data_Store_CPT->read_items( $order, $type );
$order(WC_Order) (required)
Order object.
$type(string) (required)
Order item type.

Abstract_WC_Order_Data_Store_CPT::read_items() code WC 9.4.2

public function read_items( $order, $type ) {
	global $wpdb;

	// When the order is not yet saved, we cannot get the items from DB. Trying to do so will risk reading items of different orders that were saved incorrectly.
	if ( 0 === $order->get_id() ) {
		return array();
	}

	// Get from cache if available.
	$items = 0 < $order->get_id() ? wp_cache_get( 'order-items-' . $order->get_id(), 'orders' ) : false;

	if ( false === $items ) {
		$items = $wpdb->get_results(
			$wpdb->prepare( "SELECT order_item_type, order_item_id, order_id, order_item_name FROM {$wpdb->prefix}woocommerce_order_items WHERE order_id = %d ORDER BY order_item_id;", $order->get_id() )
		);
		foreach ( $items as $item ) {
			wp_cache_set( 'item-' . $item->order_item_id, $item, 'order-items' );
		}
		if ( 0 < $order->get_id() ) {
			wp_cache_set( 'order-items-' . $order->get_id(), $items, 'orders' );
		}
	}

	$items = wp_list_filter( $items, array( 'order_item_type' => $type ) );

	if ( ! empty( $items ) ) {
		$items = array_map( array( 'WC_Order_Factory', 'get_order_item' ), array_combine( wp_list_pluck( $items, 'order_item_id' ), $items ) );
	} else {
		$items = array();
	}

	return $items;
}