Business Bloomer
  • About
  • WooCommerce Blog
  • Online Courses
  • Login
  • 0
  • About
  • WooCommerce Blog
  • Online Courses
  • Login
  • 0

WooCommerce: Add Select Field to “My Account” Register Form

> Published: May 2017 - Revised: Apr 2019
> Blog Category: WooCommerce Tips
> Blog Tags: My Account
> Blog Comments: 24 Comments
Tweet

Join 17,000+ WooWeekly subscribers

We’ve already seen how to add First & Last Name to the “My Account” register form. Today, I want to expand a bit and show you how to add and save a select box.

WooCommerce: add a select box @ My Account registration form

PHP Snippet: Add Select Field to “My Account” Register Form | WooCommerce

/**
 * @snippet       Add Select Field to "My Account" Register Form | WooCommerce
 * @how-to        Get CustomizeWoo.com FREE
 * @sourcecode    https://businessbloomer.com/?p=72508
 * @author        Rodolfo Melogli
 * @testedwith    WooCommerce 3.5.7
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */
 
// -------------------
// 1. Show field @ My Account Registration
 
add_action( 'woocommerce_register_form', 'bbloomer_extra_register_select_field' );
 
function bbloomer_extra_register_select_field() {
   
    ?>
 
<p class="form-row form-row-wide">
<label for="find_where"><?php _e( 'Where did you find us?', 'woocommerce' ); ?>  <span class="required">*</span></label>
<select name="find_where" id="find_where" />
    <option value="goo">Google</option>
    <option value="fcb">Facebook</option>
    <option value="twt">Twitter</option>
</select>
</p>
 
<?php
   
}
 
// -------------------
// 2. Save field on Customer Created action
 
add_action( 'woocommerce_created_customer', 'bbloomer_save_extra_register_select_field' );
  
function bbloomer_save_extra_register_select_field( $customer_id ) {
if ( isset( $_POST['find_where'] ) ) {
        update_user_meta( $customer_id, 'find_where', $_POST['find_where'] );
}
}
 
// -------------------
// 3. Display Select Field @ User Profile (admin) and My Account Edit page (front end)
  
add_action( 'show_user_profile', 'bbloomer_show_extra_register_select_field', 30 );
add_action( 'edit_user_profile', 'bbloomer_show_extra_register_select_field', 30 ); 
add_action( 'woocommerce_edit_account_form', 'bbloomer_show_extra_register_select_field', 30 );
  
function bbloomer_show_extra_register_select_field($user){ 
   
  if (empty ($user) ) {
  $user_id = get_current_user_id();
  $user = get_userdata( $user_id );
  }
   
?>    
       
<p class="form-row form-row-wide">
<label for=""><?php _e( 'Where did you find us?', 'woocommerce' ); ?>  <span class="required">*</span></label>
<select name="find_where" id="find_where" />
    <option disabled value> -- select an option -- </option>
    <option value="goo" <?php if (get_the_author_meta( 'find_where', $user->ID ) == "goo") echo 'selected="selected" '; ?>>Google</option>
    <option value="fcb" <?php if (get_the_author_meta( 'find_where', $user->ID ) == "fcb") echo 'selected="selected" '; ?>>Facebook</option>
    <option value="twt" <?php if (get_the_author_meta( 'find_where', $user->ID ) == "twt") echo 'selected="selected" '; ?>>Twitter</option>
</select>
</p>
 
<?php
 
}
 
// -------------------
// 4. Save User Field When Changed From the Admin/Front End Forms
  
add_action( 'personal_options_update', 'bbloomer_save_extra_register_select_field_admin' );    
add_action( 'edit_user_profile_update', 'bbloomer_save_extra_register_select_field_admin' );   
add_action( 'woocommerce_save_account_details', 'bbloomer_save_extra_register_select_field_admin' );
  
function bbloomer_save_extra_register_select_field_admin( $customer_id ){
if ( isset( $_POST['find_where'] ) ) {
   update_user_meta( $customer_id, 'find_where', $_POST['find_where'] );
}
}

Related posts:

  1. WooCommerce: Add New Tab @ My Account Page
  2. WooCommerce: Add First & Last Name to My Account Register Form
  3. WooCommerce: Change User Role for New Customers
  4. WooCommerce: Add Privacy Policy Consent @ My Account Registration
  5. WooCommerce: Separate Login, Registration, My Account Pages
  6. WooCommerce: Allow Users to Edit Processing Orders
  7. WooCommerce: Deny Automatic Login Upon Registration @ My Account
  8. WooCommerce: File Upload @ My Account Registration Form
  9. WooCommerce: Rename “My Account” If Logged Out @ Nav Menu
  10. WooCommerce: Add a Custom Download File @ My Account

Where to add this snippet?

You can place PHP snippets at the bottom of your child theme functions.php file (delete "?>" if you have it there). CSS, on the other hand, goes in your child theme style.css file. Make sure you know what you are doing when editing such files - if you need more guidance, please take a look at my free video tutorial "Where to Place WooCommerce Customization?"

Does this snippet (still) work?

Please let me know in the comments if everything worked as expected. I would be happy to revise the snippet if you report otherwise (please provide screenshots). I have tested this code with Storefront theme, the WooCommerce version listed above and a WordPress-friendly hosting on PHP 7.3.

If you think this code saved you time & money, feel free to join 14,000+ WooCommerce Weekly subscribers for blog post updates or 250+ Business Bloomer supporters for 365 days of WooCommerce benefits. Thank you in advance :)

Need Help with WooCommerce?

Check out these free video tutorials. You can learn how to customize WooCommerce without unnecessary plugins, how to properly configure the WooCommerce plugin settings and even how to master WooCommerce troubleshooting in case of a bug!

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza.

Follow @rmelogli
Category: WooCommerce Tips
Tag: My Account

Post navigation

Previous post: WooCommerce: Change No. of Thumbnails per Row @ Product Gallery
Next post: WooCommerce: Edit Add to Cart Default, Min, Max & Step Product Quantity

24 thoughts on “WooCommerce: Add Select Field to “My Account” Register Form”

  1. Jose
    November 17, 2020

    hi, how could I record the new custom field in the _users table in the wordpress database ?

    Reply
    1. Rodolfo Melogli
      November 25, 2020

      Hi Jose, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

      Reply
  2. Karsten
    August 9, 2020

    thanks for these numerous snippets! A great many of them are in use with us!
    I’m trying to change this a bit …
    The point is that we don’t need a select field, but a text field …
    the background: it is about the possibility that the “new” user can enter a name by which he was “invited”.
    So far we had solved this via an email invite plugin, but they are no longer GDPR compliant….

    I replaced the following in the show field @ My Account Registration:

    <p class="form-row form-row-wide">
    <label for="invite_from"><?php _e( 'Hat dich jemand eingeladen?', 'woocommerce' ); ?>  </label>
    <input type="text" class="input-text" name="invite_from" id="invite_from" value=""></input>
    </p>
    

    But what needs to be done in the section “Display invited Field @ User Profile (admin) and My Account Edit page (front end)”

    <label for=""><?php _e( 'Hat dich jemand eingeladen?', 'woocommerce' ); ?>  </label>
    <input type="text" class="input-text" name="invite_from" id="invite_from" value="">
    <div pseudo="-webkit-textfield-decoration-container">
            <div>
                <div contenteditable="plaintext-only"><?php if (get_the_author_meta( 'invite_from', $user->ID ) == ??????????????????</div>
            </div>
    

    Can you help me? Thanks, Karsten

    Reply
    1. Rodolfo Melogli
      August 18, 2020

      Did you also save it (part 2)?

      Reply
  3. Gideon
    January 4, 2020

    Hi Rodolfo

    How can I add conditional logic to the field i.e greater than 18 years

    Thanks

    Reply
    1. Rodolfo Melogli
      January 6, 2020

      Hi Gideon, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

      Reply
  4. Steve
    October 29, 2019

    What happens to your additional php code when there is an update to wordpress / woocommerce? Will it need re-applying?

    Reply
    1. Rodolfo Melogli
      October 29, 2019

      Hi Steve, it’s unlikely but yes, like all plugins and snippets it might need to be revised in case WooCommerce changes its coding

      Reply
  5. Louis
    May 28, 2019

    Hell there,

    This is like the best Woocommerce customization website i’ve ever come across. Thank you very much for the valuable site.
    I am trying to customized the My Account > Account Details – I would like remove the password option completely and I am trying to integrate OTP service by MiniOrange. Please lemme know how to remove the password related option from there to replace with the OTP option.

    thanks,

    Louis

    Reply
    1. Rodolfo Melogli
      May 28, 2019

      Hi Louis, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

      Reply
  6. Sophia
    April 6, 2019

    Hi Rodolfo,

    Thank you for the tutorial and it’s great. I added a field quite similar and everything looks good until the last step. When I try change the choice and save it, woocommerce would say it updates successfully. But it’s not. It remains the previous choice. Could you please help me out? Thanks a lot.

    Reply
    1. Rodolfo Melogli
      April 11, 2019

      Thank you Sophia ๐Ÿ™‚ If you use the exact copy of my snippet, does it work/save?

      Reply
  7. Monika
    December 11, 2018

    Hey Rodolfo,

    Thank you for the code. It is of a great help!

    Just wanted to notify you that there is a extra closing bracket at the end of the code which needs to be removed.

    Thanks,
    Monika

    Reply
    1. Rodolfo Melogli
      December 11, 2018

      Thank you ๐Ÿ™‚

      Reply
  8. John
    October 17, 2018

    Hi there,

    When you changed the channel where this person has found the website, from e.g. Google to Facebook, and I save it, it does not change it and returns back to the initial choice.

    =(

    Reply
    1. Rodolfo Melogli
      October 19, 2018

      You’re right John ๐Ÿ™‚ I found a few bugs, so in 5 minutes I will publish the revised snippet!

      Reply
  9. Javier Labbe
    March 1, 2018

    Rodolfo,

    This code is excellent! You are saving me again! There isn’t a plugin to accomplish these simple tasks.

    I am going to use this code for one of our Woocommerce sites. I was wondering if it would be difficult to add two features? The first, to make it required. The second, instead of Google, Facebook and twitter, I have Groups enabled for users and would want the choices to be Group A, Group B and Group C. Once the option is picked. I want it to post the selection and update to the user’s profile.

    Many thanks for your hard work.

    Javier

    Reply
    1. Rodolfo Melogli
      March 6, 2018

      Javier, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R

      Reply
  10. Alexander
    September 24, 2017

    Hello Rodolfo!

    This is excellent! I would like to use this form to have someone select the type of account they are registering for. The two account types are Employer and Candidate. Any help would be greatly appreciated!

    Reply
    1. Rodolfo Melogli
      September 27, 2017

      Alexander, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R

      Reply
  11. Lee
    August 8, 2017

    thanks you so much :*

    Reply
    1. Rodolfo Melogli
      August 9, 2017

      ๐Ÿ™‚

      Reply
  12. Michael
    May 31, 2017

    Hello Rodolfo,

    Just want to say thanks for the tutorials – very informative!
    Quick question in the section with option values of slt,prt, and ltd – are these random values?

        &lt;option value=&quot;slt&quot; ID ) == "goo") echo 'selected="selected" '; ?&gt;&gt;Google
        &lt;option value=&quot;prt&quot; ID ) == "fcb") echo 'selected="selected" '; ?&gt;&gt;Facebook
        &lt;option value=&quot;ltd&quot; ID ) == "twt") echo 'selected="selected" '; ?&gt;&gt;Twitter
    
    

    The reason i am asking is I am working on using 8 fields (which I renamed in the Show field @ My Account Registration section) but there seems to be no correlation between that and the code pasted above. Just trying to understand the methodology here ๐Ÿ™‚

    Thanks!
    Michael

    Reply
    1. Rodolfo Melogli
      June 8, 2017

      Hey Michael, thanks for your comment! Yes, you can rename those to whatever you like, as long as you’re consistent in the input value and the echo “selected” check. Let me know

      Reply
Questions? Feedback? Support? Leave your Comment Now!
_____

If you are writing code, please wrap it between shortcodes: [php]code_here[/php]. Failure to complying with this (as well as going off topic, not writing in English, etc.) will result in comment deletion. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining BloomerArmada to get blog comment reply priority, ask me 1-to-1 WooCommerce questions and enjoy many more perks. Thank you :)

Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts
  • WooCommerce: Redirect Product Category Pages
  • WooCommerce: Close Button @ WooCommerce Checkout Notices
  • WooCommerce: Related Products Custom Heading & Subheading
  • WooCommerce: Display Stock Status For External Products
  • WooCommerce: Display Product Grid @ Order Emails e.g. Related Products
About Business Bloomer

With 100,000 (and growing) monthly organic sessions, Business Bloomer is the most consistent, most active and most complete WooCommerce development/customization blog.

Of course this website itself uses the WooCommerce plugin, the Storefront theme and runs on a WooCommerce-friendly hosting.

Join 75,000+ Monthly Readers & 16,500+ Subscribers.

Become a Business Bloomer Supporter.

Join BloomerArmada and become an Official Business Bloomer Supporter:
easy-peasy, and lots of perks for you.
See your Benefits →
Popular Searches: Visual Hook Guides - Checkout Page - Cart Page - Single Product Page - Add to Cart - Emails - Shipping - Prices - Hosting
Latest Articles
  • WooCommerce: Redirect Product Category Pages
  • WooCommerce: Close Button @ WooCommerce Checkout Notices
  • WooCommerce: Related Products Custom Heading & Subheading
  • WooCommerce: Display Stock Status For External Products
  • WooCommerce: Display Product Grid @ Order Emails e.g. Related Products
Latest Comments
  • Rodolfo Melogli on WooCommerce: Get Cart Info (total, items, etc) from $cart Object
  • Rodolfo Melogli on WooCommerce: Hide Checkout Fields if Virtual Product @ Cart
  • Rodolfo Melogli on WooCommerce: Separate Login, Registration, My Account Pages
  • Rodolfo Melogli on WooCommerce: Add to Cart Quantity Plus & Minus Buttons
Find Out More
  • Become a WooCommerce Expert
  • WooCommerce Blog
  • WooCommerce Online Courses
  • WooCommerce Weekly
  • Bloomer Armada
  • Affiliate Program
  • Contact
Contact Info

Ciao! I’m Rodolfo Melogli, an Italian Civil Engineer who has turned into an international WooCommerce expert. You can contact me here:

Email: [email protected]

Twitter: @rmelogli

Hire me by the hour: Get Quote ยป

VisaMastercardAmexPayPal Acceptance Mark
Business Bloomer © 2011-2023 - Terms of Use - Privacy Policy