Php 在Woocommerce 3中的新订单电子邮件的干净表格中添加一些元数据值
在函数文件中,我创建了代码。任务:将订单数据添加到电子邮件警报。 网站上没有注册。 我想把它做为一张有标题的桌子。我发现了一个类似的例子。但其中所有的数据都来自模块的元数据。我将元数据和默认信息放在一起。 不需要编写所有的html。只要把我推向正确的方向,我的数据写得多么正确Php 在Woocommerce 3中的新订单电子邮件的干净表格中添加一些元数据值,php,wordpress,woocommerce,custom-fields,email-notifications,Php,Wordpress,Woocommerce,Custom Fields,Email Notifications,在函数文件中,我创建了代码。任务:将订单数据添加到电子邮件警报。 网站上没有注册。 我想把它做为一张有标题的桌子。我发现了一个类似的例子。但其中所有的数据都来自模块的元数据。我将元数据和默认信息放在一起。 不需要编写所有的html。只要把我推向正确的方向,我的数据写得多么正确 add_filter( 'woocommerce_email_order_meta_fields', 'custom_woocommerce_email_order_meta_fields', 10, 3 ); funct
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;
}
非常感谢!你是最棒的!
if ( $email->id !== 'new_order' ) return;