Customize order item meta only for WooCommerce admin email notifications

Loading

This is the perfect answer for your question:

// Setting the "sent_to_admin" as a global variable
add_action('woocommerce_email_before_order_table', 'email_order_id_as_a_global', 1, 4);
function email_order_id_as_a_global($order, $sent_to_admin, $plain_text, $email){
    $GLOBALS['email_data'] = array(
        'sent_to_admin' => $sent_to_admin, // <== HERE we set "$sent_to_admin" value
        'email_id' => $email->id, // The email ID (to target specific email notification)
    );
}

// Conditionally customizing footer email text
add_action( 'woocommerce_order_item_meta_end', 'custom_email_order_item_meta_end', 10, 3 );
function custom_email_order_item_meta_end( $item_id, $item, $order ){

    // Getting the custom 'email_data' global variable
    $refNameGlobalsVar = $GLOBALS;
    $email_data = $refNameGlobalsVar['email_data'];

    // Only for admin email notifications
    if( ! ( is_array( $email_data ) && $email_data['sent_to_admin'] ) ) return;

    ## -------------------------- Your Code below -------------------------- ##

    $taxonomy = 'my_custom_taxonomy'; // <= Your custom taxonomy

    echo '<br/><div style="margin-top: 20px;">';
    foreach( get_the_terms( $item->get_product_id(), $taxonomy ) as $term )
        echo 'Location:  <b>' . $term->name . '</b><br/>';
    echo '</div>';
}

Code goes in function.php file of the active child theme (or active theme).

Tested and works.

About Post Author