如何将 Woocommerce 多个与会者自定义元数据包含到新订单电子邮件中

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

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)


}

但它仅显示第一个与会者的信息。


是否有一个选项可以包含元数据(仅名字和姓氏)但对于woocommerce_email_order_details挂钩内的所有(多个)与会者?


精慕HU
浏览 124回答 1
1回答

慕虎7371278

好吧,我明白了,如果有人需要的话,这是这个函数: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">&nbsp; &nbsp; <table cellspacing="0" cellpadding="6"><tbody>';$i = 1;foreach( $events as $event ) :&nbsp; &nbsp; if( sizeof($event) == 0 ) return;&nbsp; &nbsp; // get only first and last name for each attendee&nbsp; &nbsp; $attendeeFirstName = isset($event['WooCommerceEventsAttendeeName']) ? $event['WooCommerceEventsAttendeeName'] : '';&nbsp; &nbsp; $attendeeLastName = isset($event['WooCommerceEventsAttendeeLastName']) ? $event['WooCommerceEventsAttendeeLastName'] : '';&nbsp; &nbsp; $html_output .= '<tr>&nbsp; &nbsp; <th>Attendee '. $i . '</th>&nbsp; &nbsp; <td>' . $attendeeFirstName .' '.$attendeeLastName. '</td>&nbsp; &nbsp; </tr>';&nbsp; &nbsp; $i++;endforeach;&nbsp;$html_output .= '</tbody></table></div><br>'; // HTML (end)// The CSS styling$styles = '<style>&nbsp; &nbsp; .attendees-info table{width: 100%; font-family: \'Helvetica Neue\', Helvetica, Roboto, Arial, sans-serif;&nbsp; &nbsp; &nbsp; &nbsp; color: #737373; border: 1px solid #e4e4e4; margin-bottom:8px;}&nbsp; &nbsp; .attendees-info table th, table.tracking-info td{text-align: left; border-top-width: 4px;&nbsp; &nbsp; &nbsp; &nbsp; color: #737373; border: 1px solid #e4e4e4; padding: 12px; width:58%;}&nbsp; &nbsp; .attendees-info table td{text-align: left; border-top-width: 4px; color: #737373; border: 1px solid #e4e4e4; padding: 12px;}</style>';// The Output CSS + HTMLecho $styles . $html_output;}
打开App,查看更多内容
随时随地看视频慕课网APP