在WooCommerce中,“Get a custom field array values within WooCommerce email order meta”回答了我以前的一个问题的代码,它给了我从元数据后的数据中提取字段的方法。
但是,如何在电子邮件中使用此代码并对其进行调整,在新数据上方添加标题以及在数据周围添加表格?
<h2>Attendee Info</h2>
。另外,我想将字段输出包装在<table>
中。此外,我还希望能够在管理订单表中显示其中的一些字段作为新列。
这可以通过这种方式使用woocommerce_email_order_details
动作钩子来完成:
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'] : '',
__('Title') => isset($custom['fooevents_custom_title']) ? $custom['fooevents_custom_title'] : '',
__('Organization') => isset($custom['fooevents_custom_organization']) ? $custom['fooevents_custom_organization'] : '',
__('Address') => isset($custom['fooevents_custom_address']) ? $custom['fooevents_custom_address'] : '',
__('City') => isset($custom['fooevents_custom_city']) ? $custom['fooevents_custom_city'] : '',
__('Postcode') => isset($custom['fooevents_custom_postal_code']) ? $custom['fooevents_custom_postal_code'] : '',
__('State') => isset($custom['fooevents_custom_state/province']) ? $custom['fooevents_custom_state/province'] : '',
];
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;
}
代码位于活动子主题(或活动主题)的function.php文件中。经过测试和工作。
或者还使用woocommerce_email_order_meta
动作钩子,替换第一行:
add_action('woocommerce_email_order_details', 'action_after_email_order_details', 25, 4 );
这样:
add_action('woocommerce_email_order_meta', 'woocommerce_email_order_meta', 25, 4 );
你会得到一些更干净,格式化的标题,如:
要在管理员订单表中显示此数据,这个问题要复杂得多,而且过于宽泛,无法在此答案或stackOverFlow中回答。
您应该更好地在自定义元框中显示该数据,以便编辑页面,这样更容易和实用。