【问题标题】:How to get order items ids to get some product meta data?如何获取订单商品 ID 以获取一些产品元数据?
【发布时间】:2017-01-16 09:08:02
【问题描述】:

我正在尝试使用以下方法从 Woocommerce 的订单中提取商品元值:

$data = wc_get_order_item_meta( $item, '_tmcartepo_data', true );

但是,我找不到将 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;
}

我的意思是这个订单 item_id(1 和 2)

Order_item_id in database - Image

我该怎么做?

谢谢。

【问题讨论】:

  • 请描述何时触发此操作 - 例如:woocommerce_checkout_create_order 钩子在任何数据库交互之前触发,因此无法通过 $order->get_items() 访问订单商品.

标签: php wordpress woocommerce product orders


【解决方案1】:

使用 <pre><?php print_r($items); ?></pre> 检查 $items 数组/对象的所有内容。

【讨论】:

    【解决方案2】:

    2018 年更新:

    • 用 2 种可能的情况说明答案
    • 增加了对 woocommerce 3+ 的兼容性

    所以可能有两种情况:

    1) 获取商品元数据(未在订单商品元数据中设置):

    您需要在 foreach 循环中为 WC_Order 获取产品 ID,并为该产品获取一些元数据,您将使用 get_post_meta() 函数 (但不是 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循环中访问数据:

    // Iterating in an array of keys/values
    foreach( $custom_field as $key => $value ){
        echo '<p>key: '.$key.' | value: '.$value.'</p>';
    } 
    

    所有代码都经过测试并且可以工作。

    订单数据相关参考:

    【讨论】:

    • 您好,感谢您的回复。但是, $item['product_id'] 只能获取 order_id ,对我没有帮助。你可以在上面看到我的图片 (i.stack.imgur.com/8OJ7U.jpg)。我所能得到的只是 meta_value 列中的产品 ID(数字 90),但我需要的是 order_item_id 中的数字(1 或 2),这样我就可以获得 _tmcartepo_data meta_key 的值。有什么想法吗?
    • 我目前在调用 get_items()(WP 4.6.1 和 WC 2.6.4)时遇到“在布尔值上调用成员函数 get_items()”错误。试图找出原因,但您似乎为我指明了正确的方向。
    【解决方案3】:

    聚会迟到了,但使用 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;
    }
    

    经过测试并适用于我的情况。

    【讨论】:

      【解决方案4】:

      $order-&gt;get_items() 上执行foreach 时,它们的键实际上是订单行ID。所以:

      foreach ( $order->get_items() as $key => $item ) {
          $data = wc_get_order_item_meta( $key, '_tmcartepo_data' );
          ...
      }
      

      【讨论】:

        【解决方案5】:
        foreach ( $order->get_items() as $key => $item ) {
        $data = wc_get_order_item_meta( $key, '_tmcartepo_data' );
        ...
        

        这个解决方案对我来说很有价值,为你的 meta_key 更改“_tmcartepo_data”。

        【讨论】:

          【解决方案6】:

          一种从数据库中获取订单商品的简单方法;

          /**
           * @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;
          }
          

          【讨论】:

            猜你喜欢
            • 2018-03-25
            • 1970-01-01
            • 2022-07-08
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-12-26
            • 2019-02-23
            • 2023-04-07
            相关资源
            最近更新 更多