我创建了一个自定义表单字段,该字段已添加到WooCommerce结帐表单中。一切都按预期工作,除了我无法弄清楚如何将自定义字段输出到新订单客户电子邮件(在这种特殊情况下,它是保留的电子邮件) - 它在发送到的电子邮件中管理员虽然。
我看到$ sent_to_admin变量,但我找不到类似" $ sent_to_customer"。
如何将该自定义字段值输出到客户确认电子邮件?
这是我的所有代码:
/* Custom Field to Checkout
*********************************************************************************************/
/* Add form field to checkout form */
function playground_surfacing( $checkout ) {
echo '<div id="playground_surfacing"><h3>'.__('Do you need Playground Surfacing?').'</h3>';
echo 'The playset price does not include surfacing, the price for which is based on the size of your playground and type of surfacing.';
woocommerce_form_field( 'pgo_surfacing', array(
'type' => 'checkbox',
'class' => array('input-checkbox'),
'label' => __('Yes, please contact me with safety surfacing information'),
), $checkout->get_value( 'pgo_surfacing' ));
echo '</div>';
}
add_action( 'woocommerce_before_order_notes', 'playground_surfacing' );
/* Save form field to order */
function playground_surfacing_field_update_order_meta( $order_id ) {
if ($_POST['pgo_surfacing']) update_post_meta( $order_id, 'pgo_surfacing', esc_attr($_POST['pgo_surfacing']));
}
add_action('woocommerce_checkout_update_order_meta', 'playground_surfacing_field_update_order_meta');
/* Output to order detail in dashboard */
function display_pgo_surfacing_admin($order){
if (get_post_meta( $order->id, 'pgo_surfacing', true )) {
echo '<p><strong>Playground Surfacing: Yes</p>';
}
}
add_action( 'woocommerce_admin_order_data_after_billing_address', 'display_pgo_surfacing_admin', 10, 1 );
/* Add to order confirmation email */
function display_pgo_surfacing_admin_email( $fields, $sent_to_admin, $order ) {
if (get_post_meta( $order->id, 'pgo_surfacing', true )) {
echo '<p><strong>Playground Surfacing: Yes</p>';
}
}
add_filter( 'woocommerce_email_order_meta_fields', 'display_pgo_surfacing_admin_email', 10, 3 );
答案 0 :(得分:0)
更新2 :
如果您想定位&#34;新订单&#34; (管理员)和&#34;客户待命&#34; 订购电子邮件通知,因此我更改了挂钩。在这个不同的钩子函数中,您将使用 AF_INET
参数:
$email
代码放在活动子主题(或活动主题)的function.php文件中。经过测试和工作。
// Add "Playground Surfacing" to specific email notifications add_filter( 'woocommerce_email_order_meta', 'display_pgo_surfacing_admin_email', 20, 4 ); function display_pgo_surfacing_admin_email( $order, $sent_to_admin, $plain_text, $email ) { // Targetting specific email notifications: "new order" (admin) and "customer on hold" $email_ids = array('new_order', 'customer_on_hold_order'); // Get your custom field $pgo_surfacing = get_post_meta( $order->get_id(), 'pgo_surfacing', true ); if ( in_array( $email->id, $email_ids ) && $pgo_surfacing ) { echo '<p><strong>Playground Surfacing: Yes</p>'; } }
的解释:
在电子邮件通知中,变量参数$ sent_to_admin将$sent_to_admin
发送给管理员的3个通知:true
,new_order
和cancelled_order
(电子邮件ID)