3
votes

I want to add a notice section to order emails sent to admin if specific meta has a specific value (Umbrella Hole is "YES").

Code up till now:

function add_order_instruction_email( $order, $sent_to_admin, $plain_text, $email ) {
    foreach( $order->get_items() as $item ){
        $target_value = $item->get_meta('Umbrella Hole');
        if ($target_value == "Yes") {
            echo '<div style="background-color:antiquewhite;padding:5px;margin-bottom:10px;"><strong><span style="color:red;">Note:</span></strong> Umbrella Hole is present in the order. Please make sure velcro zipper split is requested from supplier too.</div>';
        }
    }
}
add_action( 'woocommerce_email_order_details', 'add_order_instruction_email', 10, 4 );

But it's not working. Anything I am doing wrong? Using latest versions of WordPress and WooCommerce.

References:
Get custom order item metadata in Woocommerce 3
How to get WooCommerce order details
WooCommerce: Show notice on new order email if specific payment method is used

1

1 Answers

2
votes

I have tested your code and it works for a registered order item meta data which key is Umbrella Hole, see it below on wp_woocommerce_order_itemmeta table for a line_item:

enter image description here

So the problem can only comes from the order item custom meta data that is not registered

I have revisited lightly your code:

add_action( 'woocommerce_email_order_details', 'add_order_instruction_email', 10, 4 );
function add_order_instruction_email( $order, $sent_to_admin, $plain_text, $email ) {
    // Loop through order items
    foreach ( $order->get_items() as $item ) {
        if ( "Yes" == $item->get_meta('Umbrella Hole') ) {
            echo '<div style="background-color:antiquewhite;padding:5px;margin-bottom:10px;"><strong><span style="color:red;">Note:</span></strong> Umbrella Hole is present in the order. Please make sure velcro zipper split is requested from supplier too.</div>';
            $break; // Stop the loop to avoid repetitions
        }
    }
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

Here below the email notification when any order line item has a registered meta data Umbrella Hole with "yes" as value:

enter image description here