如何获取订单商品ID以获取一些产品元数据? [英] How to get order items ids to get some product meta data?
问题描述
我正在尝试使用以下方法从Woocommerce的订单中提取商品元值:
I'm trying to extract item meta value from Woocommerce's orders by using:
$data = wc_get_order_item_meta( $item, '_tmcartepo_data', true );
但是,我找不到一种方法(使用get_items)将order_item_id作为第一个参数
However, I can't find a way to get order_item_id as the first parameter (using get_items)
global $woocommerce, $post, $wpdb;
$order = new WC_Order($post->ID);
$items = $order->get_items();
foreach ( $items as $item ) {
$item_id = $item['order_item_id']; //???
$data = wc_get_order_item_meta( $item_id, '_tmcartepo_data', true );
$a = $data[0]['value'];
$b = $data[1]['value'];
echo $a;
echo $b;
}
我是说这个订单item_id(1和2)
And I mean this order item_id (1 and 2)
我该怎么做?
谢谢.
推荐答案
2018更新:
- 用2种可能的情况澄清答案
- 增加了对woocommerce 3+的兼容性
所以可能有2种情况:
1)获取产品元数据(未在订单商品元数据中设置):
1) Get product meta data (not set in order item meta data):
您将需要在foreach循环中获取WC_Order
的产品ID,并获取该产品的一些元数据,您将使用 get_post_meta()
函数(但不是 wc_get_order_item_meta()
).
You will need to get the product ID in the foreach loop for a WC_Order
and to get some metadata for this product you wil use get_post_meta()
function ( but NOT wc_get_order_item_meta()
).
这是您的代码:
global $post;
$order = wc_get_order( $post->ID );
$items = $order->get_items();
foreach ( $order->get_items() => $item ) {
// Compatibility for woocommerce 3+
$product_id = version_compare( WC_VERSION, '3.0', '<' ) ? $item['product_id'] : $item->get_product_id();
// Here you get your data
$custom_field = get_post_meta( $product_id, '_tmcartepo_data', true);
// To test data output (uncomment the line below)
// print_r($custom_field);
// If it is an array of values
if( is_array( $custom_field ) ){
echo implode( '<br>', $custom_field ); // one value displayed by line
}
// just one value (a string)
else {
echo $custom_field;
}
}
2)获取订单商品元数据(自定义字段值):
2) Get order item meta data (custom field value):
global $post;
$order = wc_get_order( $post->ID );
$items = $order->get_items();
foreach ( $order->get_items() as $item_id => $item ) {
// Here you get your data
$custom_field = wc_get_order_item_meta( $item_id, '_tmcartepo_data', true );
// To test data output (uncomment the line below)
// print_r($custom_field);
// If it is an array of values
if( is_array( $custom_field ) ){
echo implode( '<br>', $custom_field ); // one value displayed by line
}
// just one value (a string)
else {
echo $custom_field;
}
}
如果自定义字段数据是数组,则可以在foreach循环中访问数据:
If the custom field data is an array, you can access the data in a foreach loop:
// Iterating in an array of keys/values
foreach( $custom_field as $key => $value ){
echo '<p>key: '.$key.' | value: '.$value.'</p>';
}
所有代码均已通过测试并且可以正常工作.
与订单中的数据相关的参考:
Reference related to data in orders:
- 如何获取WooCommerce订单详细信息 (也适用于woocommerce 3)
- 获取订单商品和Woocommerce 3中的WC_Order_Item_Product
- How to get WooCommerce order details (also for woocommerce 3)
- Get Order items and WC_Order_Item_Product in Woocommerce 3
这篇关于如何获取订单商品ID以获取一些产品元数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!