将自定义元数据作为 html 样式添加到电子邮件中,在 Woocommerce 中带有标题
Add custom meta data into emails as a html styled table with a title in Woocommerce
在 WooCommerce 中,"" 回答我之前的一个问题的代码,为我提供了从顺序 post元数据。
但是我如何使用此代码并对其进行调整,以便在电子邮件中的新数据上方添加标题并在数据周围添加 table?
我想在字段的输出上方添加一个 <h2>Attendee Info</h2>
。此外,我想将字段输出包装在 <table>
.
中
我想通过某种功能在管理订单 Table 屏幕中添加一些字段作为新列。
此外,我还希望能够将 Admin Order 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;
}
代码进入活动 child 主题(或活动主题)的 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 );
您将获得更简洁且格式与您的标题类似的内容:
To get this data displayed in admin order table is something much more complicate and too broad to be answer in this answer or on Whosebug.
You should better display that data in a custom meta box in order edit pages, which is much more easier and practical.
在 WooCommerce 中,"
但是我如何使用此代码并对其进行调整,以便在电子邮件中的新数据上方添加标题并在数据周围添加 table?
我想在字段的输出上方添加一个
<h2>Attendee Info</h2>
。此外,我想将字段输出包装在<table>
. 中
我想通过某种功能在管理订单 Table 屏幕中添加一些字段作为新列。
此外,我还希望能够将 Admin Order 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;
}
代码进入活动 child 主题(或活动主题)的 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 );
您将获得更简洁且格式与您的标题类似的内容:
To get this data displayed in admin order table is something much more complicate and too broad to be answer in this answer or on Whosebug.
You should better display that data in a custom meta box in order edit pages, which is much more easier and practical.