Advertisement
Guest User

Untitled

a guest
Nov 15th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.87 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.     woocommerce_form_field( 'my_field_name', array(
  11.         'type'          => 'text',
  12.         'class'         => array('my-field-class orm-row-wide'),
  13.         'label'         => __('DOB of Person To be Named On Share Contribution Certificate'),
  14.         'placeholder'   => __('DD/MM/YYYY'),
  15.         ), $checkout->get_value( 'my_field_name' ));
  16.    
  17.     echo '</div>';
  18.    
  19. }
  20. /**
  21.  * Process the checkout
  22.  **/
  23. add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
  24. function my_custom_checkout_field_process() {
  25.     global $woocommerce;
  26.    
  27.     // Check if set, if its not set add an error. This one is only requite for companies
  28.     if ($_POST['billing_company'])
  29.         if (!$_POST['my_field_name'])
  30.             $woocommerce->add_error( __('Please enter your XXX.') );
  31. }
  32. /**
  33.  * Update the user meta with field value
  34.  **/
  35. add_action('woocommerce_checkout_update_user_meta', 'my_custom_checkout_field_update_user_meta');
  36. function my_custom_checkout_field_update_user_meta( $user_id ) {
  37.     if ($user_id && $_POST['my_field_name']) update_user_meta( $user_id, 'my_field_name', esc_attr($_POST['my_field_name']) );
  38. }
  39. /**
  40.  * Update the order meta with field value
  41.  **/
  42. add_action('woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta');
  43. function my_custom_checkout_field_update_order_meta( $order_id ) {
  44.     if ($_POST['my_field_name']) update_post_meta( $order_id, 'My Field', esc_attr($_POST['my_field_name']));
  45. }
  46. /**
  47.  * Add the field to order emails
  48.  **/
  49. add_filter('woocommerce_email_order_meta_keys', 'my_custom_checkout_field_order_meta_keys');
  50. function my_custom_checkout_field_order_meta_keys( $keys ) {
  51.     $keys[] = 'My Field';
  52.     return $keys;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement