gpt4 book ai didi

php - 在 Woocommerce 中将自定义元数据作为带有标题的 html 样式表添加到电子邮件中

转载 作者:搜寻专家 更新时间:2023-10-31 20:58:23 25 4
gpt4 key购买 nike

在 WooCommerce 中,"Get a custom field array values within WooCommerce email order meta " 对我之前的一个问题的回答代码,为我提供了从订单发布元数据的数组中提取字段的方法。

但是我该如何使用这段代码并对其进行调整,以便在电子邮件中在新数据上方添加一个标题并在数据周围添加一个表格?

  1. 我想添加一个 <h2>Attendee Info</h2>在字段的输出之上。此外,我想将字段输出包装在 <table> 中。 .

  2. 我想通过某种功能在管理订单表屏幕中添加一些字段作为新列。

此外,我还希望能够将 Admin Order Table 中的某些字段显示为新列。

最佳答案

这可以通过使用 woocommerce_email_order_details 操作 Hook 来完成:

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 操作 Hook ,替换第一行:

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

你会得到更干净和格式化的标题,如:

enter image description here

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 stackOverFlow.

You should better display that data in a custom meta box in order edit pages, which is much more easier and practical.

关于php - 在 Woocommerce 中将自定义元数据作为带有标题的 html 样式表添加到电子邮件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50280473/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com