Automattic\WooCommerce\Admin\API\Reports\Orders
DataStore::get_products_by_order_ids
Get product IDs, names, and quantity from order IDs.
Method of the class: DataStore{}
No Hooks.
Returns
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() DataStore::get products by order ids code WC 11.1.2
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})
AND product_qty > 0
",
ARRAY_A
);
/* phpcs:enable */
return $products;
}