webimpian
5/2/2018 - 9:30 AM

Add first name, last name & billing mobile number form at registration page

	<?php
	/**
	 * @snippet       Add First & Last Name to My Account Register Form - WooCommerce
	 * @how-to        Watch tutorial @ https://businessbloomer.com/?p=19055
	 * @sourcecode    https://businessbloomer.com/?p=21974
	 * @author        Rodolfo Melogli
	 * @credits       Claudio SM Web
	 * @compatible    WC 2.6.14, WP 4.7.2, PHP 5.5.9
	 */
	 
	// 1. Add fields
	add_action( 'woocommerce_register_form_start', 'bbloomer_add_name_woo_account_registration' );
	function bbloomer_add_name_woo_account_registration() {
		?>
	 
		<p class="form-row form-row-first">
			<label for="first_name"><?php _e( 'First name', 'woocommerce' ); ?> <span class="required">*</span></label>
			<input type="text" class="input-text form-control" name="first_name" id="first_name" value="<?php if ( ! empty( $_POST['first_name'] ) ) esc_attr_e( $_POST['first_name'] ); ?>" />
		</p>
	 
		<p class="form-row form-row-last">
			<label for="last_name"><?php _e( 'Last name', 'woocommerce' ); ?> <span class="required">*</span></label>
			<input type="text" class="input-text form-control" name="last_name" id="last_name" value="<?php if ( ! empty( $_POST['last_name'] ) ) esc_attr_e( $_POST['last_name'] ); ?>" />
		</p>
		
		<p class="form-row form-row-wide">
			<label for="reg_billing_phone"><?php _e( 'Mobile number', 'woocommerce' ); ?> <span class="required">*</span></label>
			<input type="text" class="input-text form-control" name="billing_phone" id="reg_billing_phone" value="<?php if ( ! empty( $_POST['billing_phone'] ) ) esc_attr_e( $_POST['billing_phone'] ); ?>" />
		</p>
	 
		<div class="clear"></div>
	 
		<?php
	}
	 
	// 2. Validate fields
	add_filter( 'woocommerce_registration_errors', 'bbloomer_validate_name_fields', 10, 3 ); 
	function bbloomer_validate_name_fields( $errors, $username, $email ) {
		if ( isset( $_POST['first_name'] ) && empty( $_POST['first_name'] ) ) {
			$errors->add( 'first_name_error', __( '<strong>Error</strong>: First name is required!', 'woocommerce' ) );
		}
		if ( isset( $_POST['last_name'] ) && empty( $_POST['last_name'] ) ) {
			$errors->add( 'last_name_error', __( '<strong>Error</strong>: Last name is required!.', 'woocommerce' ) );
		}
		if ( isset( $_POST['billing_phone'] ) && empty( $_POST['billing_phone'] ) ) {
			$errors->add( 'billing_phone_error', __( '<strong>Error</strong>: Mobile number is required!.', 'woocommerce' ) );
		}
		return $errors;
	}
	 
	// 3. Save fields
	add_action( 'woocommerce_created_customer', 'bbloomer_save_name_fields' ); 
	function bbloomer_save_name_fields( $customer_id ) {
		if ( isset( $_POST['first_name'] ) ) {
			update_user_meta( $customer_id, 'first_name', sanitize_text_field( $_POST['first_name'] ) );
		}
		if ( isset( $_POST['last_name'] ) ) {
			update_user_meta( $customer_id, 'last_name', sanitize_text_field( $_POST['last_name'] ) );
		}
		if ( isset( $_POST['billing_phone'] ) ) {
			update_user_meta( $customer_id, 'billing_phone', sanitize_text_field( $_POST['billing_phone'] ) );
		}
	 
	}