I am at my wits end here and have tried every solution I could find online for days and nothing works. The worst part is the problem should be solvable in about 4 lines of code but it just isn’t working.
What I need: When the order completed email goes out I want the order notes (not the customer notes, the actual Order Notes) added into the email. I can filter through them after but I cannot seem to get the notes to appear in the email at all. This is an example of an order note…on an order:
so far I have tried this code:
::PHP::
<?php $comments = $order->get_customer_order_notes(); if($comments){ echo '<h2>' . _e( 'Order Notes', 'woocommerce' ) . '</h2>'; foreach($comments as $comment) { echo $comment->comment_content . '<br />'; } } ?>
which is basically what I need except its targeting the customer_order_notes, which are comments users add into the order when they place it. like: “my dog will pick up my package, his name is lucky”
I have also written a lugin to get the notes based off other peoples, the base is this:
::PHP::
add_action( 'woocommerce_email_order_meta', 'bl_add_order_notes_to_completed_email', 10 ); function bl_add_order_notes_to_completed_email() { global $woocommerce, $post; // If the order is not completed then don't continue. // if ( get_post_status( $post->ID ) != 'wc-completed' ){ // return false; // } $args = array( 'post_id' => $post->ID, 'status' => 'approve', 'type' => 'order_note' ); // Fetch comments $notes = get_comments( $args ); echo '<h2>' . _e( 'Order Notes', 'woocommerce' ) . '</h2>'; echo '<ul class="order_notes" style="list-style:none; padding-left:0px;">'; // Check that there are order notes if ( $notes ) { // Display each order note foreach( $notes as $note ) { ?> <li style="padding:0px -10px;"> <div class="note_content" style="background:#d7cad2; padding:10px;"> <?php echo wpautop( wptexturize( $note->comment_content ) ); ?> </div> <p class="meta"> <abbr class="exact-date" title="<?php echo $note->comment_date; ?>"><?php printf( __( 'added on %1$s at %2$s', 'woocommerce-customer-order-notes-completed-order-emails' ), date_i18n( wc_date_format(), strtotime( $note->comment_date ) ), date_i18n( wc_time_format(), strtotime( $note->comment_date ) ) ); ?></abbr> <?php if ( $note->comment_author !== __( 'WooCommerce', 'woocommerce-customer-order-notes-completed-order-emails' ) ) printf( ' ' . __( 'by %s', 'woocommerce-customer-order-notes-completed-order-emails' ), $note->comment_author ); ?> </p> </li> <?php } } echo '</ul>'; }
I don’t really know why that isnt working. It looks like it should but it does nothing.
If anyne has a solution that will print those notes into my email…like seen in this image…I will love you forever.
Advertisement
Answer
Answering this much later; but better than never. The final code I ended up using that worked and is still functioning as of today is the following:
?> <h2><?php _e( 'Tracking ID', 'woocommerce' ); ?></h2> <?php $comments = $order->get_customer_order_notes(); $customer_comments = $order->get_order_notes(); foreach( $comments as $comment ){ if ( strpos( $comment -> comment_content, "MyTracking" ) !== false ){ echo $comment -> comment_content . '<br />'; } } foreach( $customer_comments as $comment_2 ){ if ( strpos( $comment_2 -> comment_content, "filterText" ) !== false ){ echo $comment_2 -> comment_content . '<br />'; } }
This was put inside the email template customer-completed-order.php, taken from the woocommerce plugin’s templates/emails/ directory. I placed the code in at the desired location and put the customer-completed-order.php file in my child theme in it’s root directory within the following folders: woocommerce/emails/customer-completed-order.php
It then added both the customer and admin notes. I also added a filter, because my main goal was to get the tracking data posted to the admin order notes sent to the customer in their completed order email.