从Woocommerce中的订单ID获取产品ID
2 个回答
- 投票数
-
- 2013-04-25
WooCommerce 3.0 +
您可以通过以下方式获取订单的订单项
$order = wc_get_order( $order_id ); $items = $order->get_items();
然后,如果您遍历所有项目,则可以获得所有相关数据:
foreach ( $items as $item ) { $product_name = $item->get_name(); $product_id = $item->get_product_id(); $product_variation_id = $item->get_variation_id(); }
一个很好的提示是检查管理员订单页面如何获取数据,在那里您会找到很多答案!
WooCommerce 3.0之前版本
$order = new WC_Order( $order_id ); $items = $order->get_items(); foreach ( $items as $item ) { $product_name = $item['name']; $product_id = $item['product_id']; $product_variation_id = $item['variation_id']; }
WooCommerce 3.0+
you can get the order items of an order by
$order = wc_get_order( $order_id ); $items = $order->get_items();
then if you loop through the items, you can get all the relevant data:
foreach ( $items as $item ) { $product_name = $item->get_name(); $product_id = $item->get_product_id(); $product_variation_id = $item->get_variation_id(); }
a good tip is to check how the admin order pages get the data, you'll find many answers there!
Pre-WooCommerce 3.0
$order = new WC_Order( $order_id ); $items = $order->get_items(); foreach ( $items as $item ) { $product_name = $item['name']; $product_id = $item['product_id']; $product_variation_id = $item['variation_id']; }
-
除非我缺少任何东西,否则在最新版本的WooCommerce中似乎无法使用...Unless I'm missing something, this doesn't appear to work in the latest version of WooCommerce...
- 0
- 2015-11-09
- rnevius
-
对于我来说,它仍然可以在WooCommerce 2.4.8中使用,但是您需要定义$ order_id变量(有时它位于$ order->id中,具体取决于您的上下文).Still works in WooCommerce 2.4.8 for me, but you need to have the $order_id variable defined (sometimes it's in $order->id, depending on your context).
- 0
- 2015-11-10
- Ewout
-
@mevius我为3+添加了带有调度功能的编辑,用于检查多个产品@mevius i added an edit for 3+ with a dispatch function for checking multiple products
- 0
- 2017-10-20
- Garet Claborn
-
请参阅调度程序的编辑历史记录,如果没有它就不好了,因为它会毫无意义地吞噬您的服务器see edit history for dispatcher, this is not good without it as it will eat up your server pointlessly
- 0
- 2017-11-22
- Garet Claborn
-
@GaretClaborn完全取决于您对这些数据的处理方式."按原样",此示例代码片段不会以任何方式浪费内存.@GaretClaborn that depends entirely on what you're doing with this data. 'as is', this example snippet is not wasteful of memory in any way.
- 0
- 2017-11-27
- Ewout
-
如果要检查多个产品,那么您将重复很多逻辑,而无法利用循环的第一次运行If you are checking multiple products then you're repeating quite a bit of logic vs being able to capitalize on the first run of the loop
- 0
- 2017-12-19
- Garet Claborn
-
- 2013-04-25
我努力了,并取得了一些成就.我想与其他开发者分享.这不是首选方法,但是出于知识,我正在发布答案.
global $wpdb; $result = $wpdb->get_results('select t1.order_item_id, t2.* FROM wp_woocommerce_order_items as t1 JOIN wp_woocommerce_order_itemmeta as t2 ON t1.order_item_id = t2.order_item_id where t1.order_id='.$order->ID); echo '<pre>'; print_r($result); echo '</pre>';
希望会帮助某人.
此外:
最好使用wordpress表前缀来避免在多个网站或迁移过程中出现问题.
global $wpdb; $table_name = $wpdb->prefix . 'table_name';
I worked on it and achieved something. That I would like to share to other developers. This is not preferred way to do it, but for knowledge I am posting my answer.
global $wpdb; $result = $wpdb->get_results('select t1.order_item_id, t2.* FROM wp_woocommerce_order_items as t1 JOIN wp_woocommerce_order_itemmeta as t2 ON t1.order_item_id = t2.order_item_id where t1.order_id='.$order->ID); echo '<pre>'; print_r($result); echo '</pre>';
hope will help someone.
Additionally:
Better to use wordpress table prefix to avoid problems in multiple website or in migration etc.
global $wpdb; $table_name = $wpdb->prefix . 'table_name';
-
@ErenorPaz谢谢,我在回答中添加了内容,以回复您的评论:)@ErenorPaz Thanks, I added content in answer, in reply to your comment :)
- 1
- 2016-10-25
- arslaan ejaz
-
谢谢您即使在旧线程上也能快速响应!我将删除以前的评论,因为它已经过时了:)Thank you for the fast response, even on an old thread! I'll delete my previous comments, since it's outdated now :)
- 0
- 2016-10-25
- Erenor Paz
我在Woocommerce产品详细信息和订单详细信息关系方面遇到麻烦.我无法在Woocommerce主题的查看订单页面上找到相关订单ID的产品ID.我只想在查看订单页面上获取产品内容和永久链接等.
我尝试在
wp_postmeta
中搜索,但是没有运气.