Last active
February 9, 2019 22:56
-
-
Save yanknudtskov/0be45e0fdb9824458a62eb931d17d84d to your computer and use it in GitHub Desktop.
Add Extra WooCommerce Registration Fields #woocommerce
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Add new register fields for WooCommerce registration. | |
| */ | |
| function yanco_woocommerce_extra_register_fields() { | |
| ?> | |
| <p class="form-row form-row-first"> | |
| <label for="reg_billing_first_name"><?php _e( 'First name', 'woocommerce' ); ?> <span class="required">*</span></label> | |
| <input type="text" class="input-text" name="billing_first_name" id="reg_billing_first_name" value="<?php if ( ! empty( $_POST['billing_first_name'] ) ) esc_attr_e( $_POST['billing_first_name'] ); ?>" /> | |
| </p> | |
| <p class="form-row form-row-last"> | |
| <label for="reg_billing_last_name"><?php _e( 'Last name', 'woocommerce' ); ?> <span class="required">*</span></label> | |
| <input type="text" class="input-text" name="billing_last_name" id="reg_billing_last_name" value="<?php if ( ! empty( $_POST['billing_last_name'] ) ) esc_attr_e( $_POST['billing_last_name'] ); ?>" /> | |
| </p> | |
| <p class="form-row form-row-wide"> | |
| <label for="reg_billing_address_1"><?php _e( 'Adresse', 'woocommerce' ); ?></label> | |
| <input type="text" class="input-text" name="billing_address_1" id="reg_billing_address_1" value="<?php if ( ! empty( $_POST['billing_address_1'] ) ) esc_attr_e( $_POST['billing_address_1'] ); ?>" /> | |
| </p> | |
| <p class="form-row form-row-wide"> | |
| <label for="reg_billing_address_2"><?php _e( 'Addresslinje 2', 'woocommerce' ); ?></label> | |
| <input type="text" class="input-text" name="billing_address_2" id="reg_billing_address_2" value="<?php if ( ! empty( $_POST['billing_address_2'] ) ) esc_attr_e( $_POST['billing_address_2'] ); ?>" /> | |
| </p> | |
| <p class="form-row form-row-first"> | |
| <label for="reg_billing_postcode"><?php _e( 'Postnummer', 'woocommerce' ); ?></label> | |
| <input type="text" class="input-text" name="billing_postcode" id="reg_billing_postcode" value="<?php if ( ! empty( $_POST['billing_postcode'] ) ) esc_attr_e( $_POST['billing_postcode'] ); ?>" /> | |
| </p> | |
| <p class="form-row form-row-last"> | |
| <label for="reg_billing_city"><?php _e( 'By', 'woocommerce' ); ?></label> | |
| <input type="text" class="input-text" name="billing_city" id="reg_billing_city" value="<?php if ( ! empty( $_POST['billing_city'] ) ) esc_attr_e( $_POST['billing_city'] ); ?>" /> | |
| </p> | |
| <p class="form-row form-row-wide"> | |
| <label for="reg_billing_phone"><?php _e( 'Phone', 'woocommerce' ); ?></label> | |
| <input type="text" class="input-text" name="billing_phone" id="reg_billing_phone" value="<?php if ( ! empty( $_POST['billing_phone'] ) ) esc_attr_e( $_POST['billing_phone'] ); ?>" /> | |
| </p> | |
| <?php | |
| } | |
| add_action( 'woocommerce_register_form_start', 'yanco_woocommerce_extra_register_fields' ); | |
| /** | |
| * Validate the extra register fields. | |
| * | |
| * @param WP_Error $validation_errors Errors. | |
| * @param string $username Current username. | |
| * @param string $email Current email. | |
| */ | |
| function yanco_woocommerce_validate_extra_register_fields( $validation_errors, $username, $email ) { | |
| if ( isset( $_POST['billing_first_name'] ) && empty( $_POST['billing_first_name'] ) ) { | |
| $validation_errors->add( 'billing_first_name_error', __( '<strong>Error</strong>: First name is required!', 'woocommerce' ) ); | |
| } | |
| if ( isset( $_POST['billing_last_name'] ) && empty( $_POST['billing_last_name'] ) ) { | |
| $validation_errors->add( 'billing_last_name_error', __( '<strong>Error</strong>: Last name is required!.', 'woocommerce' ) ); | |
| } | |
| return $validation_errors; | |
| } | |
| add_filter( 'woocommerce_registration_errors', 'yanco_woocommerce_validate_extra_register_fields', 10, 3 ); | |
| /** | |
| * Save the extra register fields. | |
| * | |
| * @param int $customer_id Current customer ID. | |
| */ | |
| function yanco_woocommerce_save_extra_register_fields( $customer_id ) { | |
| if ( isset( $_POST['billing_first_name'] ) ) { | |
| update_user_meta( $customer_id, 'first_name', sanitize_text_field( $_POST['billing_first_name'] ) ); | |
| update_user_meta( $customer_id, 'billing_first_name', sanitize_text_field( $_POST['billing_first_name'] ) ); | |
| } | |
| if ( isset( $_POST['billing_last_name'] ) ) { | |
| update_user_meta( $customer_id, 'last_name', sanitize_text_field( $_POST['billing_last_name'] ) ); | |
| update_user_meta( $customer_id, 'billing_last_name', sanitize_text_field( $_POST['billing_last_name'] ) ); | |
| } | |
| if ( isset( $_POST['billing_address_1'] ) ) { | |
| update_user_meta( $customer_id, 'billing_address_1', sanitize_text_field( $_POST['billing_address_1'] ) ); | |
| } | |
| if ( isset( $_POST['billing_address_2'] ) ) { | |
| update_user_meta( $customer_id, 'billing_address_2', sanitize_text_field( $_POST['billing_address_2'] ) ); | |
| } | |
| if ( isset( $_POST['billing_postcode'] ) ) { | |
| update_user_meta( $customer_id, 'billing_postcode', sanitize_text_field( $_POST['billing_postcode'] ) ); | |
| } | |
| if ( isset( $_POST['billing_city'] ) ) { | |
| update_user_meta( $customer_id, 'billing_city', sanitize_text_field( $_POST['billing_city'] ) ); | |
| } | |
| if ( isset( $_POST['billing_phone'] ) ) { | |
| update_user_meta( $customer_id, 'billing_phone', sanitize_text_field( $_POST['billing_phone'] ) ); | |
| } | |
| } | |
| add_action( 'woocommerce_created_customer', 'yanco_woocommerce_save_extra_register_fields' ); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment