Woocommerce - 通过产品 ID 获取元数据
Woocommerce - Getting meta data by product IDs
我正在寻找 return 每个订单的所有元数据(按与当前用户关联的产品 ID)。我尝试了各种 return 空数组的方法。我正在寻找访问 ID、键和值元数据
有人指出我哪里出错了吗?谢谢
$table_posts = $wpdb->prefix . "posts";
$table_items = $wpdb->prefix . "woocommerce_order_items";
$table_itemmeta = $wpdb->prefix . "woocommerce_order_itemmeta";
$orders_statuses = "'wc-completed', 'wc-processing', 'wc-on-hold'";
$orders_ids = $wpdb->get_col("
SELECT $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'
ORDER BY $table_items.order_item_id DESC"
);
上面的查询return下面的订单 ID 是正确的。
Array
(
[0] => 881
[1] => 708
)
我循环遍历所有订单
foreach ($orders_ids as $key => $value) {
$orders = wc_get_order($value);
foreach ($orders->get_items() as $item ){
$item_meta = $item->get_meta();
$item_meta_data = $item->get_meta_data();
$item_formatted_data = $item->get_formatted_meta_data();
$item_formatted_data_true = $item->get_formatted_meta_data( '_', true );
print_r($item_meta); // returns empty array
print_r($item_meta_data); // returns example below
print_r($item_formatted_data); // returns empty array
print_r($item_formatted_data_true); // returns empty array
}
}
这是我使用上面这段代码的输出
Array
(
[0] => WC_Meta_Data Object
(
[current_data:protected] => Array
(
[id] => 646
[key] => yith_booking_data
[value] => Array
(
[from] => 1603152000
[to] => 1603411200
[duration] => 3
[person_types] => Array
(
)
[booking_services] => Array
(
)
[booking_service_quantities] => Array
(
)
[_added-to-cart-timestamp] => 1602161920
)
)
[data:protected] => Array
(
[id] => 646
[key] => yith_booking_data
[value] => Array
(
[from] => 1603152000
[to] => 1603411200
[duration] => 3
[person_types] => Array
(
)
[booking_services] => Array
(
)
[booking_service_quantities] => Array
(
)
[_added-to-cart-timestamp] => 1602161920
)
)
)
[1] => WC_Meta_Data Object
(
[current_data:protected] => Array
(
[id] => 647
[key] => _booking_id
[value] => 882
)
[data:protected] => Array
(
[id] => 647
[key] => _booking_id
[value] => 882
)
)
)
Array
(
[0] => WC_Meta_Data Object
(
[current_data:protected] => Array
(
[id] => 451
[key] => yith_booking_data
[value] => Array
(
[from] => 1600905600
[to] => 1600992000
[duration] => 1
[person_types] => Array
(
)
[booking_services] => Array
(
)
[booking_service_quantities] => Array
(
)
[_added-to-cart-timestamp] => 1600955166
)
)
[data:protected] => Array
(
[id] => 451
[key] => yith_booking_data
[value] => Array
(
[from] => 1600905600
[to] => 1600992000
[duration] => 1
[person_types] => Array
(
)
[booking_services] => Array
(
)
[booking_service_quantities] => Array
(
)
[_added-to-cart-timestamp] => 1600955166
)
)
)
[1] => WC_Meta_Data Object
(
[current_data:protected] => Array
(
[id] => 452
[key] => _booking_id
[value] => 709
)
[data:protected] => Array
(
[id] => 452
[key] => _booking_id
[value] => 709
)
)
)
找到答案了。在循环中使用此代码
foreach ($orders_ids as $key => $value) {
$orders = wc_get_order($value);
foreach ($orders->get_items() as $item ){
$item_meta_data = $item->get_meta_data();
$item_key_meta = $item_meta_data[0]->value; <- add code
print_r($item_key_meta);
}
}
Returns
Array
(
[from] => 1603152000
[to] => 1603411200
[duration] => 3
[person_types] => Array
(
)
[booking_services] => Array
(
)
[booking_service_quantities] => Array
(
)
[_added-to-cart-timestamp] => 1602161920
)
Array
(
[from] => 1600905600
[to] => 1600992000
[duration] => 1
[person_types] => Array
(
)
[booking_services] => Array
(
)
[booking_service_quantities] => Array
(
)
[_added-to-cart-timestamp] => 1600955166
)
我正在寻找 return 每个订单的所有元数据(按与当前用户关联的产品 ID)。我尝试了各种 return 空数组的方法。我正在寻找访问 ID、键和值元数据
有人指出我哪里出错了吗?谢谢
$table_posts = $wpdb->prefix . "posts";
$table_items = $wpdb->prefix . "woocommerce_order_items";
$table_itemmeta = $wpdb->prefix . "woocommerce_order_itemmeta";
$orders_statuses = "'wc-completed', 'wc-processing', 'wc-on-hold'";
$orders_ids = $wpdb->get_col("
SELECT $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'
ORDER BY $table_items.order_item_id DESC"
);
上面的查询return下面的订单 ID 是正确的。
Array
(
[0] => 881
[1] => 708
)
我循环遍历所有订单
foreach ($orders_ids as $key => $value) {
$orders = wc_get_order($value);
foreach ($orders->get_items() as $item ){
$item_meta = $item->get_meta();
$item_meta_data = $item->get_meta_data();
$item_formatted_data = $item->get_formatted_meta_data();
$item_formatted_data_true = $item->get_formatted_meta_data( '_', true );
print_r($item_meta); // returns empty array
print_r($item_meta_data); // returns example below
print_r($item_formatted_data); // returns empty array
print_r($item_formatted_data_true); // returns empty array
}
}
这是我使用上面这段代码的输出
Array
(
[0] => WC_Meta_Data Object
(
[current_data:protected] => Array
(
[id] => 646
[key] => yith_booking_data
[value] => Array
(
[from] => 1603152000
[to] => 1603411200
[duration] => 3
[person_types] => Array
(
)
[booking_services] => Array
(
)
[booking_service_quantities] => Array
(
)
[_added-to-cart-timestamp] => 1602161920
)
)
[data:protected] => Array
(
[id] => 646
[key] => yith_booking_data
[value] => Array
(
[from] => 1603152000
[to] => 1603411200
[duration] => 3
[person_types] => Array
(
)
[booking_services] => Array
(
)
[booking_service_quantities] => Array
(
)
[_added-to-cart-timestamp] => 1602161920
)
)
)
[1] => WC_Meta_Data Object
(
[current_data:protected] => Array
(
[id] => 647
[key] => _booking_id
[value] => 882
)
[data:protected] => Array
(
[id] => 647
[key] => _booking_id
[value] => 882
)
)
)
Array
(
[0] => WC_Meta_Data Object
(
[current_data:protected] => Array
(
[id] => 451
[key] => yith_booking_data
[value] => Array
(
[from] => 1600905600
[to] => 1600992000
[duration] => 1
[person_types] => Array
(
)
[booking_services] => Array
(
)
[booking_service_quantities] => Array
(
)
[_added-to-cart-timestamp] => 1600955166
)
)
[data:protected] => Array
(
[id] => 451
[key] => yith_booking_data
[value] => Array
(
[from] => 1600905600
[to] => 1600992000
[duration] => 1
[person_types] => Array
(
)
[booking_services] => Array
(
)
[booking_service_quantities] => Array
(
)
[_added-to-cart-timestamp] => 1600955166
)
)
)
[1] => WC_Meta_Data Object
(
[current_data:protected] => Array
(
[id] => 452
[key] => _booking_id
[value] => 709
)
[data:protected] => Array
(
[id] => 452
[key] => _booking_id
[value] => 709
)
)
)
找到答案了。在循环中使用此代码
foreach ($orders_ids as $key => $value) {
$orders = wc_get_order($value);
foreach ($orders->get_items() as $item ){
$item_meta_data = $item->get_meta_data();
$item_key_meta = $item_meta_data[0]->value; <- add code
print_r($item_key_meta);
}
}
Returns
Array
(
[from] => 1603152000
[to] => 1603411200
[duration] => 3
[person_types] => Array
(
)
[booking_services] => Array
(
)
[booking_service_quantities] => Array
(
)
[_added-to-cart-timestamp] => 1602161920
)
Array
(
[from] => 1600905600
[to] => 1600992000
[duration] => 1
[person_types] => Array
(
)
[booking_services] => Array
(
)
[booking_service_quantities] => Array
(
)
[_added-to-cart-timestamp] => 1600955166
)