php 成功结帐挂钩后获取订单数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42530626/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting order data after successful checkout hook
提问by Syed Haris Ali Ghaznavi
In WooCommerce, I would like to send a request to an API once the customer has successfully checked out. Its basically a website where the client is selling online courses (Like udemy).
在 WooCommerce 中,一旦客户成功结帐,我想向 API 发送请求。它基本上是一个客户销售在线课程的网站(如udemy)。
When the customer checks out, I would like to send an API request and enroll the user for that particular course. I have tried several WooCommerce hooks but none worked for me.
当客户结账时,我想发送一个 API 请求并为该特定课程注册用户。我尝试了几个 WooCommerce 挂钩,但没有一个对我有用。
This is the code that I'm using:
这是我正在使用的代码:
add_action('woocommerce_checkout_order_processed', 'enroll_student', 10, 1);
function enroll_student($order_id)
{
echo $order_id;
echo "Hooked";
}
I am writing this code inside an activated plugin and for ease, I am currently using Cash on Delivery method.
我正在一个激活的插件中编写此代码,为方便起见,我目前使用的是货到付款方法。
Can anyone point me out where I am going wrong because when I checkout I cant see the message "hooked" that I am printing nor the $order_id
?
任何人都可以指出我哪里出错了,因为当我结帐时,我看不到我正在打印的“钩住”消息,也看不到$order_id
?
It takes me to the success page and doesn't show these two things that I am printing.
它带我到成功页面,并没有显示我正在打印的这两件事。
回答by LoicTheAztec
Update 2Only For Woocommerce 3+ (added restriction to execute the code only once)
更新 2仅适用于 Woocommerce 3+ (添加了仅执行一次代码的限制)
add_action('woocommerce_thankyou', 'enroll_student', 10, 1);
function enroll_student( $order_id ) {
if ( ! $order_id )
return;
// Allow code execution only once
if( ! get_post_meta( $order_id, '_thankyou_action_done', true ) ) {
// Get an instance of the WC_Order object
$order = wc_get_order( $order_id );
// Get the order key
$order_key = $order->get_order_key();
// Get the order number
$order_key = $order->get_order_number();
if($order->is_paid())
$paid = __('yes');
else
$paid = __('no');
// Loop through order items
foreach ( $order->get_items() as $item_id => $item ) {
// Get the product object
$product = $item->get_product();
// Get the product Id
$product_id = $product->get_id();
// Get the product name
$product_id = $item->get_name();
}
// Output some data
echo '<p>Order ID: '. $order_id . ' — Order Status: ' . $order->get_status() . ' — Order is paid: ' . $paid . '</p>';
// Flag the action as done (to avoid repetitions on reload for example)
$order->update_meta_data( '_thankyou_action_done', true );
$order->save();
}
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
代码位于活动子主题(或主题)的 function.php 文件或任何插件文件中。
Related thread:
相关线程:
The code is tested and works.
该代码经过测试并有效。
Updated(to get the product Idfrom Orders items as asked in your comment)
更新(从您的评论中要求的订单项目中获取产品 ID)
May be you could use woocommerce_thankyou
hook instead, that will display on order-received page your echoed code, this way:
也许你可以使用woocommerce_thankyou
钩子代替,它会在订单接收页面上显示你的回显代码,这样:
add_action('woocommerce_thankyou', 'enroll_student', 10, 1);
function enroll_student( $order_id ) {
if ( ! $order_id )
return;
// Getting an instance of the order object
$order = wc_get_order( $order_id );
if($order->is_paid())
$paid = 'yes';
else
$paid = 'no';
// iterating through each order items (getting product ID and the product object)
// (work for simple and variable products)
foreach ( $order->get_items() as $item_id => $item ) {
if( $item['variation_id'] > 0 ){
$product_id = $item['variation_id']; // variable product
} else {
$product_id = $item['product_id']; // simple product
}
// Get the product object
$product = wc_get_product( $product_id );
}
// Ouptput some data
echo '<p>Order ID: '. $order_id . ' — Order Status: ' . $order->get_status() . ' — Order is paid: ' . $paid . '</p>';
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
代码位于活动子主题(或主题)的 function.php 文件或任何插件文件中。
The code is tested and works.
该代码经过测试并有效。
Then you can use all class
WC_Abstract_Order
methods on the$order
object.
然后您可以
WC_Abstract_Order
在$order
对象上使用所有类方法。
Related:
有关的:
回答by mujuonly
you can get the order items of an order by
您可以通过以下方式获取订单的订单项目
// Getting an instance of the order object
$order = new WC_Order( $order_id );
$items = $order->get_items();
//Loop through them, you can get all the relevant data:
foreach ( $items as $item ) {
$product_name = $item['name'];
$product_id = $item['product_id'];
$product_variation_id = $item['variation_id'];
}