Ledger Nano X - The secure hardware wallet
SHARE
TWEET

wasccwoocommercecustomfields

SRD75 Apr 15th, 2016 (edited) 137 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field' );
  2.  
  3. function my_custom_checkout_field( $checkout ) {
  4.  
  5.     woocommerce_form_field( 'date_of_birth', array(
  6.         'type'          => 'text',
  7.         'class'         => array('my-field-class form-row-wide'),
  8.         'label'         => __('Date of Birth'),
  9.         'placeholder'   => __('dd/mm/yyyy'),
  10.         ), $checkout->get_value( 'date_of_birth' ));
  11.        
  12.     woocommerce_form_field( 'emergency_contact_name', array(
  13.         'type'          => 'text',
  14.         'class'         => array('my-field-class form-row-wide'),
  15.         'label'         => __('Emergency Contact Name'),
  16.         'placeholder'   => __('contact name'),
  17.         ), $checkout->get_value( 'emergency_contact_name' ));
  18.        
  19.     woocommerce_form_field( 'relation', array(
  20.         'type'          => 'text',
  21.         'class'         => array('my-field-class form-row-wide'),
  22.         'label'         => __('Emergency Relation'),
  23.         'placeholder'   => __('wife/husband'),
  24.         ), $checkout->get_value( 'relation' ));
  25.        
  26.     woocommerce_form_field( 'emergency_phone', array(
  27.         'type'          => 'text',
  28.         'class'         => array('my-field-class form-row-wide'),
  29.         'label'         => __('Emergency Phone'),
  30.         'placeholder'   => __('xxxx xxx xxx / xxxx xxxx'),
  31.         ), $checkout->get_value( 'emergency_phone' ));
  32.  
  33. }
  34.  
  35. /**
  36.  * Process the checkout
  37.  */
  38.  
  39. add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
  40.  
  41. function my_custom_checkout_field_process() {
  42.     // Check if set, if its not set add an error.
  43.     if ( ! $_POST['date_of_birth'] )
  44.         wc_add_notice( __( 'Please enter your date of birth' ), 'error' );
  45.     if ( ! $_POST['emergency_contact_name'] )
  46.         wc_add_notice( __( 'Please enter your Emergency Contact Name' ), 'error' );    
  47.     if ( ! $_POST['relation'] )
  48.         wc_add_notice( __( 'Please enter how your Emergency Contact is related to you' ), 'error' );
  49.     if ( ! $_POST['emergency_phone'] )
  50.         wc_add_notice( __( 'Please enter the phone number of your Emergency Contact' ), 'error' );             
  51. }
  52.  
  53. /**
  54.  * Update the order meta with field value
  55.  */
  56. add_action( 'woocommerce_checkout_update_user_meta', 'my_custom_checkout_field_update_user_meta' );
  57.  
  58. function my_custom_checkout_field_update_order_meta( $order_id ) {
  59.     if ( ! empty( $_POST['date_of_birth'] ) ) {
  60.         update_user_meta( $user_id, 'Date Of Birth', sanitize_text_field( $_POST['date_of_birth'] ) );
  61.     }
  62.     if ( ! empty( $_POST['emergency_contact_name'] ) ) {
  63.         update_user_meta( $user_id, 'Emergency Contact Name', sanitize_text_field( $_POST['emergency_contact_name'] ) );
  64.     }
  65.     if ( ! empty( $_POST['relation'] ) ) {
  66.         update_user_meta( $user_id, 'Emergency Relation', sanitize_text_field( $_POST['relation'] ) );
  67.     }
  68.     if ( ! empty( $_POST['emergency_phone'] ) ) {
  69.         update_user_meta( $user_id, 'Emergency Phone', sanitize_text_field( $_POST['emergency_phone'] ) );
  70.     }          
  71. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top