WordPress at a glance
function is not described

WC_API_Products::get_product_orders() public WC 2.4.0

Get the orders for a product

{} It's a method of the class: WC_API_Products{}

Return

Array|WP_Error.

Usage

$WC_API_Products = new WC_API_Products();
$WC_API_Products->get_product_orders( $id, $fields, $filter, $status, $page );
$id(int) (required)
the product ID to get orders for
$fields **
-
Default: null
$filter(array)
filters to include in response
$status(string)
the order status to retrieve
$page($page)
page to retrieve

Changelog

Since 2.4.0 Introduced.

Code of WC_API_Products::get_product_orders() WC 5.0.0

<?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 ) );
}