在电子邮件通知中显示自定义订单元数据值WooCommerce

时间:2020-04-24 10:52:06

标签: php wordpress woocommerce hook-woocommerce email-notifications

基于以下代码

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

我尝试将my_field_name添加到订购确认电子邮件中。据我了解,我必须使用woocommerce_email_customer_details

不幸的是,我来到此解决方案时并没有得到期望的结果。

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 个答案:

答案 0 :(得分:1)

通过if条件"$email->id == ...",您有一些小错误 您可以定位邮件

如何定位其他WooCommerce订单电子邮件

  • 'customer_completed_order'
  • 'customer_processing_order'
  • 'customer_on_hold_order'
  • 'customer_refunded_order'
  • 'customer_reset_password'
  • 'customer_invoice'
  • 'customer_new_account'
  • 'customer_note'
  • “取消订单”
  • “失败订单”
  • “新订单”

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 );