Skip to content

Instantly share code, notes, and snippets.

@Acephalia
Last active April 17, 2023 04:42
Show Gist options
  • Select an option

  • Save Acephalia/4ff9fcd1e732929aeab1e5170d620fcf to your computer and use it in GitHub Desktop.

Select an option

Save Acephalia/4ff9fcd1e732929aeab1e5170d620fcf to your computer and use it in GitHub Desktop.
Woocomerce Add Order Note With Specific Words To Order Completed Notification Email
//This code snippet will search through the order notes for the words 'Loren ipsum' and then display that note in the Order Completed Email sent to a customer.
function action_woocommerce_email_order_details( $order, $sent_to_admin, $plain_text, $email ) {
// Target specific email notification
if ( $email->id == 'customer_completed_order' ) {
// Get order id
$order_id = $order->get_id();
// Get order notes
$order_notes = wc_get_order_notes( array(
'order_id' => $order_id,
'order_by' => 'date_created',
'order' => 'ASC',
));
// Notes is NOT empty
if ( ! empty( $order_notes ) ) {
foreach ( $order_notes as $order_note ) {
// Check if note contains test 'Loren ipsum'
// Modify text Loren ipsum and style as required
if ( strpos( $order_note->content, 'Loren ipsum' ) !== false ) {
echo '<p style="color:blue;font-size:16px;">' . $order_note->content . '</p>';
}
}
}
}
}
add_action( 'woocommerce_email_order_details', 'action_woocommerce_email_order_details', 1, 4 );
//Modified code for u/NathansUsername to extract Tracking number located in order note between square brackets.
function action_woocommerce_email_order_details( $order, $sent_to_admin, $plain_text, $email ) {
// Target specific email notification
if ( $email->id == 'customer_completed_order' ) {
// Get order id
$order_id = $order->get_id();
// Get order notes
$order_notes = wc_get_order_notes( array(
'order_id' => $order_id,
'order_by' => 'date_created',
'order' => 'ASC',
));
// Notes is NOT empty
if ( ! empty( $order_notes ) ) {
foreach ( $order_notes as $order_note ) {
// Search for tracking number in note content and extract tracking number between square brackets.
if ( preg_match( '/\[(.*?)\]/', $order_note->content, $matches ) ) {
$tracking_number = $matches[1];
//Print output in email. Modify style and prefix text here.
echo '<p style="color:blue;font-size:20px;">Your tracking number is ' . $tracking_number . '</p>';
}
}
}
}
}
add_action( 'woocommerce_email_order_details', 'action_woocommerce_email_order_details', 1, 4 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment