付款前先在结帐页面中获取订单ID [英] Get the order ID in checkout page before payment process

查看:135
本文介绍了付款前先在结帐页面中获取订单ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在WooCommerce中,我需要在WooCoommerce的结帐页面上获得 order ID ,即在创建订单后付款之前.

In WooCommerce, I need to get order ID right in checkout page of WooCoommerce, before payment, when the order is created.

我查看了所有会话,并尝试找出订单何时在 order_awaiting_payment 会话中付款,但是在付款之前我需要它.
因此,我想到了一种解决方案,该解决方案是在加载结帐页面时(实际上是准备好付款)并在结帐页面真正完成时进行订购.

I look at all sessions and tried to find out when order goes for payment in order_awaiting_payment session, but I need it before going for payment.
So I think about a solution that is make order when checkout page loaded (In fact make it ready for payment) and when checkout real complete update it.

如何在WooCommerce进行付款之前在结帐页面中获取订单ID?

How to get order ID in checkout page before order go for payment in WooCommerce?

我认为对此有一些迷惑,但我找不到它.

I think there is some hook for this but I can't find it.

推荐答案

您可以使用挂钩在

You can use a custom function hooked in woocommerce_checkout_order_processed action hook.
Since woocommerce 3.0+ version, here Is the corresponding core code located in process_checkout() function.

// Since WooCommerce version 3.0+
do_action( 'woocommerce_checkout_order_processed', $order_id, $posted_data, $order );

及以下WooCommerce 3.0版本:

and below WooCommerce 3.0 version:

// Since WooCommerce version 2.1+ (before version 3.0+)
do_action( 'woocommerce_checkout_order_processed', $order_id, $this->posted );

因此,有2种情况取决于您使用的woocommerce版本:

So there is 2 cases depending which version of woocommerce you are using:

自WooCommerce 3.0 + 起,您可以在挂钩函数中使用2个其他参数,并且在获得时无需创建订单对象的实例 $order 已经作为参数.
您还可以通过 $posted_data 自变量直接访问发布的数据.

Since WooCommerce 3.0+ you can use 2 additional arguments in your hooked function and you will not need to create an instance of the order object as you get $order already as an argument.
You will be able also to access the posted data directly through $posted_data argument.

add_action('woocommerce_checkout_order_processed', 'action_checkout_order_processed', 10, 3);
function action_checkout_order_processed( $order_id, $posted_data, $order ) {
   // Do something
}

由于WooCommerce 2.1 + (在WooCommerce 3.0之前),您仅将 $order_id 作为参数,因此您可能需要使用 获取 $order 对象的实例wc_get_order() 功能:

Since WooCommerce 2.1+ (Before WooCommerce 3.0), you have only the $order_id as argument, so you might be need to get an instance of $order object with wc_get_order() function:

add_action('woocommerce_checkout_order_processed', 'action_checkout_order_processed', 10, 1);
function action_checkout_order_processed( $order_id ) {
   // get an instance of the order object
   $order = wc_get_order( $order_id );

   // Do something
}

该代码会出现在您活动的子主题(或主题)的function.php文件中,也可能会出现在任何插件文件中.

这篇关于付款前先在结帐页面中获取订单ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆