wordpress 如何获取 order_item_id WooCommerce

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19499330/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 19:23:39  来源:igfitidea点击:

How to get order_item_id WooCommerce

wordpresswoocommerce

提问by user2903890

I need to be able to get order_item_id, the unique value applied to each item in each order. This is what I have so far:

我需要能够获得 order_item_id,这是应用于每个订单中每个项目的唯一值。这是我到目前为止:

        global $wpdb;
    $order = new WC_Order( $order_id );
    $items = $order->get_items(); 
        foreach ( $items as $item ) {
            $product_id = $item['product_id'];
            $item_id = $item['item_id'];

The last line in the above code gets order_item_id. It normally wouldn't work but it works because I've edited get_items in class-wc-order and included:

上面代码中的最后一行获取 order_item_id。它通常不起作用,但它起作用,因为我已经在 class-wc-order 中编辑了 get_items 并包括:

        $items[ $item->order_item_id ]['item_id'] = $item->order_item_id;

What I want to know is how can I get order_item_id without having to edit class-wc-order. Is there any easy way?

我想知道的是如何在不必编辑 class-wc-order 的情况下获得 order_item_id。有什么简便的方法吗?

Thanks!

谢谢!

回答by user3367143

This may be already too late for your project but may be useful for others:

这对于您的项目来说可能已经太晚了,但可能对其他人有用:

foreach ($items as $key => $product ) 

The $keyvariable is the item_idyou are looking for.

$key变量是item_id你所期待的。

回答by user3481396

The index/key of the array returned by $order->get_items()is the order_item_id... so try this:

返回的数组的索引/键$order->get_items()order_item_id... 所以试试这个:

foreach ($order_items as $order_item_id => $order_item) { 
}