WC_API_Products::get_product_orders()publicWC 2.4.0

Get the orders for a product

Метод класса: WC_API_Products{}

Хуки из метода

Возвращает

Массив|WP_Error.

Использование

$WC_API_Products = new WC_API_Products();
$WC_API_Products->get_product_orders( $id, $fields, $filter, $status, $page );
$id(int) (обязательный)
the product ID to get orders for
$fields **
-
По умолчанию: null
$filter(массив)
filters to include in response
По умолчанию: array()
$status(строка)
the order status to retrieve
По умолчанию: null
$page($page)
page to retrieve
По умолчанию: 1

Список изменений

С версии 2.4.0 Введена.

Код WC_API_Products::get_product_orders() WC 8.7.0

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