public
Last active

WooCommerce - Add a special field to the checkout, order emails and user/order meta

  • Download Gist
gistfile1.aw
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
/**
* 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;
}

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

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

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.

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

What input types are available here?

The text input for iva's field

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

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

Just what I needed, thanks again Mike ;)

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.

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

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.

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!

@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

@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 )

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.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.