【问题标题】:How can I get the order Id in woocommerce_payment_complete hook [duplicate]如何在 woocommerce_payment_complete 挂钩中获取订单 ID [重复]
【发布时间】:2020-10-29 23:44:39
【问题描述】:
我已经做了一个WooCommerce订单支付成功的功能。
add_action( 'woocommerce_payment_complete', 'do_it' );
function do_it( $order_id ){
// Here will run the function
$order_id="//I want the order id here of woocommerce";
$order = wc_get_order( $order_id ); //Then I can get order details from here
}
如何使用这个钩子在此处获取 $order_id?
【问题讨论】:
标签:
php
wordpress
woocommerce
hook-woocommerce
orders
【解决方案1】:
您的函数中已经有订单 ID 作为参数。
add_action('woocommerce_payment_complete', 'do_it', 10, 1);
function do_it($order_id) {
$order = new WC_Order( $order_id );
$myuser_id = (int)$order->user_id;
$user_info = get_userdata($myuser_id);
$items = $order->get_items();
foreach ($items as $item) {
if ($item['product_id']==24) {
// Do something clever
}
}
return $order_id;
}
【解决方案2】:
试试这个。
add_action( 'woocommerce_payment_complete', 'do_it' );
function do_it( $orderId ){
// Get an instance of the WC_Order object (same as before)
$order = wc_get_order( $order_id );
$orderId = $order->get_id(); // Get the order ID
}