• Resolved Samuel Jonathan Schneider Fernandez

    (@samuelfernandez)


    Hi,

    i use this Code to hook additional Fields:

    /**
     * Hook additional Registration Fields
     */
    
    function wooc_extra_register_fields() {?>
           <p class="form-row form-row-first">
           <label for="reg_billing_first_name"><?php _e( 'First name', 'woocommerce' ); ?><span class="required">*</span></label>
           <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'] ); ?>" />
           </p>
           <p class="form-row form-row-last">
           <label for="reg_billing_last_name"><?php _e( 'Last name', 'woocommerce' ); ?><span class="required">*</span></label>
           <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'] ); ?>" />
           </p>
    	   <p class="form-row form-row-wide">
           <label for="reg_billing_country"><?php _e( 'Land', 'woocommerce' ); ?><span class="required">*</span></label>
           <input type="text" class="input-text" name="billing_country" id="reg_billing_country" value="<?php if ( ! empty( $_POST['billing_country'] ) ) esc_attr_e( $_POST['billing_country'] ); ?>" />
           </p>
           <div class="clear"></div>
           <?php
     }
     add_action( 'woocommerce_register_form_start', 'wooc_extra_register_fields' );
    
    /**
     * register fields Validating.
     */
    
    function wooc_validate_extra_register_fields( $username, $email, $validation_errors ) {
    
          if ( isset( $_POST['billing_first_name'] ) && empty( $_POST['billing_first_name'] ) ) {
    
                 $validation_errors->add( 'billing_first_name_error', __( 'Vorname ist erforderlich!', 'woocommerce' ) );
    
          }
    
          if ( isset( $_POST['billing_last_name'] ) && empty( $_POST['billing_last_name'] ) ) {
    
                 $validation_errors->add( 'billing_last_name_error', __( 'Nachname ist erforderlich!.', 'woocommerce' ) );
    
          }
          if ( isset( $_POST['billing_country'] ) && empty( $_POST['billing_country'] ) ) {
    
                 $validation_errors->add( 'billing_country_error', __( 'Feld Land ist erforderlich!.', 'woocommerce' ) );
    
          }
             return $validation_errors;
    }
    
    add_action( 'woocommerce_register_post', 'wooc_validate_extra_register_fields', 10, 3 );
    
    /**
    * Save addiotional Field in Database
    */
    
    function wooc_save_extra_register_fields( $customer_id ) {
          if ( isset( $_POST['billing_first_name'] ) ) {
                 //First name field which is by default
                 update_user_meta( $customer_id, 'first_name', sanitize_text_field( $_POST['billing_first_name'] ) );
                 // First name field which is used in WooCommerce
                 update_user_meta( $customer_id, 'billing_first_name', sanitize_text_field( $_POST['billing_first_name'] ) );
          }
          if ( isset( $_POST['billing_last_name'] ) ) {
                 // Last name field which is by default
                 update_user_meta( $customer_id, 'last_name', sanitize_text_field( $_POST['billing_last_name'] ) );
                 // Last name field which is used in WooCommerce
                 update_user_meta( $customer_id, 'billing_last_name', sanitize_text_field( $_POST['billing_last_name'] ) );
          }
        if ( isset( $_POST['billing_country'] ) ) {
                     // Country input filed which is used in WooCommerce
                     update_user_meta( $customer_id, 'billing_country', sanitize_text_field( $_POST['billing_country'] ) );
              }
    
    }
    add_action( 'woocommerce_created_customer', 'wooc_save_extra_register_fields' );

    Last Name and First Name is working fine. After the registration, i get the value in my Database. But for country it doesn’t work. What is the reason for this?

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘Add Country Field to Woo Registration Form’ is closed to new replies.