如何获取订单商品 ID 以获取一些产品元数据?

2023-01-17php开发问题
1

本文介绍了如何获取订单商品 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>';
} 

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

订单中数据相关参考:

  • 如何获取 WooCommerce 订单详情(也适用于 woocommerce 3)
  • 获取订单商品和 Woocommerce 3 中的 WC_Order_Item_Product

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

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17