I have this table (wp_woocommerce_order_itemmeta):
And this table (wp_woocommerce_order_items):
I'm using a function (functions.php) which returns these item id's in an array:
function retrieve_orders_ids_from_a_product_id( $product_id, $order_date ) {
global $wpdb;
$table_posts = $wpdb->prefix . "posts";
$table_items = $wpdb->prefix . "woocommerce_order_items";
$table_itemmeta = $wpdb->prefix . "woocommerce_order_itemmeta";
// Define HERE the orders status to include in <== <== <== <== <== <== <==
$orders_statuses = "'wc-processing'";
# Requesting All defined statuses Orders IDs for a defined product ID
$orders_ids = $wpdb->get_col( "
SELECT DISTINCT $table_items.order_id
FROM $table_itemmeta, $table_items, $table_posts
WHERE $table_items.order_item_id = $table_itemmeta.order_item_id
AND $table_items.order_id = $table_posts.ID
AND $table_posts.post_status IN ( $orders_statuses )
AND $table_itemmeta.meta_key LIKE '_product_id'
AND $table_itemmeta.meta_value LIKE '$product_id'
AND $table_itemmeta.meta_key LIKE 'order_date'
AND $table_itemmeta.meta_value LIKE '$order_date'
ORDER BY $table_items.order_item_id DESC"
);
// return an array of Orders IDs for the given product ID
return $orders_ids;
}
And I call it inside my page:
<?php $orders_ids_array = retrieve_orders_ids_from_a_product_id($getid, $order_date); ?>
I'm 100% sure that $getid ($getid = product_id) and $order_date has the exact value which I from my db.
But if I print_r the array it returns with:
Array ( )
instead of any value.
Finally I need to count these id-s, but if doesn't return anything it looks impossible to me.

