在 Woocommerce 3 中为新订单电子邮件在干净的 table 中添加一些元数据值
Add some meta data values in a clean table for new order emails in Woocommerce 3
在函数文件中,我创建了代码。任务:将订单数据添加到电子邮件警报中。
网站上没有注册。
我想以 table 的形式来做这件事。我找到了一个类似的。但是其中的所有数据都取自模块的元数据。我同时拥有元数据和默认信息。
不需要全部写html。把我推向正确的方向如何正确写入我的数据。
add_filter( 'woocommerce_email_order_meta_fields', 'custom_woocommerce_email_order_meta_fields', 10, 3 );
function custom_woocommerce_email_order_meta_fields( $fields, $sent_to_admin, $order ) {
$fields['billing_first_name'] = array(
'label' => __( 'Name' ),
'value' => $order->get_billing_first_name(),
);
$fields['billing_phone'] = array(
'label' => __( 'Phone' ),
'value' => $order->get_billing_phone(),
);
$fields['billing_address_1'] = array(
'label' => __( 'Street' ),
'value' => $order->get_billing_address_1(),
);
$fields['billing_address_2'] = array(
'label' => __( 'Build' ),
'value' => $order->get_billing_address_2(),
);
$fields['billing_address_3'] = array(
'label' => __( 'Apartment' ),
'value' => get_post_meta( $order->id, 'Apartment', true ),
);
$fields['billing_address_4'] = array(
'label' => __( 'Floor' ),
'value' => get_post_meta( $order->id, 'Floor', true ),
);
$fields['billing_address_5'] = array(
'label' => __( 'Entrance' ),
'value' => get_post_meta( $order->id, 'Entrance', true ),
);
$fields['customer_message'] = array(
'label' => __( 'Note' ),
'value' => $order->customer_message,
);
return $fields;
}
注意: 在您的代码中,将过时的 $order->id
替换为 $order->get_id()
。
要在干净的 html 中添加带有默认顺序元数据的自定义字段元数据,试试这个:
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 )
{
$domain = 'woocommerce';
// Define all translatable custom fields labels and data
$fields_array = [
[ 'label' => __( 'Name', $domain ), 'value' => $order->get_billing_first_name() ],
[ 'label' => __( 'Phone', $domain ), 'value' => $order->get_billing_phone() ],
[ 'label' => __( 'Street', $domain ), 'value' => $order->get_billing_address_1() ],
[ 'label' => __( 'Build', $domain ), 'value' => $order->get_billing_address_2() ],
[ 'label' => __( 'Apartment', $domain ), 'value' => $order->get_meta( 'Apartment', true ) ],
[ 'label' => __( 'Floor', $domain ), 'value' => $order->get_meta( 'Floor', true ) ],
[ 'label' => __( 'Entrance', $domain ), 'value' => $order->get_meta( 'Entrance', true ) ],
[ 'label' => __( 'Note', $domain ), 'value' => $order->get_customer_note() ],
];
// The HTML Structure
$html_output = '<h2>' . __( 'Extra data (title)', $domain ) . '</h2>
<div class="discount-info">
<table cellspacing="0" cellpadding="6"><tbody>';
// Loop though the data array to set the fields
foreach( $fields_array as $field ):
// Only display non empty fields values
if( ! empty($field['value']) ):
$html_output .= '<tr>
<th>' . $field['label'] . ':</th>
<td>' . $field['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;}
.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 文件。已测试并有效。
To target the "New Order" email notification only, you can add the following line inside the function code at the beginning:
if ( $email->id !== 'new_order' ) return;
在函数文件中,我创建了代码。任务:将订单数据添加到电子邮件警报中。
网站上没有注册。
我想以 table 的形式来做这件事。我找到了一个类似的
add_filter( 'woocommerce_email_order_meta_fields', 'custom_woocommerce_email_order_meta_fields', 10, 3 );
function custom_woocommerce_email_order_meta_fields( $fields, $sent_to_admin, $order ) {
$fields['billing_first_name'] = array(
'label' => __( 'Name' ),
'value' => $order->get_billing_first_name(),
);
$fields['billing_phone'] = array(
'label' => __( 'Phone' ),
'value' => $order->get_billing_phone(),
);
$fields['billing_address_1'] = array(
'label' => __( 'Street' ),
'value' => $order->get_billing_address_1(),
);
$fields['billing_address_2'] = array(
'label' => __( 'Build' ),
'value' => $order->get_billing_address_2(),
);
$fields['billing_address_3'] = array(
'label' => __( 'Apartment' ),
'value' => get_post_meta( $order->id, 'Apartment', true ),
);
$fields['billing_address_4'] = array(
'label' => __( 'Floor' ),
'value' => get_post_meta( $order->id, 'Floor', true ),
);
$fields['billing_address_5'] = array(
'label' => __( 'Entrance' ),
'value' => get_post_meta( $order->id, 'Entrance', true ),
);
$fields['customer_message'] = array(
'label' => __( 'Note' ),
'value' => $order->customer_message,
);
return $fields;
}
注意: 在您的代码中,将过时的 $order->id
替换为 $order->get_id()
。
要在干净的 html 中添加带有默认顺序元数据的自定义字段元数据,试试这个:
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 )
{
$domain = 'woocommerce';
// Define all translatable custom fields labels and data
$fields_array = [
[ 'label' => __( 'Name', $domain ), 'value' => $order->get_billing_first_name() ],
[ 'label' => __( 'Phone', $domain ), 'value' => $order->get_billing_phone() ],
[ 'label' => __( 'Street', $domain ), 'value' => $order->get_billing_address_1() ],
[ 'label' => __( 'Build', $domain ), 'value' => $order->get_billing_address_2() ],
[ 'label' => __( 'Apartment', $domain ), 'value' => $order->get_meta( 'Apartment', true ) ],
[ 'label' => __( 'Floor', $domain ), 'value' => $order->get_meta( 'Floor', true ) ],
[ 'label' => __( 'Entrance', $domain ), 'value' => $order->get_meta( 'Entrance', true ) ],
[ 'label' => __( 'Note', $domain ), 'value' => $order->get_customer_note() ],
];
// The HTML Structure
$html_output = '<h2>' . __( 'Extra data (title)', $domain ) . '</h2>
<div class="discount-info">
<table cellspacing="0" cellpadding="6"><tbody>';
// Loop though the data array to set the fields
foreach( $fields_array as $field ):
// Only display non empty fields values
if( ! empty($field['value']) ):
$html_output .= '<tr>
<th>' . $field['label'] . ':</th>
<td>' . $field['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;}
.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 文件。已测试并有效。
To target the "New Order" email notification only, you can add the following line inside the function code at the beginning:
if ( $email->id !== 'new_order' ) return;