i have added option box custom field in checkout page using bellow snippet hook's and function.when user order the product that option box custom field value displaying in backed admin panel but i am not able send that field in order email.
The code or function for sending custom field value i have added in bottom side of this question.
Can any body guide me to send this custom field in order email for both user customer and site 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;
}
Aucun commentaire:
Enregistrer un commentaire