在电子邮件模板挂钩中获取WooCommerce订单ID



有人知道如何通过挂钩获得定制电子邮件模板的订单id吗?

我正在使用一个自定义的API,它根据订单id返回账单,我还试图将其打印在电子邮件模板中。

这就是我迄今为止所尝试的:

add_action( 'woocommerce_email_before_order_table', function ( $order, $sent_to_admin, $plain_text, $email ) {
echo '<pre>' . FS4E_VSDC::fs4e_get_order_slip( $order->get_id() ) . '</pre>';
}, 10, 4 );

那样的话,我就不会收到账单。但如果我硬编码订单id,那么一切都正常。

编辑

当我转储$order时,我得到了完整的对象:

["items":protected]=>
array(5) {
["line_items"]=>
array(3) {
[118]=>
object(WC_Order_Item_Product)#2019 (11) {
["extra_data":protected]=>
array(9) {
["product_id"]=>
int(0)
["variation_id"]=>
int(0)
["quantity"]=>
int(1)
["tax_class"]=>
string(0) ""
["subtotal"]=>
int(0)
["subtotal_tax"]=>
int(0)
["total"]=>
int(0)
["total_tax"]=>
int(0)
["taxes"]=>
array(2) {
["subtotal"]=>
array(0) {
}
["total"]=>
array(0) {
}
}
}
["data":protected]=>
array(11) {
["order_id"]=>
int(55)
["name"]=>
string(9) "Product 1"
["product_id"]=>
int(11)
["variation_id"]=>
int(0)
["quantity"]=>
int(1)
["tax_class"]=>
string(0) ""
["subtotal"]=>
string(1) "0"
["subtotal_tax"]=>
string(1) "0"
["total"]=>
string(1) "0"
["total_tax"]=>
string(1) "0"
["taxes"]=>
array(2) {
["total"]=>
array(0) {
}
["subtotal"]=>
array(0) {
}
}
}
["cache_group":protected]=>
string(11) "order-items"
["meta_type":protected]=>
string(10) "order_item"
["object_type":protected]=>
string(10) "order_item"
["id":protected]=>
int(118)
["changes":protected]=>
array(0) {
}
["object_read":protected]=>
bool(true)
["default_data":protected]=>
array(11) {

但当我尝试以以下方式访问它时:

add_action( 'woocommerce_email_before_order_table', function ( $order, $sent_to_admin, $plain_text, $email ) {
echo '<pre>' . FS4E_VSDC::fs4e_get_order_slip( $order[order_id] ) . '</pre>';
}, 10, 4 );

我遇到内部服务器错误。

感谢

试试这个,

add_action( 'woocommerce_email_before_order_table', function ( $order, $sent_to_admin, $plain_text, $email ) {
$order = wc_get_order( $order_id );
$order_data = $order->get_data(); // The Order data
$order_id = $order_data['id'];
echo '<pre>' . FS4E_VSDC::fs4e_get_order_slip($order_id ) . '</pre>';
}, 10, 4 );

如CBroe所述,验证订单中包含的内容。

我记得当我使用WooCommerce时,$order->get_id()是帖子的id,我必须使用$order->get_order_number()

有问题的API可能需要订单号,而不是帖子id。更改

FS4E_VSDC::fs4e_get_order_slip( $order->get_id() ) 

FS4E_VSDC::fs4e_get_order_slip( $order->get_order_number() ) 

最新更新