Created
May 27, 2018 16:48
-
-
Save codeagencybe/e587a386e546e906e1b90c7fcc9b86a7 to your computer and use it in GitHub Desktop.
WooCommerce B2B signup form
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // show new fields in signup form: checkbox for B2B account, billing_company & billing_vat | |
| // perhaps the checkbox can be left away | |
| function ca_extra_register_fields() {?> | |
| <p class="form-row form-row-wide"> | |
| <?php _e( 'Ik registreer een zakelijke account', 'woocommerce' ); ?><label for="reg_billing_account"></label> | |
| <input type="checkbox" class="input-text" name="billing_account" id="reg_billing_account" value="<?php esc_attr_e( $_POST['billing_account'] ); ?>" /> | |
| </p> | |
| <p class="form-row form-row-wide"> | |
| <label for="reg_billing_company"><?php _e( 'Bedrijfsnaam', 'woocommerce' ); ?></label> | |
| <input type="text" class="input-text" name="billing_company" id="reg_billing_company" value="<?php esc_attr_e( $_POST['billing_company'] ); ?>" /> | |
| </p> | |
| <p class="form-row form-row-wide"> | |
| <label for="reg_billing_vat"><?php _e( 'BTW-nummer', 'woocommerce' ); ?></label> | |
| <input type="text" class="input-text" name="billing_vat" id="reg_billing_vat" value="<?php esc_attr_e( $_POST['billing_vat'] ); ?>" /> | |
| </p> | |
| <div class="clear"></div> | |
| <?php | |
| } | |
| add_action( 'woocommerce_register_form', 'ca_extra_register_fields' ); | |
| // validate the new fields. If checkbox is checked, company & vat must be filled in | |
| // if checkbox is removed, then vat field must be validated if company is filled in | |
| function ca_validate_extra_register_fields( $username, $email, $validation_errors ) { | |
| if ( isset( $_POST['billing_account'] ) && empty( $_POST['billing_company'] ) ) { | |
| $validation_errors->add( 'billing_company_error', __( '<strong>Error</strong>: Indien u een zakelijke account registreert, is bedrijfsnaam een verplicht veld!', 'woocommerce' ) ); | |
| } | |
| if ( isset( $_POST['billing_account'] ) && empty( $_POST['billing_vat'] ) ) { | |
| $validation_errors->add( 'billing_vat_error', __( '<strong>Error</strong>: Indien u een zakelijke account registreert, is BTW-nummer een verplicht veld!', 'woocommerce' ) ); | |
| } | |
| if ( isset( $_POST['billing_company'] ) && empty( $_POST['billing_vat'] ) ) { | |
| $validation_errors->add( 'billing_vat_error', __( '<strong>Error</strong>: Indien u een zakelijke account registreert, is BTW-nummer een verplicht veld!', 'woocommerce' ) ); | |
| } | |
| return $validation_errors; | |
| } | |
| add_action( 'woocommerce_register_post', 'ca_validate_extra_register_fields', 10, 3 ); | |
| // save new fields into user meta fields. using billing_ make sure the fields are stored and automatically re-used for checkout form | |
| function ca_save_extra_register_fields( $customer_id ) { | |
| if ( isset( $_POST['billing_account'] ) ) { | |
| // Phone input filed which is used in WooCommerce | |
| update_user_meta( $customer_id, 'billing_account', sanitize_text_field( $_POST['billing_account'] ) ); | |
| } | |
| if ( isset( $_POST['billing_company'] ) ) { | |
| //First name field which is by default | |
| update_user_meta( $customer_id, 'billing_company', sanitize_text_field( $_POST['billing_company'] ) ); | |
| } | |
| if ( isset( $_POST['billing_vat'] ) ) { | |
| // Last name field which is used in WooCommerce | |
| update_user_meta( $customer_id, 'billing_vat', sanitize_text_field( $_POST['billing_vat'] ) ); | |
| } | |
| } | |
| add_action( 'woocommerce_created_customer', 'ca_save_extra_register_fields' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you !