[英]Woocommerce add order notes to failed order admin email
I need to add a specific order note in woocommerce to a failed order email to the admin.我需要将 woocommerce 中的特定订单注释添加到管理员的失败订单 email 中。
I am using the following code in functions.php which works - except it places all comments in the email.我在functions.php中使用以下代码,它可以工作-除了它将所有注释放在email中。
I only want notes with the text "Error processing payment blah blah blah" in them to be added to the email such as this example:我只想将带有“错误处理付款等等等等”文本的注释添加到 email 中,例如这个例子:
Error processing payment.处理付款时出错。 Reason: Processor Declined – Possible Stolen Card原因:处理器被拒绝 - 可能是被盗的卡
Below is the code I am using - how do I make it filter and add only those notes which contain the text "Error processing payment" in them and not send all comments?下面是我正在使用的代码 - 我如何使其过滤并仅添加那些包含文本“错误处理付款”而不发送所有评论的注释?
// Add comments to Failed order emails
add_action( 'woocommerce_email_order_meta', 'woo_add_order_notes_to_email', 10, 3 );
function woo_add_order_notes_to_email( $order, $sent_to_admin = true, $plain_text =
false ) {
// You want to send those only to the Admin
if ( ! $sent_to_admin ) {
return;
}
// You can also check which email you are sending, by checking the order status
// Optional, comment it out, if not needed
if ( 'failed' != $order->get_status() ) {
// Will end execution, with everything other than the completed order email.
return;
}
$notes = array();
$args = array(
'post_id' => $order->id,
'approve' => 'approve',
'type' => '' // You don't need type as orders get only order notes as comments.
);
// Remove the filter excluding the comments from queries
remove_filter( 'comments_clauses', array( 'WC_Comments', 'exclude_order_comments' ) );
// You get all notes for the order /public and private/
$notes = get_comments( $args );
// Add the filter again.
add_filter( 'comments_clauses', array( 'WC_Comments', 'exclude_order_comments' ) );
echo '<h4 style="font-family:Arial,sans-serif;font-size:120%;line-height:110%;color:red;">IMPORTANT Order Notes</h4>';
echo '<ul class="order_notes">';
if ( $notes ) {
foreach( $notes as $note ) {
$note_classes = get_comment_meta( $note->comment_ID, 'is_customer_note', true ) ? array( 'customer-note', 'note' ) : array( 'note' );
?>
<li rel="<?php echo absint($note->comment_ID); ?>" class="<?php echo implode(' ', $note_classes); ?>">
<div class="note_content">
<?php echo wpautop( wptexturize( wp_kses_post( $note->comment_content ) ) ); ?>
</div>
</li>
<?php
}
} else {
echo '<li class="no-order-comment">There are no order notes</li>';
}
echo '</ul>';
}
Can you please help?你能帮忙吗?
You can use the wc_get_order_notes
function to get all notes based on a specific order id .您可以使用wc_get_order_notes
function 获取基于特定订单 ID 的所有注释。
You will need to create an array to use as a function parameter with the following keys:您需要使用以下键创建一个数组以用作 function 参数:
limit
as an empty field (no limit) limit
为空字段(无限制)order_id
the order id of which to get notes order_id
获取备注的订单id Finally you can use the PHP strpos
function to check that the count contains the string "Error processing payment" .最后,您可以使用 PHP strpos
function 检查计数是否包含字符串"Error processing payment" 。
// Add comments to Failed order emails
add_action( 'woocommerce_email_order_meta', 'woo_add_order_notes_to_email', 10, 4 );
function woo_add_order_notes_to_email( $order, $sent_to_admin, $plain_text, $email ) {
// You want to send those only to the Admin
if ( ! $sent_to_admin ) {
return;
}
// You can also check which email you are sending, by checking the order status
// Optional, comment it out, if not needed
if ( 'failed' != $order->get_status() ) {
// Will end execution, with everything other than the completed order email.
return;
}
// Get the admin order notes
$args = array(
'limit' => '',
'order_id' => $order->get_id(),
);
$notes = wc_get_order_notes( $args );
// If there are no notes
if ( empty($notes) ) {
echo '<li class="no-order-comment">There are no order notes</li>';
return;
}
echo '<h4 style="font-family:Arial,sans-serif;font-size:120%;line-height:110%;color:red;">IMPORTANT Order Notes</h4>';
echo '<ul class="order_notes">';
foreach( $notes as $note ) {
// Print only the note containing the string "Error processing payment"
if ( strpos( $note->content, 'Error processing payment' ) !== false ) {
$note_classes = $note->customer_note ? array( 'customer-note', 'note' ) : array( 'note' );
?>
<li rel="<?php echo absint($note->id); ?>" class="<?php echo implode(' ', $note_classes); ?>">
<div class="note_content">
<?php echo wpautop( wptexturize( wp_kses_post( $note->content ) ) ); ?>
</div>
</li>
<?php
}
}
echo '</ul>';
}
The code has been tested and works.该代码已经过测试并且可以工作。 Add it to your active theme's functions.php.将其添加到您的活动主题的功能中。php。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.