我使用下面的代码段挂钩和函数在签出页面中添加了选项框自定义字段。当用户订购产品时,选项框自定义字段值显示在“backed admin”面板中,但我无法将该字段以订单形式发送电子邮件。
我在这个问题的底部添加了用于发送自定义字段值的代码或函数。
任何人都可以指导我发送此自定义字段,以便向用户客户和网站管理员发送电子邮件。
// Add a new checkout field
function kia_filter_checkout_fields($fields){
$fields[\'extra_fields\'] = array(
\'some_field\' => array(
\'type\' => \'select\',
\'required\' => true,
\'label\' => __( \'Some field\' ),
\'options\' => array(
\'Newspaper\' => __(\'Newspaper Ad\', \'woocommerce\' ),
\'Facebook\' => __(\'Facebook\', \'woocommerce\' ),
\'Twitter\' => __(\'Twitter\', \'woocommerce\' ),
\'Email\' => __(\'Email\', \'woocommerce\' ),
\'Poster\' => __(\'College Poster\', \'woocommerce\' ),
\'SMS\' => __(\'SMS\', \'woocommerce\' ),
\'Telephone\' => __(\'Telephone Call\', \'woocommerce\' ),
\'Friends\' => __(\'Friends\', \'woocommerce\' ),
\'Others\' => __(\' Others\', \'woocommerce\' )
)
)
);
return $fields;
}
add_filter( \'woocommerce_checkout_fields\', \'kia_filter_checkout_fields\' );
// display the extra field on the checkout form
function kia_extra_checkout_fields(){
$checkout = WC()->checkout(); ?>
<div class="extra-fields">
<h3><?php _e( \'How did you learn about us?\' ); ?></h3>
<?php foreach ( $checkout->checkout_fields[\'extra_fields\'] as $key => $field ) : ?>
<?php woocommerce_form_field( $key, $field, $checkout->get_value( $key ) ); ?>
<?php endforeach; ?>
</div>
<?php }
add_action( \'woocommerce_after_order_notes\' ,\'kia_extra_checkout_fields\' );
// save the extra field when checkout is processed
function kia_save_extra_checkout_fields( $order_id, $posted ){
if( isset( $posted[\'some_field\'] ) ) {
update_post_meta( $order_id, \'_some_field\',
sanitize_text_field( $posted[\'some_field\'] ) );
}
}
add_action( \'woocommerce_checkout_update_order_meta\',
\'kia_save_extra_checkout_fields\', 10, 2 );
// display the extra data in the order admin panel
function kia_display_order_data_in_admin( $order ){ ?>
<div class="order_data_column">
<h4><?php _e( \'Extra Details\', \'woocommerce\' ); ?></h4>
<?php
echo \'<p><strong>\' . __( \'Some field\' ) . \':</strong>\'
. get_post_meta( $order->id, \'_some_field\', true ) . \'</p>\'; ?>
</div>
<?php }
add_action( \'woocommerce_admin_order_data_after_order_details\',
\'kia_display_order_data_in_admin\' );
In bellow snippet Function which i am using to send custom field value in order email
/**
* 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[\'How did you hear about us?\'] = $field;
return $keys;
}