php 如何获取订单商品 ID 以获取一些产品元数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39390838/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get order items ids to get some product meta data?
提问by camelot
I'm trying to extract item meta value from Woocommerce's orders by using:
我正在尝试使用以下方法从 Woocommerce 的订单中提取项目元值:
$data = wc_get_order_item_meta( $item, '_tmcartepo_data', true );
However, I can't find a way to get order_item_id as the first parameter (using get_items)
但是,我找不到获取 order_item_id 作为第一个参数的方法(使用 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;
}
And I mean this order item_id (1 and 2)
我的意思是这个订单 item_id (1 和 2)
Order_item_id in database - Image
How can I don that?
我怎么能这样做?
Thanks.
谢谢。
回答by LoicTheAztec
2018 Update:
- Clarifying the answer with 2 possible cases
- Added compatibility for woocommerce 3+
2018 更新:
- 用两种可能的情况澄清答案
- 添加了 woocommerce 3+ 的兼容性
So There can be 2 cases:
所以可能有两种情况:
1) Get product meta data(not set in order item meta data):
1)获取产品元数据(不在订单项元数据中设置):
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()
).
您将需要在 foreach 循环中为 a 获取产品 ID,WC_Order
并为该产品获取一些元数据,您将使用get_post_meta()
函数(但不是wc_get_order_item_meta()
)。
So here is your code:
所以这是你的代码:
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) Get order item meta data(custom field value):
2)获取订单项元数据(自定义字段值):
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;
}
}
If the custom field data is an array, you can access the data in a foreach loop:
如果自定义字段数据是数组,则可以在 foreach 循环中访问数据:
// Iterating in an array of keys/values
foreach( $custom_field as $key => $value ){
echo '<p>key: '.$key.' | value: '.$value.'</p>';
}
All code is tested and works.
所有代码都经过测试和工作。
Reference related to data in orders:
订单数据相关参考:
- How to get WooCommerce order details(also for woocommerce 3)
- Get Order items and WC_Order_Item_Product in Woocommerce 3
- 如何获取 WooCommerce 订单详细信息(也适用于 woocommerce 3)
- 在 Woocommerce 3 中获取订单项目和 WC_Order_Item_Product
回答by Berend
When doing the foreach on $order->get_items()
, their key is actually the orderline ID. So:
在对 执行 foreach 时$order->get_items()
,它们的键实际上是订单行 ID。所以:
foreach ( $order->get_items() as $key => $item ) {
$data = wc_get_order_item_meta( $key, '_tmcartepo_data' );
...
}
回答by jgangso
Late to the party, but being working with the same point with TM Extra Product Options plugin, I think this is what answers your question:
迟到了,但与 TM Extra Product Options 插件的相同点,我认为这就是回答你的问题的原因:
$order = wc_get_order( $post->ID );
$items = $order->get_items();
foreach( $items as $item ){
$data = unserialize($item['item_meta']['_tmcartepo_data'][0]);
$a = $data[0]['value'];
$b = $data[1]['value'];
echo $a;
echo $b;
}
Tested and works in my case.
在我的情况下经过测试并有效。
回答by Cagy79
Use this <pre><?php print_r($items); ?></pre>
to check all the contents of the $items array/object.
使用它<pre><?php print_r($items); ?></pre>
来检查 $items 数组/对象的所有内容。
回答by Luis Vergh1l Miguel
foreach ( $order->get_items() as $key => $item ) {
$data = wc_get_order_item_meta( $key, '_tmcartepo_data' );
...
This solution was worth to me, change "_tmcartepo_data" for your meta_key.
这个解决方案对我来说很有价值,为你的元密钥更改“_tmcartepo_data”。
回答by Mostafa Soufi
A simple way to get order items from database;
从数据库中获取订单商品的简单方法;
/**
* @param $order_id
*
* @return array|null|object
*/
function get_order_items( $order_id ) {
global $wpdb, $table_prefix;
$items = $wpdb->get_results( "SELECT * FROM `{$table_prefix}woocommerce_order_items` WHERE `order_id` = {$order_id}" );
$item_name = array();
foreach ( $items as $item ) {
$item_name[] = $item->order_item_name;
}
return $item_name;
}