5
votes

Wordpress - Woocommerce

What I try to do:

  • Add some custom fields in the Checkout page.
  • Also, send the value of these fields to my email when receiving a new order.

This is what I did, but failed:

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'] ) );
}
}
}

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.

Solution please. Thanks in advance.

2
post your code please so its easy to tell the issueLSN
Check the comments specified above the code in this link..gist.github.com/mikejolley/1547491Domain
Thanks for replies. I also post my codes for creating the field on Checkout page above. @WisdmLabs Yes, that was the instruction I followed, but it didn't work. The only thing I think I might did wrong is the 'Tracking Code', but isn't it 'my_field' in my case?Wei
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.Domain
@WisdmLabs Thanks, that works.Wei

2 Answers

5
votes

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.

0
votes

I had similar trouble of printing the result until I added an underscore before 'my_field'.

So use

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

it seems when woocommerce is querying the meta data for use in admin section, an underscore is added. So for a field you created as

    add_filter( 'woocommerce_checkout_fields' , 'brown_remove_billing_postcode_checkout' );

function brown_remove_billing_postcode_checkout( $fields ) {

    // Add New Fields
    $fields['billing']['billing_delivery_date'] = array(
        'label'     => __('Delivery Date', 'woocommerce'),
        'placeholder'   => _x('yyyy-mm-dd', 'placeholder', 'woocommerce'),
        'required'  => true,
        'class'     => array('form-row-wide'),
        'clear'     => true,
        'autocomplete' => false
     );

//Update the order meta with field value. Don't miss the underscore before _b
$fields['billing_delivery_date'] = get_post_meta( $order->id, '_billing_delivery_date', true );

    return $fields;
}

You need to add to your functions.php

/*----------------------------------------------
    Add Delivery Date Field to the order emails
-------------------------------------------------*/
add_filter('woocommerce_email_order_meta_keys', 'my_woocommerce_email_order_meta_keys');

function my_woocommerce_email_order_meta_keys( $keys ) {
    $keys['Delivery Date'] = '_billing_delivery_date';
    return $keys;    
}