如何将 Woocommerce 多参与者自定义元数据包含到新订单电子邮件中 [英] How to Include Woocommerce multiple attendee custom meta data into new order email

查看:16
本文介绍了如何将 Woocommerce 多参与者自定义元数据包含到新订单电子邮件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

灵感来自将自定义元数据作为带有 Woocommerce 标题的 html 样式表添加到电子邮件中 我使用以下代码显示与会者信息:

Inspired from Add custom meta data into emails as a html styled table with a title in Woocommerce I am using the following code to display attendees info:

add_action('woocommerce_email_order_details', 'action_after_email_order_details', 25, 4 );
function action_after_email_order_details( $order, $sent_to_admin, $plain_text, $email ){
   $event = get_post_meta( $order->get_id(), 'WooCommerceEventsOrderTickets', true );

   if( ! is_array($event) ) return;

   $event = isset($event[1][1]) ? $event[1][1] : '';

   if( sizeof($event) == 0 ) return;

   $custom = isset($event['WooCommerceEventsCustomAttendeeFields']) ? $event['WooCommerceEventsCustomAttendeeFields'] : '';

   // Set our array of needed data
   $fields_array = [
    __('First name')    => isset($event['WooCommerceEventsAttendeeName']) ? $event['WooCommerceEventsAttendeeName'] : '',
    __('Last name')     => isset($event['WooCommerceEventsAttendeeLastName']) ? $event['WooCommerceEventsAttendeeLastName'] : '',
   ];

   if( ! $event ) return;

   // The HTML Structure
   $html_output = '<h2>' . __('Attendee Info') . '</h2>
   <div class="discount-info">
      <table cellspacing="0" cellpadding="6"><tbody>';

   // Loop though the data array to set the fields
   foreach( $fields_array as $label => $value ):
   if( ! empty($value) ):

     $html_output .= '<tr>
        <th>' . $label . '</th>
        <td>' . $value . '</td>
    </tr>';

   endif;
   endforeach;

   $html_output .= '</tbody></table>
   </div><br>'; // HTML (end)

   // The CSS styling
   $styles = '<style>
    .discount-info table{width: 100%; font-family: \'Helvetica Neue\', Helvetica, Roboto, Arial, sans-serif;
        color: #737373; border: 1px solid #e4e4e4; margin-bottom:8px;}
    .discount-info table th, table.tracking-info td{text-align: left; border-top-width: 4px;
        color: #737373; border: 1px solid #e4e4e4; padding: 12px; width:58%;}
    .discount-info table td{text-align: left; border-top-width: 4px; color: #737373; border: 1px solid #e4e4e4; padding: 12px;}
   </style>';

// The Output CSS + HTML
echo $styles . $html_output;
}

但它只显示来自第一位与会者的信息.

是否可以选择在 woocommerce_email_order_details 钩子中包含元数据(只有名字和姓氏)但对于所有(多个)参与者?

Is there an option to include meta data ( only the first and last name ) but for all ( multiple ) attendees inside woocommerce_email_order_details hook?

推荐答案

好的,我想通了,如果有人需要,这里有这个功能:

OK I figured it out, here's the function if someone needs it:

add_action('woocommerce_email_order_details', 'action_after_email_order_details', 25, 4 );
function action_after_email_order_details( $order, $sent_to_admin, $plain_text, $email ) {
$events = get_post_meta( $order->get_id(), 'WooCommerceEventsOrderTickets', true );

if( ! is_array($events) ) return;

$events = isset($events[1]) ? $events[1] : '';

if( ! $events ) return;

// The HTML Structure
$html_output = '<h2>' . __('Attendee Info') . '</h2>
<div class="attendees-info">
    <table cellspacing="0" cellpadding="6"><tbody>';

$i = 1;
foreach( $events as $event ) :

    if( sizeof($event) == 0 ) return;
    // get only first and last name for each attendee
    $attendeeFirstName = isset($event['WooCommerceEventsAttendeeName']) ? $event['WooCommerceEventsAttendeeName'] : '';
    $attendeeLastName = isset($event['WooCommerceEventsAttendeeLastName']) ? $event['WooCommerceEventsAttendeeLastName'] : '';

    $html_output .= '<tr>
    <th>Attendee '. $i . '</th>
    <td>' . $attendeeFirstName .' '.$attendeeLastName. '</td>
    </tr>';

    $i++;

endforeach; 

$html_output .= '</tbody></table>
</div><br>'; // HTML (end)

// The CSS styling
$styles = '<style>
    .attendees-info table{width: 100%; font-family: \'Helvetica Neue\', Helvetica, Roboto, Arial, sans-serif;
        color: #737373; border: 1px solid #e4e4e4; margin-bottom:8px;}
    .attendees-info table th, table.tracking-info td{text-align: left; border-top-width: 4px;
        color: #737373; border: 1px solid #e4e4e4; padding: 12px; width:58%;}
    .attendees-info table td{text-align: left; border-top-width: 4px; color: #737373; border: 1px solid #e4e4e4; padding: 12px;}
</style>';

// The Output CSS + HTML
echo $styles . $html_output;
}

这篇关于如何将 Woocommerce 多参与者自定义元数据包含到新订单电子邮件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆