如何在Wordpress的自定义插件中获取订单详细信息


How to get Order detail in custom plugin in Wordpress

实际上我正在创建一个新插件,我想获取order number和所有其他详细信息,但现在只是order number.为此,我使用以下代码,该代码是从Stackoverflow过去的答案中获得的。

 add_action( 'wp_head', 'get_order_num' );
function get_order_num($order_id) {

    global $woocommerce, $order;
$order = new WC_Order($order->ID);
print_r($order);
die();
//to escape # from order id
$order_id = trim(str_replace('#', '', $order->get_order_number()));
echo ($order_id);
}

但这向我显示结果为 0

这是我print_r时显示的结果

 WC_Order Object
(
    [order_type] => simple
    [id] => 0
    [post] => 
    [order_date] => 
    [modified_date] => 
    [customer_message] => 
    [customer_note] => 
    [post_status] => 
    [prices_include_tax] => 
    [tax_display_cart] => excl
    [display_totals_ex_tax] => 1
    [display_cart_ex_tax] => 1
    [formatted_billing_address:protected] => 
    [formatted_shipping_address:protected] => 
)

我知道这不会向我显示数组中的订单号。我想知道如何在

你可以挂woocommerce_thankyou_order_id .它会像这样工作:

function my_beautiful_hook($order_id)
{
//Do some magic here
}
add_action('woocommerce_thankyou_order_id','my_beautiful_hook')