Display custom order meta data value in email notifications WooCommerce

柔情痞子 提交于 2021-02-11 14:02:29

问题


Based on the following code

Add a custom checkbox in WooCommerce checkout which value shows in admin edit order

I tried to add my_field_name to order confirmation email. As I understand I have to use woocommerce_email_customer_details.

So I came to this solution, unfortunately without the desired result.

add_action('woocommerce_email_customer_details','woocommerce_email_order_invoice_number', 28, 4 );
function woocommerce_email_order_invoice_number( $order, $sent_to_admin, $plain_text, $email ) {
  if( $my_field_name = get_post_meta( $order->get_id(), 'my_field_name', true ) ) 
      echo '<p><strong>My custom field: </strong> <span style="color:red;">Is enabled</span></p>';
}

回答1:


You have some minor mistakes, via the if condition "$email->id == ..." you can target the mails

How to target other WooCommerce order emails

  • 'customer_completed_order'
  • 'customer_processing_order'
  • 'customer_on_hold_order'
  • 'customer_refunded_order'
  • 'customer_reset_password'
  • 'customer_invoice'
  • 'customer_new_account'
  • 'customer_note'
  • 'cancelled_order'
  • 'failed_order'
  • 'new_order'

function woocommerce_email_order_invoice_number( $order, $sent_to_admin, $plain_text, $email ) {
    // For 'new order'
    if ( $email->id == 'new_order' ) {
    
        // Get post meta
        $my_field_name = get_post_meta( $order->get_id(), 'my_field_name', true );
   
        // True and equal to     
        if ( $my_field_name && $my_field_name == 1 ) {
            echo '<p><strong>My custom field: </strong> <span style="color:red;">Is enabled</span></p>';
        }
    }
}
add_action( 'woocommerce_email_customer_details', 'woocommerce_email_order_invoice_number', 20, 4 );


来源:https://stackoverflow.com/questions/61406669/display-custom-order-meta-data-value-in-email-notifications-woocommerce

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