-
-
Save mikejolley/1604009 to your computer and use it in GitHub Desktop.
/** | |
* 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; | |
} |
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?
Hi, how to add this field in a new column in the admin orders.
Thanks!
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?
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;
}
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
Use wc_add_notice instead:
wc_add_notice( __( 'Please enter something into this new shiny field.' ), 'error' );
Worked for me.
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
Hello,
Can i change the position, where it will be displays in the email?
It displays after order comments.
How can it be displayed after first/last billing name or before billing email?
How could I add that custom field value to order_comments? Fulfillment software won't pull in the order meta by default.
You can also used to add Email order data by using this code
add_action( "woocommerce_email_after_order_table", "custom_woocommerce_email_after_order_table", 10, 1);
function custom_woocommerce_email_after_order_table( $order ) {
echo '<p><strong>Mobile Phone :</strong>'. get_post_meta( $order->id, "mobile_phone", true ) .'</p>';
echo '<p><strong>Birthday :</strong>'. get_post_meta( $order->id, "birthday", true ) .'</p>';
}
How to show an error when the added field is empty?
Edit: Here is the Updated Code. (I disabled the Last Few Lines which helps to send the detail via Email)
https://gist.github.com/mcnaveen/917fc756f163596ca80e1b7e14233f9f
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;
}