Php函数返回没有值

时间:2018-01-11 18:36:58

标签: php arrays wordpress

我有这张桌子(wp_woocommerce_order_itemmeta):

proof that it has value inside it

此表(wp_woocommerce_order_items):

proof #2

我使用的函数(functions.php)在数组中返回这些项ID:

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

我在我的页面中调用它:

<?php $orders_ids_array = retrieve_orders_ids_from_a_product_id($getid, $order_date); ?>

我100%确定$getid$getid = product_id)和$order_date具有我的数据库中的确切值。

但如果我print_r它返回的数组:

  

Array()

而不是任何价值。

最后我需要计算这些id-s,但如果没有返回任何东西,那对我来说是不可能的。

2 个答案:

答案 0 :(得分:1)

看起来像是

 AND $table_itemmeta.meta_value LIKE 'order_date'

应该是

 AND $table_itemmeta.meta_key LIKE 'order_date'

答案 1 :(得分:0)

好的,它有两个问题。第一:

for key, val zip(d['inds'], d['vals']):

应该是

 AND $table_itemmeta.meta_value LIKE 'order_date'

,第二个是 AND $table_itemmeta.meta_key LIKE 'order_date' 总是刷新,因为我的$order_date循环。