我很难弄清楚如何将新的自定义计费字段添加到我的管理员通知电子邮件中。我不是 php 专家(无论如何),所以我不确定我搞砸了什么,还是因为它试图将该字段附加到一般的 woo commerce管理员通知电子邮件或来自 Bookings 插件的电子邮件中。我的自定义字段在计费页面上工作正常,它只是将其放入管理员电子邮件中,最好也进入后端管理员订单管理区域?
老实说,我在圣诞节前就一直在做这件事,完全忘记了我在做什么和在哪里。
提前谢谢你!
这是我对代码的看法。
/**
* Add the field to the checkout
**/
add_action('woocommerce_after_order_notes', 'my_custom_checkout_field');
function my_custom_checkout_field( $checkout ) {
echo '<div id="my_custom_checkout_field"><h3>'.__('Referral Source').'</h3>';
/**
* Output the field. This is for 1.4.
*
* To make it compatible with 1.3 use $checkout->checkout_form_field instead:
$checkout->checkout_form_field( 'my_field_name', array(
'type' => 'text',
'class' => array('my-field-class orm-row-wide'),
'label' => __('Fill in this field'),
'placeholder' => __('Enter a number'),
));
**/
woocommerce_form_field( 'my_field_name', array(
'type' => 'text',
'class' => array('my-field-class form-row-wide'),
'label' => __('How did you hear about us?'),
'placeholder' => __('Please enter how you were referred to our clinic.'),
'required' => true,
), $checkout->get_value( 'my_field_name' ));
echo '</div>';
}
/**
* Update the user meta with field value
**/
add_action('woocommerce_checkout_update_user_meta', 'my_custom_checkout_field_update_user_meta');
function my_custom_checkout_field_update_user_meta( $user_id ) {
if ($user_id && $_POST['my_field_name']) update_user_meta( $user_id, 'my_field_name', esc_attr($_POST['my_field_name']) );
}
/**
* Update the order meta with field value
**/
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 ($_POST['my_field_name']) update_post_meta( $order_id, 'My Field', esc_attr($_POST['my_field_name']));
}
/**
* Add the field to order emails
**/
add_filter('woocommerce_email_order_meta_keys', 'my_custom_checkout_field_order_meta_keys');
function my_custom_checkout_field_order_meta_keys( $keys ) {
$keys[] = 'Referral Source';
return $keys;
}
更新:
这就是我现在所处的位置。它接缝工作,但我认为我在那里有一个双重功能。我相信最后一个功能是用于将自定义字段添加到我的电子邮件中的函数。
/**
* Update the user meta with field value
**/
add_action('woocommerce_checkout_update_user_meta', 'my_custom_checkout_field_update_user_meta');
function my_custom_checkout_field_update_user_meta( $user_id ) {
if ($user_id && $_POST['my_field_name']) update_user_meta( $user_id, 'my_field_name', esc_attr($_POST['my_field_name']) );
}
/**
* Update the order meta with field value
**/
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 ($_POST['my_field_name']) update_post_meta( $order_id, 'Referral Source', esc_attr($_POST['my_field_name']));
}
/**
* Add the field to order emails
**/
add_filter('woocommerce_email_order_meta_keys', 'my_custom_checkout_field_order_meta_keys');
function my_custom_checkout_field_order_meta_keys( $keys ) {
$keys[] = 'Referral Source';
return $keys;
}
// WooCommerce 2.3+
function my_custom_email_order_meta_fields( $fields, $sent_to_admin, $order ) {
$fields['_some_field'] = array(
'label' => __( 'Referral Source' ),
'value' => get_post_meta( $order->id, 'my_field_name', true ),
);
return $fields;
}
add_filter('woocommerce_email_order_meta_fields', 'my_custom_email_order_meta_fields', 10, 3 );
add_filter('woocommerce_email_order_meta_keys', 'my_custom_checkout_field_order_meta_keys');
function my_custom_order_meta_keys( $keys ) {
$keys[] = 'Referal Source';
return $keys;
}
此方法在WooCommerce 2.3中进行了更新,尽管它应该与您的方法向后兼容。看起来您要添加到woocommerce_email_order_meta_keys
过滤器上的数组的键与您更新的元键的名称不匹配,回退到基本数组。就像您拥有它一样,但您的密钥与您在 my_custom_checkout_field_update_order_meta()
函数中更新的元密钥不匹配。
以下是在WC 2.3+中向电子邮件添加密钥的方法:
// WooCommerce 2.3+
function kia_email_order_meta_fields( $fields, $sent_to_admin, $order ) {
$fields['_some_field'] = array(
'label' => __( 'Some field' ),
'value' => get_post_meta( $order->id, '_some_field', true ),
);
$fields['_another_field'] = array(
'label' => __( 'Another field' ),
'value' => get_post_meta( $order->id, '_another_field', true ),
);
return $fields;
}
add_filter('woocommerce_email_order_meta_fields', 'kia_email_order_meta_fields', 10, 3 );