WooCommerce - 在结帐时添加新字段并订购电子邮件

时间:2015-12-14 08:15:32

标签: email woocommerce checkout

您好我正在尝试在我的结帐页面添加一个石平电子邮件字段,我也想在订单电子邮件中显示它。

在环顾四周后,我终于想出了我放在functions.php中的代码:一切正常(我在结帐页面中有新字段,我在订单的管理面板中有它)。它仍然没有出现在通知电子邮件中。我做错了什么?

以下是我的代码

// Hook in the checkout page
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );

// Our hooked in function - $fields is passed via the filter!

function custom_override_checkout_fields( $fields ) {
$fields['shipping']['shipping_email'] = array(
'label' => __('Email', 'woocommerce'),
'placeholder' => _x('Email', 'placeholder', 'woocommerce'),
'required' => false,
'class' => array('form-row-wide'),
'clear' => true
);
return $fields;
}

/* Save Field in the DB as Order Meta Data*/

  add_action('woocommerce_checkout_update_order_meta','my_custom_checkout_field_update_order_meta');

function my_custom_checkout_field_update_order_meta($order_id) {

if (!empty($_POST['shipping']['shipping_email'])) {
update_post_meta($order_id, 'Shipping email', esc_attr($_POST['shipping']    ['shipping_email']));
}
}

/* display it in the Order details screen*/

add_action('woocommerce_admin_order_data_after_billing_address', 'my_custom_billing_fields_display_admin_order_meta', 10, 1);

function my_custom_billing_fields_display_admin_order_meta($order) {
echo '

' . __('Shipping email') . ':
' . get_post_meta($order->id, '_shipping_email', true) . '
';
}

/**Add the field to order emails **/ 
add_filter('woocommerce_email_order_meta_keys', 'my_woocommerce_email_order_meta_keys');

function my_woocommerce_email_order_meta_keys( $keys ) {
$keys[] = 'Shipping email';
return $keys;
}

1 个答案:

答案 0 :(得分:0)

my_woocommerce_email_order_meta_keys功能需要进行小的更改,以下代码可以正常工作

function my_woocommerce_email_order_meta_keys( $keys ) {
    $keys['Shipping email'] = '_shipping_email';
    return $keys;
}