-1

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;
}
1

Finally i got the answer i was wrong because i tried to send the variable you can not send the variable you have to just put the name of custom field slug.

In the bellow snippet i have replaced $field with my custom field name slug _some_field

like:- $keys['How did you learn about us?'] = '_some_field';

/**
 * 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 learn about us?'] = '_some_field';
    return $keys;
}
0

If I have more than one field, then how do I order email?

This code only works for one field; if I add more, then I don't get anything in mail.

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 learn about us?'] = '_some_field';
    return $keys;
}
0

Order Post custom field multiple field as per below.

add_filter('woocommerce_email_order_meta_keys', 'my_woocommerce_email_order_meta_keys');

function my_woocommerce_email_order_meta_keys( $keys ) {
    $keys[] = 'custom_field_slug1';
    $keys[] = 'custom_field_slug2';
    $keys[] = 'custom_field_slug3';
    return $keys;
}

Please try as per above.

its worked for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.