{"id":215,"date":"2021-02-12T08:49:03","date_gmt":"2021-02-12T07:49:03","guid":{"rendered":"https:\/\/www.marranzano.com\/wordpress\/?p=215"},"modified":"2021-02-12T08:55:45","modified_gmt":"2021-02-12T07:55:45","slug":"aggiungere-campi-nel-form-di-registrazione-woocommerce","status":"publish","type":"post","link":"https:\/\/www.marranzano.com\/wordpress\/aggiungere-campi-nel-form-di-registrazione-woocommerce\/","title":{"rendered":"Aggiungere campi nel form di registrazione WOOCOMMERCE"},"content":{"rendered":"\n<p>Se devi aggiungere dei campi allo scarno menu di registrazione di woocommerce e in generale di wordpress bisogna modificare il file &#8220;functions.php&#8221; come di seguito indicato. Il fatto di essere riuscito ad inserire extra campi in registration form ha molto facilitato la registrazione.<\/p>\n\n\n\n<p>&#8220;Se serve aggiungere campi (tipo p.iva, sdi) lo puoi fare con il plugin che si chiama: <strong>Checkout Field Editor for WooCommerce<\/strong>&#8220;<\/p>\n\n\n\n<p>Nel caso sotto riportato \u00e8 stato utilizzato il plugin Checkout Field Editor for WooCommerce per aggiungere i campi P.Iva e Societ\u00e0 oltre a quelli gi\u00e0 presenti.<\/p>\n\n\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n\/\/ REGISTRAZIONE UTENTE\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\nfunction woocommerce_add_registration_fields() {?>\n    &lt;p class=\"form-row form-row-first\">\n        &lt;label for=\"reg_billing_first_name\">&lt;?php _e( 'First name', 'woocommerce' ); ?>&lt;span class=\"required\">*&lt;\/span>&lt;\/label>\n        &lt;input type=\"text\" class=\"input-text\" name=\"billing_first_name\" id=\"reg_billing_first_name\" value=\"&lt;?php if ( ! empty( $_POST&#91;'billing_first_name'] ) ) esc_attr_e( $_POST&#91;'billing_first_name'] ); ?>\" \/>\n    &lt;\/p>\n    &lt;p class=\"form-row form-row-last\">\n        &lt;label for=\"reg_billing_last_name\">&lt;?php _e( 'Last name', 'woocommerce' ); ?>&lt;span class=\"required\">*&lt;\/span>&lt;\/label>\n        &lt;input type=\"text\" class=\"input-text\" name=\"billing_last_name\" id=\"reg_billing_last_name\" value=\"&lt;?php if ( ! empty( $_POST&#91;'billing_last_name'] ) ) esc_attr_e( $_POST&#91;'billing_last_name'] ); ?>\" \/>\n    &lt;\/p>\n    &lt;p class=\"form-row form-row-first\">\n        &lt;label for=\"billing_phone\">&lt;?php _e( 'Phone', 'woocommerce' ); ?>&lt;span class=\"required\">*&lt;\/span>&lt;\/label>\n        &lt;input type=\"text\" class=\"input-text\" name=\"billing_phone\" id=\"billing_phone\" value=\"&lt;?php if ( ! empty( $_POST&#91;'billing_phone'] ) ) esc_attr_e( $_POST&#91;'billing_phone'] ); ?>\" \/>\n    &lt;\/p>\n    &lt;p class=\"form-row form-row-last\">\n        &lt;label for=\"reg_billing_company\">&lt;?php _e( 'Societ\u00e0', 'woocommerce' ); ?>&lt;span class=\"required\">*&lt;\/span>&lt;\/label>\n        &lt;input type=\"text\" class=\"input-text\" name=\"billing_company\" id=\"reg_billing_company\" value=\"&lt;?php if ( ! empty( $_POST&#91;'billing_company'] ) ) esc_attr_e( $_POST&#91;'billing_company'] ); ?>\" \/>\n    &lt;\/p>\n\t&lt;p class=\"form-row form-row-last\">\n    &lt;label for=\"reg_billing_piva\">&lt;?php _e( 'Partita Iva', 'woocommerce' ); ?>&lt;span class=\"required\">*&lt;\/span>&lt;\/label>\n    &lt;input type=\"text\" class=\"input-text\" name=\"billing_piva\" id=\"reg_billing_piva\" value=\"&lt;?php if ( ! empty( $_POST&#91;'billing_piva'] ) ) esc_attr_e( $_POST&#91;'billing_piva'] ); ?>\" \/>\n    &lt;\/p>\n    &lt;div class=\"clear\">&lt;\/div>\n    &lt;?php\n}\nadd_action( 'woocommerce_register_form_start', 'woocommerce_add_registration_fields' );\nfunction woocommerce_validate_extra_registration_fields( $username, $email, $validation_errors ) {\n \n    if ( empty( $_POST&#91;'billing_first_name'] ) ) {\n        $validation_errors->add( 'billing_first_name_error', __( 'Nome \u00e8 richiesto!', 'woocommerce' ) );\n    }\n \n    if ( empty( $_POST&#91;'billing_last_name'] ) ) {\n        $validation_errors->add( 'billing_last_name_error', __( 'Cognome \u00e8 richiesto!', 'woocommerce' ) );\n    }\n \n    if( empty($_POST&#91;'billing_phone']) ) {\n        $validation_errors->add( 'billing_phone_error', __( 'Telefono \u00e8 richiesto!', 'woocommerce' ) );\n    }\n \n    if( empty($_POST&#91;'billing_company']) ) {\n        $validation_errors->add( 'billing_company', __( 'Data di nascita \u00e8 richiesta!', 'woocommerce' ) );\n    }\n\t  if( empty($_POST&#91;'billing_piva']) ) {\n        $validation_errors->add( 'billing_piva', __( 'La Partita Iva \u00e8 richiesta!', 'woocommerce' ) );\n    }\n  \n    return $validation_errors;\n}\n \nadd_action( 'woocommerce_register_post', 'woocommerce_validate_extra_registration_fields', 10, 3 );\nfunction woocommerce_save_extra_registration_fields( $customer_id ) {\n    if ( isset( $_POST&#91;'billing_first_name'] ) ) {\n        update_user_meta( $customer_id, 'first_name', sanitize_text_field( $_POST&#91;'billing_first_name'] ) );\n        update_user_meta( $customer_id, 'billing_first_name', sanitize_text_field( $_POST&#91;'billing_first_name'] ) );\n    }\n \n    if ( isset( $_POST&#91;'billing_last_name'] ) ) {\n        update_user_meta( $customer_id, 'last_name', sanitize_text_field( $_POST&#91;'billing_last_name'] ) );\n        update_user_meta( $customer_id, 'billing_last_name', sanitize_text_field( $_POST&#91;'billing_last_name'] ) );\n    }\n \n    if ( isset( $_POST&#91;'billing_phone'] ) ) {\n        update_user_meta( $customer_id, 'billing_phone', sanitize_text_field( $_POST&#91;'billing_phone'] ) );\n    }\n \n    if( isset($_POST&#91;'billing_company']) ) {\n        update_user_meta( $customer_id, 'billing_company', sanitize_text_field( $_POST&#91;'billing_company'] ) );\n    }\n\tif( isset($_POST&#91;'billing_piva']) ) {\n        update_user_meta( $customer_id, 'billing_piva', sanitize_text_field( $_POST&#91;'billing_piva'] ) );\n    }\n }\nadd_action( 'woocommerce_created_customer', 'woocommerce_save_extra_registration_fields' );\n<\/code><\/pre>\n\n\n\n<p>Il RISULTATO:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"419\" height=\"604\" src=\"https:\/\/www.marranzano.com\/wordpress\/wp-content\/uploadstonewstonange_&quot;\u00a3$\/2021\/02\/extra-campi-in-registration-form.jpg\" alt=\"\" class=\"wp-image-216\" srcset=\"https:\/\/www.marranzano.com\/wordpress\/wp-content\/uploadstonewstonange_&quot;\u00a3$\/2021\/02\/extra-campi-in-registration-form.jpg 419w, https:\/\/www.marranzano.com\/wordpress\/wp-content\/uploadstonewstonange_&quot;\u00a3$\/2021\/02\/extra-campi-in-registration-form-208x300.jpg 208w\" sizes=\"auto, (max-width: 419px) 100vw, 419px\" \/><\/figure>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Aggiungere campi nel form di registrazione WOOCOMMERCE<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[28],"tags":[29],"class_list":["post-215","post","type-post","status-publish","format-standard","hentry","category-wordpress-e-woocommerce","tag-aggiungere-campi-nel-form-di-registrazione-woocommerce"],"_links":{"self":[{"href":"https:\/\/www.marranzano.com\/wordpress\/wp-json\/wp\/v2\/posts\/215","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.marranzano.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.marranzano.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.marranzano.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.marranzano.com\/wordpress\/wp-json\/wp\/v2\/comments?post=215"}],"version-history":[{"count":6,"href":"https:\/\/www.marranzano.com\/wordpress\/wp-json\/wp\/v2\/posts\/215\/revisions"}],"predecessor-version":[{"id":223,"href":"https:\/\/www.marranzano.com\/wordpress\/wp-json\/wp\/v2\/posts\/215\/revisions\/223"}],"wp:attachment":[{"href":"https:\/\/www.marranzano.com\/wordpress\/wp-json\/wp\/v2\/media?parent=215"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.marranzano.com\/wordpress\/wp-json\/wp\/v2\/categories?post=215"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.marranzano.com\/wordpress\/wp-json\/wp\/v2\/tags?post=215"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}