php - Woocommerce 将订单备注添加到失败的订单管理员电子邮件
问题描述
我需要将 woocommerce 中的特定订单注释添加到发送给管理员的失败订单电子邮件中。
我在 functions.php 中使用以下代码,它可以工作 - 除了它将所有评论放在电子邮件中。
我只想将带有文本“错误处理付款等等等等”的注释添加到电子邮件中,例如这个例子:
处理付款时出错。原因:处理器被拒绝 - 可能是被盗的卡
下面是我正在使用的代码 - 我如何使它过滤并仅添加那些包含文本“错误处理付款”而不发送所有评论的注释?
// 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>';
}
你能帮忙吗?
解决方案
您可以使用该函数根据特定的订单 idwc_get_order_notes
获取所有注释。
您将需要使用以下键创建一个数组以用作函数参数:
limit
作为一个空字段(没有限制)order_id
要获取注释的订单 ID
最后,您可以使用 PHPstrpos
函数检查计数是否包含字符串"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>';
}
该代码已经过测试并且可以工作。将它添加到您的活动主题的functions.php。
推荐阅读
- angular - Angular 10 POSTing Sails JS 时禁止 CSRF
- jsonb - 如何在 node-postgres 中正确转义 JSONB 深度查询的参数
- python - 如何将气流连接到minio s3
- javascript - 测试使用插件(vuelayers)的 Nuxt.js 组件时出现问题
- iis - IIS HTTP 错误 404 本地但不在服务器上
- python - 从 url 获取媒体链接,而不是使用 api.user_timeline / Tweepy-Python
- java - 如何使用 th:field 属性将 hashmap 绑定到 thymeleaf 单选按钮
- arrays - 如何重新排列要与兼容邻居订购的阵列?
- android - Android / Kotlin - 如何将 2 个套接字连接在一起(穷人的代理)
- amazon-web-services - Terraform:创建 SG 时属性“入口”的值不合适