Display order customer note in Woocommerce email notifications

橙三吉。 提交于 2019-12-07 20:48:43

问题


In Woocommerce, I am trying to get the customers additional order message:

I am trying to display that customer message on the email notifications. But i don't know how to get this information in the php code.

Here is my code in the functions.php file:

add_action( 'woocommerce_email_after_order_table', 'ts_email_after_order_table', 10, 4 );
function ts_email_after_order_table( $item_id, $item, $order, $plain_text){
    $notes=$order->customer_message; //did not work
    echo '<table cellspacing="0" cellpadding="0" style="width: 100%; color: #636363; border: 1px solid #e5e5e5;" border="0"><tbody><tr><td><p>Test: ' . $notes . '</p></td></tr></tbody></table>';
}

I really don't know how to access that information.

Any help is appreciated.


回答1:


There are some errors in your code. Using the WC_Order method get_customer_note(), try the following instead, that will display for some successful email notifications, the customer order note:

add_action( 'woocommerce_email_after_order_table', 'customer_note_email_after_order_table', 10, 4 );
function customer_note_email_after_order_table( $order, $sent_to_admin, $plain_text, $email ){
    // Only on some email notifications
    if ( in_array( $email->id, array('new_order', 'customer_on_hold_order', 'customer_processing_order', 'customer_completed_order') ) ) :

    // Get customer Order note
    $customer_note = $order->get_customer_note();

    // Display the Customer order notes section
    echo '<h2>' . __("Order notes", "woocommerce") . '</h2>
    <div style="margin-bottom: 40px;">
    <table cellspacing="0" cellpadding="0" style="width: 100%; color: #636363; border: 2px solid #e5e5e5;" border="0">
        <tr><td><p>' . $customer_note . '</p></td></tr>
    </table></div>';

    endif;
}

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



来源:https://stackoverflow.com/questions/52635919/display-order-customer-note-in-woocommerce-email-notifications

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!