Last active
November 14, 2022 13:02
-
-
Save vanbo/b10de09ad367275d2bff to your computer and use it in GitHub Desktop.
WooCommerce: Add Order notes to admin emails
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ( 'completed' != $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;">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>'; | |
} |
Great stuff - Can you help me, I get how to make it only work on failed orders - but how would I get it to only add notes with certain text...
for example
I only want notes with the text "Error processing payment" in them to be added to the email such as this example:
Error processing payment. Reason: Processor Declined – Possible Stolen Card
how do I make add only those notes which contain the text "Error processing payment" in them and not send all comments?
you should change only 11 line like this if ( 'failed' != $order->get_status() )
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great stuff - Can you help me, I get how to make it only work on failed orders - but how would I get it to only add notes with certain text...
for example
I only want notes with the text "Error processing payment" in them to be added to the email such as this example:
Error processing payment. Reason: Processor Declined – Possible Stolen Card
how do I make add only those notes which contain the text "Error processing payment" in them and not send all comments?