Custom Checkout Fields
-
I’m having no luck adding my Custom Checkout Fields to my order emails. I’ve tried adding script to my functions.php with no success.
-
Show me the script.
/**
* 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[] = ‘billing_student’;
$keys[] = ‘student_1’;return $keys;
}Thats out of date. See https://gist.github.com/mikejolley/2263203
I tried with no luck:
/**
* Add the field to order emails
**/
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_example’;
return $keys;
}My field is named “billing_student”. I’m using Checkout Field Editor Plugin to create my field.
https://snag.gy/et4X0.jpgYou’re not setting the array key. Look at my snippet again. This part is wrong
$keys[]
/**
* 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[‘Student’] = ‘billing_student’;
return $keys;
}Still no bueno ??
I’m adding it in the functions.php file in my theme directory. Is that the wrong spot?
Ah my bad, my snippet was also out of date ??
Here is a working one from someone else I found on github you can adjust https://gist.github.com/craigsimps/98adff30f0ac68247d59
add_filter(‘woocommerce_email_order_meta_fields’, ‘dog_breed_checkout_field_order_meta_fields’, 10, 3 );
function dog_breed_checkout_field_order_meta_fields( $fields, $sent_to_admin, $order ) {
$fields[‘billing_student’] = array(
‘label’ => __( ‘Student’ ),
‘value’ => get_post_meta( $order->id, ‘billing_student’, true ),
);
return $fields;
}Still no luck. Do I need to change more of the script for it to work?
Is it stored as billing_student or _billing_student? it might be the underscored version (hidden).
https://gist.github.com/mikejolley/8f22c991e80124114d6b
So in your case, meta_key would be changed to _billing_student. Check postmeta table in the database to confirm the key name.
changing it to _billing_student worked!!!
How can I add multiple fields? Thanks
Repeat this section with a different key:
$fields['meta_key'] = array( 'label' => __( 'Label' ), 'value' => get_post_meta( $order->id, 'meta_key', true ), );
Here is the code for all 3 custom fields:
add_filter(‘woocommerce_email_order_meta_fields’, ‘dog_breed_checkout_field_order_meta_fields’, 10, 3 );
function dog_breed_checkout_field_order_meta_fields( $fields, $sent_to_admin, $order ) {
$fields[‘_billing_student’] = array(
‘label’ => __( ‘Student’ ),
‘value’ => get_post_meta( $order->id, ‘_billing_student’, true ),
);
$fields[‘_teacher’] = array(
‘label’ => __( ‘Teacher’ ),
‘value’ => get_post_meta( $order->id, ‘_teacher’, true ),
);
$fields[‘_grade’] = array(
‘label’ => __( ‘Grade’ ),
‘value’ => get_post_meta( $order->id, ‘_grade’, true ),
);
return $fields;
}But only Student is coming through on the emails.
It works if I change my custom field names to billing_teacher, etc.
I’m wondering if the field name must already contain an “_”? It works and here is the code:add_filter('woocommerce_email_order_meta_fields', 'dog_breed_checkout_field_order_meta_fields', 10, 3 ); function dog_breed_checkout_field_order_meta_fields( $fields, $sent_to_admin, $order ) { $fields['_billing_student'] = array( 'label' => __( 'Student' ), 'value' => get_post_meta( $order->id, '_billing_student', true ), ); $fields['_billing_teacher'] = array( 'label' => __( 'Teacher' ), 'value' => get_post_meta( $order->id, '_billing_teacher', true ), ); $fields['_billing_grade'] = array( 'label' => __( 'Grade' ), 'value' => get_post_meta( $order->id, '_billing_grade', true ), ); return $fields; }
Thanks for all your help!
If the fields are hidden (not in the custom fields section of the order), yes _ is needed.
Hi there, as this is the only place I have found this code working I thought I would ask. this adds the fields to me email; now how would one specify where in the email?
EX:
add_filter('woocommerce_email_order_meta_fields', 'abu_add_checkout_field_order_meta_fields', 10, 3 ); function abu_add_checkout_field_order_meta_fields( $fields, $sent_to_admin, $order ) { // ********* These Two I want after the billing_last_name $fields['_billing_last_name_2'] = array( 'label' => __( '姓(カナ)' ), 'value' => get_post_meta( $order->id, '_billing_last_name_2', true ), ); $fields['_billing_first_name_2'] = array( 'label' => __( '名(カナ)' ), 'value' => get_post_meta( $order->id, '_billing_first_name_2', true ), ); // ********These Two I want after the shipping_last_name $fields['_shipping_last_name_2'] = array( 'label' => __( '姓(カナ)' ), 'value' => get_post_meta( $order->id, '_shipping_last_name_2', true ), ); $fields['_shipping_first_name_2'] = array( 'label' => __( '名(カナ)' ), 'value' => get_post_meta( $order->id, '_shipping_first_name_2', true ), ); // ******This one I want the billing_state or wherever, after one is done I am sure I can figure the others $fields['_billing_city_address_number'] = array( 'label' => __( '市区町村?番地' ), 'value' => get_post_meta( $order->id, '_billing_city_address_number', true ), ); $fields['_shipping_city_address_number'] = array( 'label' => __( '市区町村?番地' ), 'value' => get_post_meta( $order->id, '_shipping_city_address_number', true ), ); return $fields; }
Is there a way to add in the position in this function or will another be required to inject them at specific points?
- The topic ‘Custom Checkout Fields’ is closed to new replies.