【发布时间】:2021-05-10 17:36:40
【问题描述】:
我需要在 woocommerce 中将特定订单备注添加到发送给管理员的失败订单电子邮件中。
我在 functions.php 中使用以下代码,它可以工作 - 除了它将所有 cmets 放在电子邮件中。
我只想将带有“错误处理付款等等等等”文本的注释添加到电子邮件中,例如以下示例:
处理付款时出错。原因:处理器被拒绝 - 可能是卡被盗
以下是我正在使用的代码 - 我如何使其过滤并仅添加那些包含文本“错误处理付款”的注释而不发送所有 cmets?
// 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>';
}
你能帮忙吗?
【问题讨论】:
标签: php woocommerce