Automattic\WooCommerce\Admin\API\Reports\Orders

DataStore::get_products_by_order_ids()protectedWC 1.0

Get product IDs, names, and quantity from order IDs.

Method of the class: DataStore{}

No Hooks.

Return

Array.

Usage

// protected - for code of main (parent) or child class
$result = $this->get_products_by_order_ids( $order_ids );
$order_ids(array) (required)
Array of order IDs.

DataStore::get_products_by_order_ids() code WC 8.6.1

protected function get_products_by_order_ids( $order_ids ) {
	global $wpdb;
	$order_product_lookup_table = $wpdb->prefix . 'wc_order_product_lookup';
	$included_order_ids         = implode( ',', $order_ids );

	/* phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared */
	$products = $wpdb->get_results(
		"SELECT
			order_id,
			product_id,
			variation_id,
			post_title as product_name,
			product_qty as product_quantity
		FROM {$wpdb->posts}
		JOIN
			{$order_product_lookup_table}
			ON {$wpdb->posts}.ID = (
				CASE WHEN variation_id > 0
					THEN variation_id
					ELSE product_id
				END
			)
		WHERE
			order_id IN ({$included_order_ids})
		",
		ARRAY_A
	);
	/* phpcs:enable */

	return $products;
}