[英]Display product custom field via "woocommerce_email_order_meta" hook in WooCommerce admin email notification
My products have a custom field called depot .我的产品有一个名为depot的自定义字段。 I need to display this field only in admin mail notifications, not to the customer order recap or customer mail notifications.我只需要在管理员邮件通知中显示此字段,而不是在客户订单回顾或客户邮件通知中显示。
I use this code to retrieve the custom field in email notifications :我使用此代码来检索电子邮件通知中的自定义字段:
add_action( 'woocommerce_email_order_meta', 'add_depot', 10, 3 );
function add_depot( $order, $sent_to_admin, $plain_text ){
$items = $order->get_items();
foreach ( $items as $item ){
$depot = $item->get_meta('depot');
$item['name'] = 'Dépôt: ' . $depot . $item['name'];
}
}
At the moment, it displays the field only in customer emails and not in admin emails as I would like.目前,它仅在客户电子邮件中显示该字段,而不是像我希望的那样在管理电子邮件中显示。 I think I need to use $sent_to_admin but I'm not sure how.我想我需要使用$sent_to_admin但我不确定如何使用。
Thank you.谢谢你。
The woocommerce_email_order_meta
hook has 4 arguments, via $email->id
you can target specific email notifications woocommerce_email_order_meta
钩子有 4 个参数,通过$email->id
你可以定位特定的电子邮件通知
So you get:所以你得到:
function action_woocommerce_email_order_meta( $order, $sent_to_admin, $plain_text, $email ) {
// Targetting specific email notifications
$email_ids = array( 'new_order' );
// Checks if a value exists in an array
if ( in_array( $email->id, $email_ids ) ) {
// Get items
$items = $order->get_items();
// Loop trough
foreach ( $items as $item ) {
// Get meta
$depot = $item->get_meta( 'depot' );
// NOT empty
if ( ! empty ( $depot ) ) {
echo '<p style="color:green;font-size:50px;">Dépôt: ' . $depot . ' - ' . $item->get_name() . '</p>';
} else {
echo '<p style="color:red;font-size:50px;">Data not found!</p>';
}
}
}
}
add_action( 'woocommerce_email_order_meta', 'action_woocommerce_email_order_meta', 10, 4 );
You can implement this hook:你可以实现这个钩子:
do_action( 'woocommerce_email_order_meta', $order, $sent_to_admin, $plain_text, $email );
If you see, the second parameter which you get in arguments is - sent to admin.如果您看到,您在参数中获得的第二个参数是 - 发送给管理员。 So if that is true, then you can add your custom meta.因此,如果这是真的,那么您可以添加自定义元。
Let me know if that helps.如果这有帮助,请告诉我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.