0

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

But it displays only the information from the first attendee.

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?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Darko
  • 920
  • 10
  • 22

1 Answers1

0

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;
}
Darko
  • 920
  • 10
  • 22