在Woocommerce中,将自定义元数据添加到电子邮件中,作为一个带有标题的HTML样式表。

3
在WooCommerce中,之前我提出的一个问题中的答案代码"Get a custom field array values within WooCommerce email order meta"已经为我提供了从订单邮件元数据中提取字段数组的方法。
但是,我该如何调整这个代码,在邮件中添加新数据的标题和表格?
  1. 我想在字段输出上方添加一个<h2>Attendee Info</h2>。此外,我想用<table>包装字段输出。

  2. 我想通过某种函数将一些字段作为新列添加到管理员订单表屏幕中。

此外,我还希望能够在管理员订单表中显示其中一些字段作为新列。
1个回答

3

可以使用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;
}

代码放在您当前使用的子主题(或活动主题)的functions.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 );

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

enter image description here

要将这些数据显示在管理订单表中是一件非常复杂且范围太广的事情,无法在这个答案或Stack Overflow上进行回答。
你最好将这些数据显示在自定义元框中,以便在订单编辑页面上编辑,这样更容易且实用。

看起来运行良好。我唯一看到的问题是,“参与者信息”标题和表格包装器仍然会显示,即使产品没有这些字段。 - Garconis
1
有什么想法可以更新这个,如果数组中有 numerous 个数组?理想情况下,它会为每个数组生成一个表格:https://d.pr/free/n/nKHS0H - Garconis
有没有想法如何格式化以显示: 参与者1 - 名字(值) 姓氏(值) 每个都在单独的行上? - Darko
@darko 这个答案只处理一个参与者,而不是多个。 - LoicTheAztec
@LoicTheAztec 好的,我会重新发布问题,包括多个参与者的信息。 - Darko
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接