0

I have this table (wp_woocommerce_order_itemmeta):

proof that it has value inside it

And this table (wp_woocommerce_order_items):

proof #2

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.

3
  • 1
    Can you run your sql statement inside phpmyadmin or Sequel Pro or something? To troubleshoot it. Commented Jan 11, 2018 at 18:39
  • Tried. It runs but the return is nothing. Commented Jan 11, 2018 at 18:56
  • Okay, without the order_date it works and gives back the id's. And I think I know what was my problem. Commented Jan 11, 2018 at 19:03

2 Answers 2

1

It looks like

 AND $table_itemmeta.meta_value LIKE 'order_date'

should be

 AND $table_itemmeta.meta_key LIKE 'order_date'
Sign up to request clarification or add additional context in comments.

1 Comment

Yeah I fixed it, but still didn't gives me back any value
0

Okay, it has 2 problems. First:

 AND $table_itemmeta.meta_value LIKE 'order_date'

should be

 AND $table_itemmeta.meta_key LIKE 'order_date'

and the second is the $order_date is always refreshed because my foreach() loop.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.