当前位置: 首页 > 知识库问答 >
问题:

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

邵毅
2023-03-14

共有1个答案

公羊英达
2023-03-14

这可以使用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 );
 类似资料:
  • 我有一个插件管理我的货件与两个自定义状态:等待-装运和装运。 我尝试添加一个电子邮件发送时,订单传递到发货。 我在Stack Overflow:Woocommerce退款电子邮件中发现了这一点,但我可以知道它是如何工作的 下面是我的插件文件代码: 我用下面的helgatheviking和Adrien Leber的建议更新了我的代码 和我的班级: 当我改变我的订单状态时,什么也没有发生!我的触发函数

  • WordPress-WooCommerce 我想做的是: null 邮件里什么也没印。 请解决。提前谢了。

  • 我希望管理电子邮件的电子邮件主题行根据产品类别进行更改。我看过所有类似的堆栈溢出问题,但没有一个适用于Woocommerce3.8.0(参见这个和这个)。 我有的是这个 我的代码只是返回新订单的默认电子邮件主题行(在woocommerce/settings/email中设置)。我想不通为什么我的函数不能识别类别名称。 谁能告诉我我的代码出了什么问题吗? 我将这段代码放在我的child-theme/

  • 在WooCommerce 3.0发布之前,我的代码已经像魔法一样工作,将自定义值从购物车保存到结账时的订单中。但从那以后,我无法为订单创建自定义元。 环境:WordPress4.9。4.

  • 有什么方法我可以添加内容从一个自定义产品字段到‘订单完成'WooCommerce电子邮件? 我已经将此代码添加到functions.php中,以便将自定义字段保存到产品元信息中。 上面的代码工作得很好,它显示了所有“订单完成”电子邮件的自定义文本。 但是,我想用自定义产品字段数据替换文本的URL部分。每个产品的数据都不同。 谢了。

  • 如果客户下了一个订单,我想添加相应的电子邮件通知每个订单项目到新的订单电子邮件通知。 如何将产品自定义域中的收件人添加到Woocommerce新订单电子邮件通知中?