在新订单挂钩中获取订单数据
Fetching order data in new order hook
我试图在每次下订单后给自己发送一封电子邮件。我遇到的问题是 $order->get_total()
以及 get_total_tax
return 0 而不是实际订单总价值。
add_action( 'woocommerce_new_order', 'custom_after_order_created_hook', 12 , 1);
function custom_after_order_created_hook($order_id) {
$order = new WC_Order($order_id);
$with_tax = $order->get_total();
$tax = $order->get_total_tax();
$without_tax = $with_tax - $tax;
$to = "test@example.com";
$subject = "New order";
$content = "
New order {$order->id}
With tax: {$with_tax}
Without tax: {$without_tax}
Tax: {$tax}
";
$status = wp_mail($to, $subject, $content);
}
除了 $order_id 和 $order->id
之外的每个值都被评估为 0。$order_id 具有正确的值。此问题仅在使用 woocommerce_new_order
挂钩时发生(我也尝试在自定义页面上使用它 - 正常工作),这让我感到奇怪。
我不确定这里的问题是什么,我的代码的某些部分是异步的吗?
或者这个挂钩可能在订单更新为价格 paid/tax 信息之前被调用?
我应该怎么做才能在此处获取价格信息?
谢谢。
此 woocommerce_new_order 动作挂钩用于更改 create_order() 函数。因此,您最好使用 woocommerce_thankyou
操作挂钩,它会在创建订单时触发您的自定义电子邮件通知:
// Tested on WooCommerce versions 2.6+ and 3.0+
add_action( 'woocommerce_thankyou', 'new_order_custom_email_notification', 1, 1 );
function new_order_custom_email_notification( $order_id ) {
if ( ! $order_id ) return;
// Getting an instance of WC_Order object
$order = wc_get_order( $order_id );
$with_tax = $order->get_total();
$tax = $order->get_total_tax();
$without_tax = $with_tax - $tax;
$to = "test@example.com";
$subject = "New order";
$content = "
New order {$order_id}
With tax: {$with_tax}
Without tax: {$without_tax}
Tax: {$tax}
";
wp_mail($to, $subject, $content);
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
代码已经过测试并且可以工作。
Using woocommerce_checkout_order_processed
action hook instead of woocommerce_thankyou
action hook is also a good alternative, may be even better. You have just to replace:
add_action( 'woocommerce_thankyou', 'new_order_custom_email_notification', 1, 1 );
By:
add_action( 'woocommerce_checkout_order_processed', 'new_order_custom_email_notification', 1, 1 );
类似的工作答案:
The woocommerce_checkout_order_processed
hook (located in WC_Checkout process_checkout()
method that could be convenient too for this purpose.
The source code of WC_Checkout process_checkout()
method is interesting to get a view on the purchase flow.
我试图在每次下订单后给自己发送一封电子邮件。我遇到的问题是 $order->get_total()
以及 get_total_tax
return 0 而不是实际订单总价值。
add_action( 'woocommerce_new_order', 'custom_after_order_created_hook', 12 , 1);
function custom_after_order_created_hook($order_id) {
$order = new WC_Order($order_id);
$with_tax = $order->get_total();
$tax = $order->get_total_tax();
$without_tax = $with_tax - $tax;
$to = "test@example.com";
$subject = "New order";
$content = "
New order {$order->id}
With tax: {$with_tax}
Without tax: {$without_tax}
Tax: {$tax}
";
$status = wp_mail($to, $subject, $content);
}
除了 $order_id 和 $order->id
之外的每个值都被评估为 0。$order_id 具有正确的值。此问题仅在使用 woocommerce_new_order
挂钩时发生(我也尝试在自定义页面上使用它 - 正常工作),这让我感到奇怪。
我不确定这里的问题是什么,我的代码的某些部分是异步的吗?
或者这个挂钩可能在订单更新为价格 paid/tax 信息之前被调用?
我应该怎么做才能在此处获取价格信息?
谢谢。
此 woocommerce_new_order 动作挂钩用于更改 create_order() 函数。因此,您最好使用 woocommerce_thankyou
操作挂钩,它会在创建订单时触发您的自定义电子邮件通知:
// Tested on WooCommerce versions 2.6+ and 3.0+
add_action( 'woocommerce_thankyou', 'new_order_custom_email_notification', 1, 1 );
function new_order_custom_email_notification( $order_id ) {
if ( ! $order_id ) return;
// Getting an instance of WC_Order object
$order = wc_get_order( $order_id );
$with_tax = $order->get_total();
$tax = $order->get_total_tax();
$without_tax = $with_tax - $tax;
$to = "test@example.com";
$subject = "New order";
$content = "
New order {$order_id}
With tax: {$with_tax}
Without tax: {$without_tax}
Tax: {$tax}
";
wp_mail($to, $subject, $content);
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
代码已经过测试并且可以工作。
Using
woocommerce_checkout_order_processed
action hook instead ofwoocommerce_thankyou
action hook is also a good alternative, may be even better. You have just to replace:add_action( 'woocommerce_thankyou', 'new_order_custom_email_notification', 1, 1 );
By:
add_action( 'woocommerce_checkout_order_processed', 'new_order_custom_email_notification', 1, 1 );
类似的工作答案:
The
woocommerce_checkout_order_processed
hook (located inWC_Checkout process_checkout()
method that could be convenient too for this purpose.The source code of
WC_Checkout process_checkout()
method is interesting to get a view on the purchase flow.