如何从 WooCommerce 中的订单密钥获取订单 ID?

How can I get the order ID from the order Key in WooCommerce?

如何从 WooCommerce 中的订单 KEY 检索订单 ID? (PHP 代码)

例如

我有看起来像 'wc_order_qkTc2RVyGtVil' 的订单 KEY,我想获得看起来像“1950”的订单 ID。

背景: 我只得到一个包含订单 KEY 的 link。这就是为什么我不能使用订单 OBJECT 来获取 ID。

在已收到订单、订单付款和查看订单页面上,您可以从 URL 获取订单 ID,例如:

1).收到订单(谢谢)页面:

$order_id = absint( get_query_var('order-received') );

2).在订单支付页面:

$order_id = absint( get_query_var('order-pay') );

3).在查看订单页面上:

$order_id = absint( get_query_var('view-order') );

现在您可以使用 wc_get_order_id_by_order_key() WooCommerce 功能,例如:

$order_id = wc_get_order_id_by_order_key( $order_key );