woocommerce将自定义结帐字段添加到电子邮件 [英] woocommerce add custom checkout field to email
问题描述
Wordpress-Woocommerce
我要做什么:
- 在结帐页面中添加一些自定义字段。
- 此外,在收到新订单时,请将这些字段的值发送到我的电子邮件中。
这是我所做的,但是失败了:
我在这里遵循该教程: http://docs.woothemes.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/
I follow the tutorial here: http://docs.woothemes.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/
一切正常,我成功创建了字段,它们显示在订单设置页面中:
Everything goes right, I created the fields successfully and they appear in the order setting page:
add_action( 'woocommerce_after_order_notes', 'my_checkout_fields' );
function my_checkout_fields( $checkout ) {
echo '<div id="my_checkout_fields"><h2>' . __('My Heading') . '</h2>';
woocommerce_form_field( 'my_field', array(
'type' => 'select',
'options' => array(
'option_1' => 'My Option 1',
'option_2' => 'My Option 2'
),
'clear' => false,
'class' => array('form-row-wide'),
'label' => __('Whatever')
), $checkout->get_value( 'my_field' ));
echo '</div>';
}
/**
* 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 ( ! empty( $_POST['my_field'] ) ) {
update_post_meta( $order_id, 'My Field', sanitize_text_field( $_POST['my_field'] ) );
}
}
}
然后,我尝试添加如教程中第4课部分所示,将它们放入电子邮件订单元中:
Then, I tried to add them into the email order meta as shown at the 'Lesson 4' section in the tutorial:
add_filter('woocommerce_email_order_meta_keys', 'my_custom_checkout_field_order_meta_keys');
function my_custom_checkout_field_order_meta_keys( $keys ) {
$keys[] = 'my_field';
return $keys;
}
但是,自定义字段不会出现在发送的电子邮件中。
But, the custom fields do not appear in the email sent.
我还尝试通过在电子邮件模板文件中执行此操作来对其进行测试:
I have also tried to test it by doing this in the email template file:
<p><?php echo $order->my_field; ?></p>
电子邮件中没有任何内容。
Nothing is printed in the email.
请解决。
推荐答案
您应该使用我的字段而不是my_field。由于已使用此键保存了自定义字段。这肯定会解决您的问题。
You should use 'My Field' instead of my_field. As you have saved the custom field with this key. This will definitely solve your problem.
这篇关于woocommerce将自定义结帐字段添加到电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!