Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
514 views
in Technique[技术] by (71.8m points)

php - Customize order item meta only for WooCommerce admin email notifications

I need to add custom taxonomy to admin new order emails but not to customer emails. My current code displays my custom taxonomy for each item in the order but it is showing up in both admin and customer emails, which I don't want.

Looking thru email-order-items.php I don't see a way to utilize $sent_to_admin in the hook that I am using. Am I missing something?

How do I add my custom taxonomy only to admin emails using just hooks and filters?

add_action( 'woocommerce_order_item_meta_end', 'custom_woocommerce_order_item_meta_end', 10, 3 );

function custom_woocommerce_order_item_meta_end( $item_id, $item, $order ) {
     $product = $item->get_product();

     $locations = get_the_terms( $product->get_id(), 'my_custom_taxonomy' );
     echo '<br/>';
     echo '<div style="margin-top: 20px;">';
     foreach( $locations as $location ) {
          echo 'Location:  <b>' . $location->name . '</b>';
          echo '<br/>';
     }
     echo '</div>
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This can be done using $GLOBAL variable. I have revisited a bit your code too. Try this:

// 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...