Instantly share code, notes, and snippets.

Embed
What would you like to do?
WooCommerce - Add a special field to the checkout, order emails and user/order meta
/**
* Add the field to the checkout
**/
add_action('woocommerce_after_order_notes', 'my_custom_checkout_field');
function my_custom_checkout_field( $checkout ) {
echo '<div id="my_custom_checkout_field"><h3>'.__('My Field').'</h3>';
/**
* Output the field. This is for 1.4.
*
* To make it compatible with 1.3 use $checkout->checkout_form_field instead:
$checkout->checkout_form_field( 'my_field_name', array(
'type' => 'text',
'class' => array('my-field-class orm-row-wide'),
'label' => __('Fill in this field'),
'placeholder' => __('Enter a number'),
));
**/
woocommerce_form_field( 'my_field_name', array(
'type' => 'text',
'class' => array('my-field-class orm-row-wide'),
'label' => __('Fill in this field'),
'placeholder' => __('Enter a number'),
), $checkout->get_value( 'my_field_name' ));
echo '</div>';
/**
* Optional Javascript to limit the field to a country. This one shows for italy only.
**/
?>
<script type="text/javascript">
jQuery('select#billing_country').live('change', function(){
var country = jQuery('select#billing_country').val();
var check_countries = new Array(<?php echo '"IT"'; ?>);
if (country && jQuery.inArray( country, check_countries ) >= 0) {
jQuery('#my_custom_checkout_field').fadeIn();
} else {
jQuery('#my_custom_checkout_field').fadeOut();
jQuery('#my_custom_checkout_field input').val('');
}
});
</script>
<?php
}
/**
* Process the checkout
**/
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
function my_custom_checkout_field_process() {
global $woocommerce;
// Check if set, if its not set add an error. This one is only requite for companies
if ($_POST['billing_company'])
if (!$_POST['my_field_name'])
$woocommerce->add_error( __('Please enter your XXX.') );
}
/**
* Update the user meta with field value
**/
add_action('woocommerce_checkout_update_user_meta', 'my_custom_checkout_field_update_user_meta');
function my_custom_checkout_field_update_user_meta( $user_id ) {
if ($user_id && $_POST['my_field_name']) update_user_meta( $user_id, 'my_field_name', esc_attr($_POST['my_field_name']) );
}
/**
* 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 ($_POST['my_field_name']) update_post_meta( $order_id, 'My Field', esc_attr($_POST['my_field_name']));
}
/**
* 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 Field';
return $keys;
}
@knutlig

This comment has been minimized.

Copy link

knutlig commented Feb 21, 2012

Hi Mikejolley. Thanks for sharing this. Im a real novice on the backend of woocommerce, and im having problem understanding on which page this code goes to make it appear on the woocommerce checkout page.

Thanks for your reply

@mikejolley

This comment has been minimized.

Copy link
Owner

mikejolley commented Feb 22, 2012

This can go in a plugin or in theme functions.php - wouldn't attempt it unless your good with PHP though.

@hchouhan

This comment has been minimized.

Copy link

hchouhan commented Mar 29, 2012

Thanks so much for the code. I would never had been able to figure it out on my own.

I wanted to know if this is possible. Is there a way for us to create a custom field at checkout that is a list. The data needs to be pulled from the user table and show list of users who's roles are custom such as say Dealer.

@iografica

This comment has been minimized.

Copy link

iografica commented May 27, 2012

I make a plugin and it work. How can i add the field in user profile?

@wonderyak

This comment has been minimized.

Copy link

wonderyak commented Jun 5, 2012

What input types are available here?

@iografica

This comment has been minimized.

Copy link

iografica commented Jun 6, 2012

The text input for iva's field

@Youshah

This comment has been minimized.

Copy link

Youshah commented Jun 9, 2012

If more than one field is added then how to send that field information in email. I tried a lot but could not figure it out.

thanks

@ellendijk

This comment has been minimized.

Copy link

ellendijk commented Jun 29, 2012

Hi Mikejolley,
What does the "please enter your xxx" error mean? Since we've been adding some custom fields to our checkout form I get that error.
Hope you can help me

@ghost

This comment has been minimized.

Copy link

ghost commented Jul 3, 2012

Just what I needed, thanks again Mike ;)

@stephanlenting

This comment has been minimized.

Copy link

stephanlenting commented Jul 17, 2012

Mike, i have the same problem as Youshah, i have made custom fields but they are not displayed in the email.
Also, i can't see them in the order list.
They are however properly stored in the database.

@stephanlenting

This comment has been minimized.

Copy link

stephanlenting commented Jul 17, 2012

p.s. Mike, i've used the custom_override_checkout_fields function for the custom fields.

@sm8422

This comment has been minimized.

Copy link

sm8422 commented Aug 27, 2012

How can add a extra field to registration in checkout page. for example, i want to add a referred by email field when creating account in checkout page.

Please help.
I know how to add extra field but not sure how to place in the registration area.

@terrytsang

This comment has been minimized.

Copy link

terrytsang commented Nov 10, 2012

Hi all, i have just launched the custom checkout fields option extension. With this plugin, you can show/hide any field, change required settings, change field label name and placeholder, add custom fields and css for the checkout form and so on...

http://terrytsang.com/shop/shop/woocommerce-custom-checkout-options/

Hope this will solve all the customization problem, Cheers!

@schalkjoubert

This comment has been minimized.

Copy link

schalkjoubert commented Jun 27, 2013

@terrytsang

Hi, I bought your extension, and created an additional field for VAT Number.
Can you give me some guidance on how to add the field to the PIP ( print invoices and packaging lists )
http://www.woothemes.com/products/print-invoices-packing-lists/

I can sent you the PIP core files should that help...

Many thanx

@hchouhan

This comment has been minimized.

Copy link

hchouhan commented Apr 1, 2014

@mikejolly,

I used the code above but the custom field value was not displayed in the admin order area. I had to use
get_post_meta( $order->id, 'Refereed By', true )

@amberRrucker

This comment has been minimized.

Copy link

amberRrucker commented Apr 13, 2014

Okay, I'm using this snippet to create an additional field on the single product page. I'm hooking it into woocommerce_after_single_product_summary but I need to know what to change the $checkout to. $product and $post dont work. Just wondering if anyone has any ideas. Aside from gravity forms I havent seen anything anywhere about successfully doing this.

@exenewyorker

This comment has been minimized.

Copy link

exenewyorker commented Apr 29, 2014

What is the code to add multiple fields to order details?

@BasezeroADV

This comment has been minimized.

Copy link

BasezeroADV commented Jun 12, 2014

Hi Mikejolley.
Congratulations, this is really very useful ....
Would it be possible make this field mandatory only for Italy?

Thanks :)

@waqaspro

This comment has been minimized.

Copy link

waqaspro commented Jul 19, 2014

Hi, Mike!
How to add file upload field?
Thanks

@rgb4u

This comment has been minimized.

Copy link

rgb4u commented Dec 29, 2014

Hi, i added 2 fields to my product page where category=myName, but i can't save values from them on my order page, could, you please look at my code ? I try many plugins and forms to achieve that without result, just waste time and money ;/
Thanks for any good advice :)

@rahulsinh

This comment has been minimized.

Copy link

rahulsinh commented Mar 16, 2015

hi guys i m working on customized gift shopping website there are some product want multiple image upload with particular product for purchase any gift i am using woocommerce for my website can any body tell me ho to upload a mulitiple image with particular product please help me i am in truble.

@norakramer

This comment has been minimized.

Copy link

norakramer commented Apr 27, 2015

So, I'm not finding it too difficult to ADD custom fields. My issue is getting those custom fields to pass through to the order emails. :(

@Roberto77

This comment has been minimized.

Copy link

Roberto77 commented Jul 14, 2015

Hi
How to add the fields in the admin panel?

@Roberto77

This comment has been minimized.

Copy link

Roberto77 commented Jul 15, 2015

I tried this solution

add_filter( 'woocommerce_admin_billing_fields' , 'admin_field_myfield' );
function admin_field_myfield( $fields ) {
global $post;
$order_id = $post->ID;
$value=get_post_meta( $order_id , 'My Field', true);
$fields['my_field_name'] = array(
'label' => __('My Field', 'woocommerce'),
'show' => true ,
'value' => $value,
);
return $fields;
}

@Roberto77

This comment has been minimized.

Copy link

Roberto77 commented Jul 15, 2015

If you use the function $woocommerce->add_error
I have this error http://www.mysite.it/wp-admin/admin-ajax.php?action=woocommerce_checkout Failed to load resource: the server responded with a status of 500 (Internal Server Error)

Some ideas?

@eder-dias

This comment has been minimized.

Copy link

eder-dias commented Oct 8, 2015

Hi, how to add this field in a new column in the admin orders.

Thanks!

@wholebiz

This comment has been minimized.

Copy link

wholebiz commented Nov 30, 2015

I am finding that the Stripe payment gateway plugin does not respect Required custom fields, although it does respect Required standard fields, while the same fields are respected fine with the PayPal payment method. Any thoughts on this?

@elena18

This comment has been minimized.

Copy link

elena18 commented Dec 11, 2015

Hi I am trying to add a shiiping email field to my checkout page and I want to have it shown on the order email as well.

After having looked around I finally came up with this code that I put in the functions.php: everything worked (I have the new field in the checkout page and I have it in the administrative panel of the orders). Still it doesn't appear on the notification email. What I did wrong?

Here below is my code

// Hook in the checkout page
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );

// Our hooked in function - $fields is passed via the filter!

function custom_override_checkout_fields( $fields ) {
$fields['shipping']['shipping_email'] = array(
'label' => __('Email', 'woocommerce'),
'placeholder' => _x('Email', 'placeholder', 'woocommerce'),
'required' => false,
'class' => array('form-row-wide'),
'clear' => true
);
return $fields;
}

/* Save Field in the DB as Order Meta Data*/

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['shipping']['shipping_email'])) {
update_post_meta($order_id, 'Shipping email', esc_attr($_POST['shipping']['shipping_email']));
}
}

/* display it in the Order details screen*/

add_action('woocommerce_admin_order_data_after_billing_address', 'my_custom_billing_fields_display_admin_order_meta', 10, 1);

function my_custom_billing_fields_display_admin_order_meta($order) {
echo '

' . __('Shipping email') . ':
' . get_post_meta($order->id, '_shipping_email', true) . '

';
}

/**

  • 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[] = 'Shipping email';
return $keys;
}

@MEGGAWORLD

This comment has been minimized.

Copy link

MEGGAWORLD commented Feb 16, 2016

Hello there can anyone help?
I don't know where to paste the code to display the VAT number on the checkout page.
I created a child theme and pasted the code in the fonction.php file but for some reason i get a blank page the website goes off line.

The fonction.php file in the child theme only has a code to add the confirmation password on the check out page for new users.
Could it be the reason this is not working?

Regards
Louis

@loushou

This comment has been minimized.

@swoboda

This comment has been minimized.

Copy link

swoboda commented Nov 10, 2016

@Roberto77

Use wc_add_notice instead:

wc_add_notice( __( 'Please enter something into this new shiny field.' ), 'error' );

Worked for me.

@mounesh143

This comment has been minimized.

Copy link

mounesh143 commented Dec 21, 2016

i want to add new field like country and state fields. Instead of country will add some school name and according to school branch will display like state.. it should be displayed on checkout page. Is it possible or not.. if its possible i can pay the money for that please help it was urgent

@SirGoldman

This comment has been minimized.

Copy link

SirGoldman commented Jan 5, 2017

Hello,

Can i change the position, where it will be displays in the email?

@iabidali

This comment has been minimized.

Copy link

iabidali commented May 10, 2018

It displays after order comments.

How can it be displayed after first/last billing name or before billing email?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment