Advertisement
Guest User

Untitled

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