在付款过程之前在结帐页面中获取订单 ID

时间:2023-01-16
本文介绍了在付款过程之前在结帐页面中获取订单 ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

在 WooCommerce 中,我需要在创建订单时订单 ID 在付款前在 WooCoommerce 的结帐页面中获得正确的信息.

我查看了所有会话,并试图在 order_awaiting_payment 会话中找出订单何时付款,但我在付款前需要它.
所以我想到了一个解决方案,即在结账页面加载时下订单(实际上是准备付款),并在结账真正完成时更新它.

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

我认为有一些钩子,但我找不到.

解决方案

您可以使用挂接在 woocommerce_checkout_order_processed 动作挂钩.
由于woocommerce 3.0+版本,这里是位于process_checkout() 函数.

//从 WooCommerce 3.0+ 版本开始do_action('woocommerce_checkout_order_processed', $order_id, $posted_data, $order );

及以下 WooCommerce 3.0 版本:

//从 WooCommerce 2.1+ 版开始(3.0+ 版之前)do_action('woocommerce_checkout_order_processed', $order_id, $this->posted );

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

<块引用>

从 WooCommerce 3.0+ 开始,您可以在挂钩函数中使用 2 个额外的参数并且您将无需创建订单对象的实例$order 已经作为参数.
您还可以通过 $posted_data 参数直接访问发布的数据.

add_action('woocommerce_checkout_order_processed', 'action_checkout_order_processed', 10, 3);函数 action_checkout_order_processed( $order_id, $posted_data, $order ) {//做一点事}

<块引用>

自 WooCommerce 2.1+ (WooCommerce 3.0 之前),您只有 $order_id 作为参数,因此您可能需要使用 $order 对象的实例.html" rel="noreferrer">wc_get_order() 函数:

add_action('woocommerce_checkout_order_processed', 'action_checkout_order_processed', 10, 1);函数 action_checkout_order_processed( $order_id ) {//获取订单对象的实例$order = wc_get_order( $order_id );//做一点事}

代码位于活动子主题(或主题)的 function.php 文件或任何插件文件中.

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

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.

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 );

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 );

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

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
}

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
}

The Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

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

上一篇:根据 Woocommerce 中的特定购物车商品数量自动应用优惠券 下一篇:根据 Woocommerce 中的付款方式停止特定客户的电子邮件通知

相关文章

最新文章