{"id":64,"date":"2019-04-04T15:37:12","date_gmt":"2019-04-04T15:37:12","guid":{"rendered":"http:\/\/wiki.associazionejoint.org\/?p=64"},"modified":"2019-04-04T15:37:12","modified_gmt":"2019-04-04T15:37:12","slug":"come-aggiungere-campi-al-login-form-di-woocommerce","status":"publish","type":"post","link":"https:\/\/wiki.associazionejoint.org\/?p=64","title":{"rendered":"COME AGGIUNGERE CAMPI AL LOGIN FORM DI WOOCOMMERCE"},"content":{"rendered":"<h3>Ci sono tre parti di codice da aggiungere in fondo al functions.php del tema<\/h3>\n<h5>La prima parte del codice serve per creare i campi che andremo ad aggiungere. In questo caso ci saranno nome, cognome e numero di telefono.<\/h5>\n<pre>function wooc_extra_register_fields() {?&gt;\r\n       &lt;p class=\"form-row form-row-wide\"&gt;\r\n       &lt;label for=\"reg_billing_phone\"&gt;&lt;?php _e( 'Phone', 'woocommerce' ); ?&gt;&lt;\/label&gt;\r\n       &lt;input type=\"text\" class=\"input-text\" name=\"billing_phone\" id=\"reg_billing_phone\" value=\"&lt;?php esc_attr_e( $_POST['billing_phone'] ); ?&gt;\" \/&gt;\r\n       &lt;\/p&gt;\r\n       &lt;p class=\"form-row form-row-first\"&gt;\r\n       &lt;label for=\"reg_billing_first_name\"&gt;&lt;?php _e( 'First name', 'woocommerce' ); ?&gt;&lt;span class=\"required\"&gt;*&lt;\/span&gt;&lt;\/label&gt;\r\n       &lt;input type=\"text\" class=\"input-text\" name=\"billing_first_name\" id=\"reg_billing_first_name\" value=\"&lt;?php if ( ! empty( $_POST['billing_first_name'] ) ) esc_attr_e( $_POST['billing_first_name'] ); ?&gt;\" \/&gt;\r\n       &lt;\/p&gt;\r\n       &lt;p class=\"form-row form-row-last\"&gt;\r\n       &lt;label for=\"reg_billing_last_name\"&gt;&lt;?php _e( 'Last name', 'woocommerce' ); ?&gt;&lt;span class=\"required\"&gt;*&lt;\/span&gt;&lt;\/label&gt;\r\n       &lt;input type=\"text\" class=\"input-text\" name=\"billing_last_name\" id=\"reg_billing_last_name\" value=\"&lt;?php if ( ! empty( $_POST['billing_last_name'] ) ) esc_attr_e( $_POST['billing_last_name'] ); ?&gt;\" \/&gt;\r\n       &lt;\/p&gt;\r\n       &lt;div class=\"clear\"&gt;&lt;\/div&gt;\r\n       &lt;?php\r\n }\r\n add_action( 'woocommerce_register_form_start', 'wooc_extra_register_fields' );\r\n\r\n<\/pre>\n<h5>A seguire inseriamo il codice per renderli obbligtori.<\/h5>\n<pre>\/**\r\n\r\n* register fields Validating.\r\n\r\n*\/\r\n\r\nfunction wooc_validate_extra_register_fields( $username, $email, $validation_errors ) {\r\n\r\n      if ( isset( $_POST['billing_first_name'] ) &amp;&amp; empty( $_POST['billing_first_name'] ) ) {\r\n\r\n             $validation_errors-&gt;add( 'billing_first_name_error', __( '&lt;strong&gt;Error&lt;\/strong&gt;: First name is required!', 'woocommerce' ) );\r\n\r\n      }\r\n\r\n      if ( isset( $_POST['billing_last_name'] ) &amp;&amp; empty( $_POST['billing_last_name'] ) ) {\r\n\r\n             $validation_errors-&gt;add( 'billing_last_name_error', __( '&lt;strong&gt;Error&lt;\/strong&gt;: Last name is required!.', 'woocommerce' ) );\r\n\r\n      }\r\n         return $validation_errors;\r\n}\r\n\r\nadd_action( 'woocommerce_register_post', 'wooc_validate_extra_register_fields', 10, 3 );<\/pre>\n<h5>A seguire infine assegnamo questi dati nei DB di wordpress dicendogli di salfarli nel profilo dell&#8217;account ppena creato.<\/h5>\n<pre>\/**\r\n* Below code save extra fields.\r\n*\/\r\nfunction wooc_save_extra_register_fields( $customer_id ) {\r\n    if ( isset( $_POST['billing_phone'] ) ) {\r\n                 \/\/ Phone input filed which is used in WooCommerce\r\n                 update_user_meta( $customer_id, 'billing_phone', sanitize_text_field( $_POST['billing_phone'] ) );\r\n          }\r\n      if ( isset( $_POST['billing_first_name'] ) ) {\r\n             \/\/First name field which is by default\r\n             update_user_meta( $customer_id, 'first_name', sanitize_text_field( $_POST['billing_first_name'] ) );\r\n             \/\/ First name field which is used in WooCommerce\r\n             update_user_meta( $customer_id, 'billing_first_name', sanitize_text_field( $_POST['billing_first_name'] ) );\r\n      }\r\n      if ( isset( $_POST['billing_last_name'] ) ) {\r\n             \/\/ Last name field which is by default\r\n             update_user_meta( $customer_id, 'last_name', sanitize_text_field( $_POST['billing_last_name'] ) );\r\n             \/\/ Last name field which is used in WooCommerce\r\n             update_user_meta( $customer_id, 'billing_last_name', sanitize_text_field( $_POST['billing_last_name'] ) );\r\n      }\r\n\r\n}\r\nadd_action( 'woocommerce_created_customer', 'wooc_save_extra_register_fields' );<\/pre>\n<p>A questo punto dovrebbe funzionare tutto. Se vogliamo aggiungere un email per verificare l&#8217;indirizzo email utilizzato bisogner\u00e0 aggiungere un plugin :<\/p>\n<blockquote class=\"wp-embedded-content\" data-secret=\"XBoW2WoC0q\"><p><a href=\"https:\/\/it.wordpress.org\/plugins\/woo-confirmation-email\/\">Woocommerce User Email Verification<\/a><\/p><\/blockquote>\n<p><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" src=\"https:\/\/it.wordpress.org\/plugins\/woo-confirmation-email\/embed\/#?secret=XBoW2WoC0q\" data-secret=\"XBoW2WoC0q\" width=\"600\" height=\"338\" title=\"&#8220;Woocommerce User Email Verification&#8221; &#8212; Plugin Directory\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe><\/p>\n<p>Consiglio di usare l&#8217;opzione per includere il link nell&#8217;email di conferma di woocommerce quando un account viene creato cosi si fa tutto in uno.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ci sono tre parti di codice da aggiungere in fondo al functions.php del tema La prima parte del codice serve per creare i campi che andremo ad aggiungere. In questo caso ci saranno nome, cognome e numero di telefono. function wooc_extra_register_fields() {?&gt; &lt;p class=&#8221;form-row form-row-wide&#8221;&gt; &lt;label for=&#8221;reg_billing_phone&#8221;&gt;&lt;?php _e( &#8216;Phone&#8217;, &#8216;woocommerce&#8217; ); ?&gt;&lt;\/label&gt; &lt;input type=&#8221;text&#8221; class=&#8221;input-text&#8221; &hellip; <a href=\"https:\/\/wiki.associazionejoint.org\/?p=64\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;COME AGGIUNGERE CAMPI AL LOGIN FORM DI WOOCOMMERCE&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-64","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/wiki.associazionejoint.org\/index.php?rest_route=\/wp\/v2\/posts\/64","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wiki.associazionejoint.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wiki.associazionejoint.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wiki.associazionejoint.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wiki.associazionejoint.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=64"}],"version-history":[{"count":1,"href":"https:\/\/wiki.associazionejoint.org\/index.php?rest_route=\/wp\/v2\/posts\/64\/revisions"}],"predecessor-version":[{"id":65,"href":"https:\/\/wiki.associazionejoint.org\/index.php?rest_route=\/wp\/v2\/posts\/64\/revisions\/65"}],"wp:attachment":[{"href":"https:\/\/wiki.associazionejoint.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=64"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wiki.associazionejoint.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=64"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wiki.associazionejoint.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=64"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}