wordpress 如何在 WooCommerce 中获取订单 ID?

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

How can I get the order ID in WooCommerce?

wordpresswoocommerce

提问by moslem

How do I retrieve the order ID in WooCommerce?

如何在 WooCommerce 中检索订单 ID?

回答by Elvis Fernandes

This is quite an old question now, but someone may come here looking for an answer:

现在这是一个相当古老的问题,但有人可能会来这里寻找答案:

echo $order->id;

This should return the order id without "#".

这应该返回没有“#”的订单 ID。

EDIT (feb/2018)

编辑(2018 年 2 月)

The current way of accomplishing this is by using:

目前实现这一点的方法是使用:

$order->get_id();

回答by Dip

it worked. Just modified it

有效。刚刚修改了

global $woocommerce, $post;

$order = new WC_Order($post->ID);

//to escape # from order id 

$order_id = trim(str_replace('#', '', $order->get_order_number()));

回答by rgdesign

I didnt test it and dont know were you need it, but:

我没有测试它,不知道你是否需要它,但是:

$order = new WC_Order(post->ID);
echo $order->get_order_number();

Let me know if it works. I belive order number echoes with the "#" but you can split that if only need only the number.

让我知道它是否有效。我相信订单号与“#”相呼应,但如果只需要数字,您可以拆分它。

回答by piersb

$order = new WC_Order( $post_id ); 

If you

如果你

echo $order->id;

then you'll be returned the id of the post from which the order is made. As you've already got that, it's probably not what you want.

然后您将返回下订单的帖子的 ID。由于您已经知道了,这可能不是您想要的。

echo $order->get_order_number();

will return the id of the order (with a # in front of it). To get rid of the #,

将返回订单的 id(前面有一个 #)。为了摆脱#,

echo trim( str_replace( '#', '', $order->get_order_number() ) );

as per the accepted answer.

根据接受的答案。

回答by Mindaugas Dobilas

As of woocommerce 3.0

从 woocommerce 3.0 开始

$order->id;

will not work, it will generate notice, use getter function:

不起作用,它会生成通知,使用 getter 函数:

$order->get_id();

The same applies for other woocommerce objects like procut.

这同样适用于其他 woocommerce 对象,如 procut。