如何获取订单商品 ID 以获取一些产品元数据? [英] How to get order items ids to get some product meta data?

查看:22
本文介绍了如何获取订单商品 ID 以获取一些产品元数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下方法从 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 );

但是,我找不到获取 order_item_id 作为第一个参数的方法(使用 get_items)

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)

数据库中的Order_item_id - 图片

我该怎么做?

谢谢.

推荐答案

2018 年更新:

  • 用两种可能的情况澄清答案
  • 添加了对 woocommerce 3+ 的兼容性

所以可能有两种情况:

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) 获取订单项元数据(自定义字段值):

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

所有代码都经过测试且有效.

订单中数据相关参考:

这篇关于如何获取订单商品 ID 以获取一些产品元数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆