{"action":"created","issue":{"url":"https://api.github.com/repos/woothemes/woocommerce/issues/7667","labels_url":"https://api.github.com/repos/woothemes/woocommerce/issues/7667/labels{/name}","comments_url":"https://api.github.com/repos/woothemes/woocommerce/issues/7667/comments","events_url":"https://api.github.com/repos/woothemes/woocommerce/issues/7667/events","html_url":"https://github.com/woothemes/woocommerce/issues/7667","id":60251682,"number":7667,"title":"How to add custom fields in user registration on the \"My Account\" page","user":{"login":"crespopantaleon","id":11373885,"avatar_url":"https://avatars.githubusercontent.com/u/11373885?v=3","gravatar_id":"","url":"https://api.github.com/users/crespopantaleon","html_url":"https://github.com/crespopantaleon","followers_url":"https://api.github.com/users/crespopantaleon/followers","following_url":"https://api.github.com/users/crespopantaleon/following{/other_user}","gists_url":"https://api.github.com/users/crespopantaleon/gists{/gist_id}","starred_url":"https://api.github.com/users/crespopantaleon/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/crespopantaleon/subscriptions","organizations_url":"https://api.github.com/users/crespopantaleon/orgs","repos_url":"https://api.github.com/users/crespopantaleon/repos","events_url":"https://api.github.com/users/crespopantaleon/events{/privacy}","received_events_url":"https://api.github.com/users/crespopantaleon/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-03-08T10:56:52Z","updated_at":"2015-03-08T11:25:02Z","closed_at":null,"body":"Hi, first sorry for my english (I'm using a translator).\r\n\r\nI used this code in my store (https://support.woothemes.com/hc/en-us/articles/203182373) by @claudiosmweb  Following this thread (https://github.com/woothemes/woocommerce/issues/6007) I've implemented via a plugin like also explains @claudiosmweb \r\n\r\nI have problems with some things and would be very grateful if you could help me.\r\n\r\nI added several fields: first name, last name, phone, address 1, city, postcode, state (in Spain are provinces) ...\r\n\r\nThe main problem I have with the state field. Reviewing the fields of the database with two test users (a user registered through the registration form at \"My account\" page and other users registered through \"Checkout\" page) differences appear in the records of the database.\r\n\r\nFirstly, it seems that all is well, the state code field is equal for the two users, but in the \"My Account\" information about the address is different. Also, for a user His name appears after \"Hello, xxx ...\" and other shows His email \"Hello, email ...\"\r\n\r\nOn the other hand, for the registered \"Checkout\" user also additional fields are created in the database (country, company, billing email ...)\r\n\r\nEnclosed in this message code and screenshots.\r\n\r\nI suppose that the problem I have when I save the extra fields when the user is created (sanitaze_text_field in the state field ???) and I guess I have not created correctly the state selector (<select> select2 ???). But it's what I think, I'll probably wrong. I'm not a developer, so these problems.\r\n\r\nThank you very much for your time and I hope your help\r\n\r\n![myaccount1](https://cloud.githubusercontent.com/assets/11373885/6545483/9b765ce8-c589-11e4-8aa8-ea950e34183b.jpg)\r\n![myaccount2](https://cloud.githubusercontent.com/assets/11373885/6545485/a05faed0-c589-11e4-94dc-8166adbe21f4.jpg)\r\n![database1](https://cloud.githubusercontent.com/assets/11373885/6545486/a3cdff22-c589-11e4-90fb-bb93a7c951e4.jpg)\r\n![database2](https://cloud.githubusercontent.com/assets/11373885/6545487/a6d222f2-c589-11e4-871e-df118d425631.jpg)"},"comment":{"url":"https://api.github.com/repos/woothemes/woocommerce/issues/comments/77744192","html_url":"https://github.com/woothemes/woocommerce/issues/7667#issuecomment-77744192","issue_url":"https://api.github.com/repos/woothemes/woocommerce/issues/7667","id":77744192,"user":{"login":"crespopantaleon","id":11373885,"avatar_url":"https://avatars.githubusercontent.com/u/11373885?v=3","gravatar_id":"","url":"https://api.github.com/users/crespopantaleon","html_url":"https://github.com/crespopantaleon","followers_url":"https://api.github.com/users/crespopantaleon/followers","following_url":"https://api.github.com/users/crespopantaleon/following{/other_user}","gists_url":"https://api.github.com/users/crespopantaleon/gists{/gist_id}","starred_url":"https://api.github.com/users/crespopantaleon/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/crespopantaleon/subscriptions","organizations_url":"https://api.github.com/users/crespopantaleon/orgs","repos_url":"https://api.github.com/users/crespopantaleon/repos","events_url":"https://api.github.com/users/crespopantaleon/events{/privacy}","received_events_url":"https://api.github.com/users/crespopantaleon/received_events","type":"User","site_admin":false},"created_at":"2015-03-08T11:25:02Z","updated_at":"2015-03-08T11:25:02Z","body":"Here is the code:\r\n\r\n```php\r\n<?php\r\n/**\r\n * Plugin Name: WooCommerce Registration Fields\r\n * Description: My Custom registration fields.\r\n * Version: 1.0\r\n * Author: Antonio Crespo\r\n * License: GPL2\r\n */\r\n\r\n/* Extra Fields in WooCommerce Registration */\r\n\r\n/**\r\n * Add new register fields for WooCommerce registration.\r\n *\r\n * @return string Register fields HTML.\r\n */\r\nfunction wooc_extra_register_fields() {\r\n\t?>\r\n\r\n\t<p class=\"form-row form-row-wide\">\r\n\t<label for=\"reg_billing_first_name\"><?php _e( 'Nombre', 'woocommerce' ); ?> <span class=\"required\">*</span></label>\r\n\t<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'] ); ?>\" />\r\n\t</p>\r\n\r\n\t<p class=\"form-row form-row-wide\">\r\n\t<label for=\"reg_billing_last_name\"><?php _e( 'Apellidos', 'woocommerce' ); ?> <span class=\"required\">*</span></label>\r\n\t<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'] ); ?>\" />\r\n\t</p>\r\n\r\n\t<p class=\"form-row form-row-wide\">\r\n\t<label for=\"reg_billing_phone\"><?php _e( 'Teléfono', 'woocommerce' ); ?> <span class=\"required\">*</span></label>\r\n\t<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'] ); ?>\" />\r\n\t</p>\r\n\t\r\n\t<p class=\"form-row form-row-wide\">\r\n\t<label for=\"reg_billing_address_1\"><?php _e( 'Dirección', 'woocommerce' ); ?> <span class=\"required\">*</span></label>\r\n\t<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'] ); ?>\" />\r\n\t</p>\r\n\r\n\t<p class=\"form-row form-row-wide\">\r\n\t<label for=\"reg_billing_postcode\"><?php _e( 'Código Postal / Zip', 'woocommerce' ); ?> <span class=\"required\">*</span></label>\r\n\t<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'] ); ?>\" />\r\n\t</p>\r\n\t\r\n\t<p class=\"form-row form-row-wide\">\r\n\t<label for=\"reg_billing_city\"><?php _e( 'Localidad / Ciudad', 'woocommerce' ); ?> <span class=\"required\">*</span></label>\r\n\t<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'] ); ?>\" />\r\n\t</p>\r\n\t\r\n\t<p class=\"form-row form-row-wide\">\r\n\t<label for=\"reg_billing_state\"><?php _e( 'Provincia', 'woocommerce' ); ?> <span class=\"required\">*</span></label>\r\n\t<select class=\"state_select\" name=\"billing_state\" id=\"reg_billing_state\">\r\n\t<option value=\"\">Selecciona una província&hellip;</option><option value=\"C\" >A Coru&ntilde;a</option><option value=\"VI\" >Álava</option><option value=\"AB\" >Albacete</option><option value=\"A\" >Alicante</option><option value=\"AL\" >Almería</option><option value=\"O\" >Asturias</option><option value=\"AV\" >Ávila</option><option value=\"BA\" >Badajoz</option><option value=\"PM\" >Baleares</option><option value=\"B\" >Barcelona</option><option value=\"BU\" >Burgos</option><option value=\"CC\" >Cáceres</option><option value=\"CA\" >Cádiz</option><option value=\"S\" >Cantabria</option><option value=\"CS\" >Castellón</option><option value=\"CE\" >Ceuta</option><option value=\"CR\" >Ciudad Real</option><option value=\"CO\">Córdoba</option><option value=\"CU\" >Cuenca</option><option value=\"GI\" >Gerona</option><option value=\"GR\" >Granada</option><option value=\"GU\" >Guadalajara</option><option value=\"SS\" >Guipúzcoa</option><option value=\"H\" >Huelva</option><option value=\"HU\" >Huesca</option><option value=\"J\" >Jaén</option><option value=\"LO\" >La Rioja</option><option value=\"GC\" >Las Palmas</option><option value=\"LE\" >León</option><option value=\"L\" >Lérida</option><option value=\"LU\" >Lugo</option><option value=\"M\" >Madrid</option><option value=\"MA\" >Málaga</option><option value=\"ML\" >Melilla</option><option value=\"MU\" >Murcia</option><option value=\"NA\" >Navarra</option><option value=\"OR\" >Orense</option><option value=\"P\" >Palencia</option><option value=\"PO\" >Pontevedra</option><option value=\"SA\" >Salamanca</option><option value=\"TF\" >Santa Cruz de Tenerife</option><option value=\"SG\" >Segovia</option><option value=\"SE\" >Sevilla</option><option value=\"SO\" >Soria</option><option value=\"T\" >Tarragona</option><option value=\"TE\" >Teruel</option><option value=\"TO\" >Toledo</option><option value=\"V\" >Valencia</option><option value=\"VA\" >Valladolid</option><option value=\"BI\" >Vizcaya</option><option value=\"ZA\" >Zamora</option><option value=\"Z\" >Zaragoza</option>\r\n\t</select>\r\n\t</p>\r\n\t\r\n\t<?php\r\n}\r\n\r\nadd_action( 'woocommerce_register_form_start', 'wooc_extra_register_fields' );\r\n\r\n/**\r\n * Validate the extra register fields.\r\n *\r\n * @param  string $username          Current username.\r\n * @param  string $email             Current email.\r\n * @param  object $validation_errors WP_Error object.\r\n *\r\n * @return void\r\n */\r\nfunction wooc_validate_extra_register_fields( $username, $email, $validation_errors ) {\r\n\tif ( isset( $_POST['billing_first_name'] ) && empty( $_POST['billing_first_name'] ) ) {\r\n\t\t$validation_errors->add( 'billing_first_name_error', __( 'Nombre es un campo requerido.', 'woocommerce' ) );\r\n\t}\r\n\r\n\tif ( isset( $_POST['billing_last_name'] ) && empty( $_POST['billing_last_name'] ) ) {\r\n\t\t$validation_errors->add( 'billing_last_name_error', __( 'Apellidos es un campo requerido.', 'woocommerce' ) );\r\n\t}\r\n\r\n\r\n\tif ( isset( $_POST['billing_phone'] ) && empty( $_POST['billing_phone'] ) ) {\r\n\t\t$validation_errors->add( 'billing_phone_error', __( 'Teléfono es un campo requerido.', 'woocommerce' ) );\r\n\t}\r\n\t\r\n\tif ( isset( $_POST['billing_address_1'] ) && empty( $_POST['billing_address_1'] ) ) {\r\n\t\t$validation_errors->add( 'billing_address_1_error', __( 'Dirección es un campo requerido.', 'woocommerce' ) );\r\n\t}\r\n\t\r\n\tif ( isset( $_POST['billing_postcode'] ) && empty( $_POST['billing_postcode'] ) ) {\r\n\t\t$validation_errors->add( 'billing_postcode_error', __( 'Código postal / Zip es un campo requerido.', 'woocommerce' ) );\r\n\t}\r\n\t\r\n\tif ( isset( $_POST['billing_city'] ) && empty( $_POST['billing_city'] ) ) {\r\n\t\t$validation_errors->add( 'billing_city_error', __( 'Localidad / Ciudad es un campo requerido.', 'woocommerce' ) );\r\n\t}\r\n\t\r\n\tif ( isset( $_POST['billing_state'] ) && empty( $_POST['billing_state'] ) ) {\r\n\t\t$validation_errors->add( 'billing_state', __( 'Provincia es un campo requerido.', 'woocommerce' ) );\r\n\t}\r\n}\r\n\r\nadd_action( 'woocommerce_register_post', 'wooc_validate_extra_register_fields', 10, 3 );\r\n\r\n/**\r\n * Save the extra register fields.\r\n *\r\n * @param  int  $customer_id Current customer ID.\r\n *\r\n * @return void\r\n */\r\nfunction wooc_save_extra_register_fields( $customer_id ) {\r\n\tif ( isset( $_POST['billing_first_name'] ) ) {\r\n\t\t// WordPress default first name field.\r\n\t\tupdate_user_meta( $customer_id, 'first_name', sanitize_text_field( $_POST['billing_first_name'] ) );\r\n\r\n\t\t// WooCommerce billing first name.\r\n\t\tupdate_user_meta( $customer_id, 'billing_first_name', sanitize_text_field( $_POST['billing_first_name'] ) );\r\n\t}\r\n\r\n\tif ( isset( $_POST['billing_last_name'] ) ) {\r\n\t\t// WordPress default last name field.\r\n\t\tupdate_user_meta( $customer_id, 'last_name', sanitize_text_field( $_POST['billing_last_name'] ) );\r\n\r\n\t\t// WooCommerce billing last name.\r\n\t\tupdate_user_meta( $customer_id, 'billing_last_name', sanitize_text_field( $_POST['billing_last_name'] ) );\r\n\t}\r\n\r\n\tif ( isset( $_POST['billing_phone'] ) ) {\r\n\t\t// WooCommerce billing phone\r\n\t\tupdate_user_meta( $customer_id, 'billing_phone', sanitize_text_field( $_POST['billing_phone'] ) );\r\n\t}\r\n\t\r\n\tif ( isset( $_POST['billing_address_1'] ) ) {\r\n\t\t// WooCommerce billing address\r\n\t\tupdate_user_meta( $customer_id, 'billing_address_1', sanitize_text_field( $_POST['billing_address_1'] ) );\r\n\t}\r\n\t\r\n\tif ( isset( $_POST['billing_postcode'] ) ) {\r\n\t\t// WooCommerce billing postcode\r\n\t\tupdate_user_meta( $customer_id, 'billing_postcode', sanitize_text_field( $_POST['billing_postcode'] ) );\r\n\t}\r\n\t\r\n\tif ( isset( $_POST['billing_city'] ) ) {\r\n\t\t// WooCommerce billing city\r\n\t\tupdate_user_meta( $customer_id, 'billing_city', sanitize_text_field( $_POST['billing_city'] ) );\r\n\t}\r\n\t\r\n\tif ( isset( $_POST['billing_state'] ) ) {\r\n\t\t// WooCommerce billing state\r\n\t\tupdate_user_meta( $customer_id, 'billing_state', sanitize_text_field( $_POST['billing_state'] ) );\r\n\t}\r\n}\r\n\r\nadd_action( 'woocommerce_created_customer', 'wooc_save_extra_register_fields' );\r\n```\r\n\r\n\r\n"}}