Full-stack WordPress service - Design. Development. Support.
  • Home
  • Services
    • Design
    • Maintenance
    • Support
  • Pricing
  • About
  • Our Works
  • Get a Quote
  • Menu Menu
Blog - Latest News
You are here: Home1 / Advance WordPress2 / How to make custom sign up form in Woocommerce
extra stock option in woocommerce

How to make custom sign up form in Woocommerce

February 7, 2018/in Advance WordPress, Woocommerce /by Ministry of WP

Sometimes you think about if your customers would like to place their birthday, city etc as sign up information. Later you can wish them on their birthday or you can campaign new products in an area based. There are lots of things can be happened. Let’s see how we can add a custom sign up form in Woocommerce.

/**
 * Add new register fields for WooCommerce registration.
 *
 * @return string Register fields HTML.
 */
function wooc_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>

	<div class="clear"></div>

	<p class="form-row form-row-wide">
	<label for="reg_billing_dob"><?php _e( 'dob', 'woocommerce' ); ?> <span class="required">*</span></label>
	<input type="text" class="input-text" name="billing_dob" id="reg_billing_dob" value="<?php if ( ! empty( $_POST['billing_dob'] ) ) esc_attr_e( $_POST['billing_dob'] ); ?>" />
	</p>

	<?php
}
add_action( 'woocommerce_register_form_start', 'wooc_extra_register_fields' );
/**
 * Validate the extra register fields.
 *
 * @param  string $username          Current username.
 * @param  string $email             Current email.
 * @param  object $validation_errors WP_Error object.
 *
 * @return void
 */
function wooc_validate_extra_register_fields( $username, $email, $validation_errors ) {
	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' ) );
	}
	if ( isset( $_POST['billing_dob'] ) && empty( $_POST['billing_dob'] ) ) {
		$validation_errors->add( 'billing_dob_error', __( '<strong>Error</strong>: Birthday is required!.', 'woocommerce' ) );
	}
}
add_action( 'woocommerce_register_post', 'wooc_validate_extra_register_fields', 10, 3 );
/**
 * Save the extra register fields.
 *
 * @param  int  $customer_id Current customer ID.
 *
 * @return void
 */
function wooc_save_extra_register_fields( $customer_id ) {
	if ( isset( $_POST['billing_first_name'] ) ) {
		// WordPress default first name field.
		update_user_meta( $customer_id, 'first_name', sanitize_text_field( $_POST['billing_first_name'] ) );
		// WooCommerce billing first name.
		update_user_meta( $customer_id, 'billing_first_name', sanitize_text_field( $_POST['billing_first_name'] ) );
	}
	if ( isset( $_POST['billing_last_name'] ) ) {
		// WordPress default last name field.
		update_user_meta( $customer_id, 'last_name', sanitize_text_field( $_POST['billing_last_name'] ) );
		// WooCommerce billing last name.
		update_user_meta( $customer_id, 'billing_last_name', sanitize_text_field( $_POST['billing_last_name'] ) );
	}
	if ( isset( $_POST['billing_dob'] ) ) {
		// WooCommerce billing dob
		update_user_meta( $customer_id, 'billing_dob', sanitize_text_field( $_POST['billing_dob'] ) );
	}
}
add_action( 'woocommerce_created_customer', 'wooc_save_extra_register_fields' );

Just add this code to your functions.php file.

Then Go to Woocommerce settings page to active the sign-up option. Follow the image below.

After logging out(in case you are logged in), go to “My Account page”, then you will see this:

custom sign up form in Woocommerce

You can also customize this code by adding or removing new fields. You need to add your custom fields in three steps. First, add form fields in HTML, then validate the form data, then save it in woocommerce. Done!

Tags: custom sign up form in Woocommerce, Woocommerce
Share this entry
  • Share on Facebook
  • Share on Twitter
  • Share on WhatsApp
  • Share on Pinterest
  • Share on LinkedIn
  • Share on Tumblr
  • Share on Vk
  • Share on Reddit
  • Share by Mail
https://ministryofwp.com/wp-content/uploads/2016/04/extra-stock-option-in-woocommerce.jpg 483 605 Ministry of WP https://ministryofwp.com/wp-content/uploads/2019/01/WP-ministry-logo-120px.png Ministry of WP2018-02-07 06:21:542018-02-08 13:37:33How to make custom sign up form in Woocommerce
You might also like
how to set up woocommerce store Learn how to set up Woocommerce Store in less than 10 Minutes
Make themes woocommerce compatible How to make themes woocommerce compatible?
extra stock option in woocommerce Adding extra stock option in Woocommerce

SUBSCRIBE TO MINISTRY OF WP VIA EMAIL

Latest Tutorials

  • How to make custom sign up form in Woocommerce
  • How to show posts under custom post type taxonomy?
  • How to create variable product in Woocommerce
  • Learn how to set up Woocommerce Store in less than 10 Minutes
  • How to make multilingual site easily?

Categories

  • Advance WordPress
  • Basic WordPress
  • Genesis
  • Woocommerce
  • Wordpress Menu
  • WordPress Plugins
  • WordPress Themes

WE ARE MWP

Quick Navigation

  • Our Works
  • Pricing
  • Get a Quote
  • About
  • Blog
  • Contact

LATEST BLOG POSTS

  • How to make custom sign up form in Woocommerce
  • How to show posts under custom post type taxonomy?
  • How to create variable product in Woocommerce
  • Learn how to set up Woocommerce Store in less than 10 Minutes
  • How to make multilingual site easily?

Newsletter

Entire website content is © 2016-2022 Ministry of WP
  • Facebook
  • Youtube
How to show posts under custom post type taxonomy?
Scroll to top