{Name (First):7.3} requested the following code from my WordPress AI Code Generator.
write code to add to my woocommerce completed order email template.
The new code should check the order notes for the string "tracking number", and if this is found in the comment, add the full text of the comment to the outgoing email.
The robot generated the following code:
Here is a WordPress code snippet that you can adapt to fit your needs:
“`php
add_action( 'woocommerce_email_after_order_table', 'nm_write_add_tracking_to_email', 10, 4 );
function nm_write_add_tracking_to_email( $order, $sent_to_admin, $plain_text, $email ) {
if ( 'completed' === $order->get_status() ) {
$comments = $order->get_customer_order_notes();
if ( $comments ) {
foreach ( $comments as $comment ) {
$note = $comment->comment_content;
if ( strpos( $note, 'tracking number' ) !== false ) {
echo wpautop( wptexturize( 'Order Note: ' . $note ) );
}
}
}
}
}
“`
This script registers a function for action named 'woocommerce_email_after_order_table'. This action is executed after the order table is created in the email. The function 'nm_write_add_tracking_to_email' is hooked into this action.
Inside this function, we first check if the order status is 'completed'. If yes, we retrieve all customer notes for the order with the get_customer_order_notes() function. If there are any comments, we process each comment and check whether the string 'tracking number' is present within the comments. If yes, we add the note to the email with echo.
Please make some changes according to your need and put this code inside your child theme's functions.php file. Test it in a development environment before using it in a live website.