Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field' );
- function my_custom_checkout_field( $checkout ) {
- woocommerce_form_field( 'date_of_birth', array(
- 'type' => 'text',
- 'class' => array('my-field-class form-row-wide'),
- 'label' => __('Date of Birth'),
- 'placeholder' => __('dd/mm/yyyy'),
- ), $checkout->get_value( 'date_of_birth' ));
- woocommerce_form_field( 'emergency_contact_name', array(
- 'type' => 'text',
- 'class' => array('my-field-class form-row-wide'),
- 'label' => __('Emergency Contact Name'),
- 'placeholder' => __('contact name'),
- ), $checkout->get_value( 'emergency_contact_name' ));
- woocommerce_form_field( 'relation', array(
- 'type' => 'text',
- 'class' => array('my-field-class form-row-wide'),
- 'label' => __('Emergency Relation'),
- 'placeholder' => __('wife/husband'),
- ), $checkout->get_value( 'relation' ));
- woocommerce_form_field( 'emergency_phone', array(
- 'type' => 'text',
- 'class' => array('my-field-class form-row-wide'),
- 'label' => __('Emergency Phone'),
- 'placeholder' => __('xxxx xxx xxx / xxxx xxxx'),
- ), $checkout->get_value( 'emergency_phone' ));
- }
- /**
- * Process the checkout
- */
- add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
- function my_custom_checkout_field_process() {
- // Check if set, if its not set add an error.
- if ( ! $_POST['date_of_birth'] )
- wc_add_notice( __( 'Please enter your date of birth' ), 'error' );
- if ( ! $_POST['emergency_contact_name'] )
- wc_add_notice( __( 'Please enter your Emergency Contact Name' ), 'error' );
- if ( ! $_POST['relation'] )
- wc_add_notice( __( 'Please enter how your Emergency Contact is related to you' ), 'error' );
- if ( ! $_POST['emergency_phone'] )
- wc_add_notice( __( 'Please enter the phone number of your Emergency Contact' ), 'error' );
- }
- /**
- * Update the order meta with field value
- */
- add_action( 'woocommerce_checkout_update_user_meta', 'my_custom_checkout_field_update_user_meta' );
- function my_custom_checkout_field_update_order_meta( $order_id ) {
- if ( ! empty( $_POST['date_of_birth'] ) ) {
- update_user_meta( $user_id, 'Date Of Birth', sanitize_text_field( $_POST['date_of_birth'] ) );
- }
- if ( ! empty( $_POST['emergency_contact_name'] ) ) {
- update_user_meta( $user_id, 'Emergency Contact Name', sanitize_text_field( $_POST['emergency_contact_name'] ) );
- }
- if ( ! empty( $_POST['relation'] ) ) {
- update_user_meta( $user_id, 'Emergency Relation', sanitize_text_field( $_POST['relation'] ) );
- }
- if ( ! empty( $_POST['emergency_phone'] ) ) {
- update_user_meta( $user_id, 'Emergency Phone', sanitize_text_field( $_POST['emergency_phone'] ) );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement