問題描述
在 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 withwc_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的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!