I am trying to, based on shipping and if / if not the order note is empty, to add information to the customer complete order email.
Two different messages based on if the order notes field is filled in or not. I've placed test orders but nothing shows up.
This is the code I am trying to get to work:
add_action( 'woocommerce_email_order_details', 'local_pickup_order_instructions', 10, 4 );
function local_pickup_order_instructions( $order, $sent_to_admin, $plain_text, $email ) {
if ( 'customer_completed_order' != $email->id ) return;
foreach( $order->get_items('shipping') as $shipping_item ) {
$shipping_rate_id = $shipping_item->get_method_id();
$method_array = explode(':', $shipping_rate_id );
$shipping_method_id = reset($method_array);
if ('local_pickup' == $shipping_method_id && empty($_POST['order_comments'])){ ?>
<div style="">Your instructions text here</div>
<?php
break;
}
else {
if ('local_pickup' == $shipping_method_id && !empty($_POST['order_comments'] ) ) { ?>
<div style="">Your instructions text here</div>
<?php
}
}
}
}
There are some mistakes in your code… To display a different custom text when shipping method is "Local Pickup" if there is (or not) a customer note, use the following simplified and revisited code:
add_action( 'woocommerce_email_order_details', 'local_pickup_order_instructions', 10, 4 );
function local_pickup_order_instructions( $order, $sent_to_admin, $plain_text, $email ) {
if ( $email->id === 'customer_completed_order' ) {
$shipping_items = $order->get_items('shipping');
$shipping_item = reset($shipping_items); // Get first shipping item
$customer_note = $order->get_customer_note(); // Get customer note
// Targeting Local pickup shipping methods
if ( strpos( $shipping_item->get_method_id(), 'local_pickup' ) !== false ) {
if ( empty($customer_note) ) {
echo '<div style="color:red;">'.__("Instructions text here… (No customer note)").'</div>'; // Empty order note
} else {
echo '<div style="color:green;">'.__("Instructions text here… (has a customer note)").'</div>'; // Filled order note
}
}
}
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.