You’ll find details here: https://gist.github.com/lukecav/05afef12feaf980c121da9afb9291ad5
OR
Another approach is https://github.com/woocommerce/woocommerce/wiki/wc_get_orders-and-WC_Order_Query . Use if….else and store order data in Arr[product_id] = OrderArr
Kindly accept my answer if it is of any help.