将 WooCommerce 订单 ID 传递给钩子 [英] Passing WooCommerce Order ID into a hook
问题描述
我在 WooCommerce 自定义中经常遇到一个问题:如何在函数中获取当前订单 ID.
I have a recurring issue in my WooCommerce customizations: how to get the current order id inside a function.
一些钩子可以用 $order_id 传递它,但是在用户帐户页面的特定订单的上下文中,例如,下面的不起作用.
Some hooks can pass it with $order_id, but in the context of a user's account page for a specific order, for example, the below doesn't work.
当然,问题是 $order_id
是空的.但是我如何将订单的 ID 传递给这个函数呢?
The problem is that $order_id
is empty, of course. But how would I pass the ID of the order into this function?
add_action( 'woocommerce_order_items_table', 'xcsn_woocommerce_order_items_table');
function xcsn_woocommerce_order_items_table ( $order_id ) {
$order = new WC_Order( $order_id );
echo 'The order ID is: ' . $order->get_order_number();
// or
echo '<br>The order ID is: ' . $order->id;
}
我试过了,但它返回的是用户 ID 号,而不是订单:
I tried this, but it returns the user id number, not the order:
global $woocommerce;
$order = new WC_Order($post->ID);
echo 'The Order ID: ' . $order->get_order_number();
有什么建议吗?
推荐答案
add_action( 'woocommerce_order_items_table', 'xcsn_woocommerce_order_items_table',10,2);
function xcsn_woocommerce_order_items_table ( $order_id ) {
$order = new WC_Order( $order_id );
echo 'The order ID is: ' . $order->get_order_number();
// or
echo '<br>
The order ID is: ' . $order_id;
}
检查这个你需要在动作上添加10,2,它在成功提交订单后返回订单ID,没有这个功能将在提交订单之前运行,所以订单ID可能0
或 null
check this you need to add 10,2 on the action, that it returns the order id on after the successful order submission, without this the function will run before submit the order, so the order id maybe 0
or null
这篇关于将 WooCommerce 订单 ID 传递给钩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!