WC_API_Products::get_product_orders( int $id, $fields = null, array $filter = array(), string $status = null, $page = 1 )
Get the orders for a product
Description Description
Parameters Parameters
- $id
-
(Required) the product ID to get orders for
-
(Required) fields fields to retrieve
- $filter
-
(Optional) filters to include in response
Default value: array()
- $status
-
(Optional) the order status to retrieve
Default value: null
- $page
-
(Optional) $page page to retrieve
Default value: 1
Return Return
(array|WP_Error)
Source Source
File: includes/legacy/api/v2/class-wc-api-products.php
public function get_product_orders( $id, $fields = null, $filter = array(), $status = null, $page = 1 ) {
global $wpdb;
$id = $this->validate_request( $id, 'product', 'read' );
if ( is_wp_error( $id ) ) {
return $id;
}
$order_ids = $wpdb->get_col( $wpdb->prepare( "
SELECT order_id
FROM {$wpdb->prefix}woocommerce_order_items
WHERE order_item_id IN ( SELECT order_item_id FROM {$wpdb->prefix}woocommerce_order_itemmeta WHERE meta_key = '_product_id' AND meta_value = %d )
AND order_item_type = 'line_item'
", $id ) );
if ( empty( $order_ids ) ) {
return array( 'orders' => array() );
}
$filter = array_merge( $filter, array(
'in' => implode( ',', $order_ids ),
) );
$orders = WC()->api->WC_API_Orders->get_orders( $fields, $filter, $status, $page );
return array( 'orders' => apply_filters( 'woocommerce_api_product_orders_response', $orders['orders'], $id, $filter, $fields, $this->server ) );
}
Changelog Changelog
| Version | Description |
|---|---|
| 2.4.0 | Introduced. |