【发布时间】:2017-09-11 00:10:19
【问题描述】:
我正在尝试在每次下订单后给自己发送一封电子邮件。我遇到的问题是 $order->get_total() 和 get_total_tax 返回 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 钩子时才会出现此问题(我也尝试在自定义页面上使用它 - 可以正常工作),这让我想知道。
我不确定这里有什么问题,我的代码的某些部分是异步的吗?
或者,在订单更新为支付价格/税收信息之前,可能会调用这个钩子?
我应该怎么做才能在此处获取价格信息?
谢谢。
【问题讨论】:
-
哪个版本的WC?
标签: php wordpress woocommerce hook-woocommerce orders