Advertisement
Guest User

Untitled

a guest
May 26th, 2015
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. /**
  2. * Add the field to the checkout
  3. **/
  4. add_action('woocommerce_after_order_notes', 'my_custom_checkout_field');
  5.  
  6. function my_custom_checkout_field( $checkout ) {
  7.  
  8. echo '<div id="my_custom_checkout_field"><h3>'.__('My Field').'</h3>';
  9.  
  10. /**
  11. * Output the field. This is for 1.4.
  12. *
  13. * To make it compatible with 1.3 use $checkout->checkout_form_field instead:
  14.  
  15. $checkout->checkout_form_field( 'my_field_name', array(
  16. 'type' => 'text',
  17. 'class' => array('my-field-class orm-row-wide'),
  18. 'label' => __('Fill in this field'),
  19. 'placeholder' => __('Enter a number'),
  20. ));
  21. **/
  22. woocommerce_form_field( 'my_field_name', array(
  23. 'type' => 'text',
  24. 'class' => array('my-field-class orm-row-wide'),
  25. 'label' => __('Fill in this field'),
  26. 'placeholder' => __('Enter a number'),
  27. ), $checkout->get_value( 'my_field_name' ));
  28.  
  29. echo '</div>';
  30.  
  31. /**
  32. * Optional Javascript to limit the field to a country. This one shows for italy only.
  33. **/
  34. ?>
  35. <script type="text/javascript">
  36. jQuery('select#billing_country').live('change', function(){
  37.  
  38. var country = jQuery('select#billing_country').val();
  39.  
  40. var check_countries = new Array(<?php echo '"IT"'; ?>);
  41.  
  42. if (country && jQuery.inArray( country, check_countries ) >= 0) {
  43. jQuery('#my_custom_checkout_field').fadeIn();
  44. } else {
  45. jQuery('#my_custom_checkout_field').fadeOut();
  46. jQuery('#my_custom_checkout_field input').val('');
  47. }
  48.  
  49. });
  50. </script>
  51. <?php
  52. }
  53.  
  54. /**
  55. * Process the checkout
  56. **/
  57. add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
  58.  
  59. function my_custom_checkout_field_process() {
  60. global $woocommerce;
  61.  
  62. // Check if set, if its not set add an error. This one is only requite for companies
  63. if ($_POST['billing_company'])
  64. if (!$_POST['my_field_name'])
  65. $woocommerce->add_error( __('Please enter your XXX.') );
  66. }
  67.  
  68. /**
  69. * Update the user meta with field value
  70. **/
  71. add_action('woocommerce_checkout_update_user_meta', 'my_custom_checkout_field_update_user_meta');
  72.  
  73. function my_custom_checkout_field_update_user_meta( $user_id ) {
  74. if ($user_id && $_POST['my_field_name']) update_user_meta( $user_id, 'my_field_name', esc_attr($_POST['my_field_name']) );
  75. }
  76.  
  77. /**
  78. * Update the order meta with field value
  79. **/
  80. add_action('woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta');
  81.  
  82. function my_custom_checkout_field_update_order_meta( $order_id ) {
  83. if ($_POST['my_field_name']) update_post_meta( $order_id, 'My Field', esc_attr($_POST['my_field_name']));
  84. }
  85.  
  86. /**
  87. * Add the field to order emails
  88. **/
  89. add_filter('woocommerce_email_order_meta_keys', 'my_custom_checkout_field_order_meta_keys');
  90.  
  91. function my_custom_checkout_field_order_meta_keys( $keys ) {
  92. $keys[] = 'My Field';
  93. return $keys;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement