• Resolved patyg

    (@patyg)


    Hello,

    I need to edit the register form in Woocommerce by adding the following fields (which should also be added to the checkout page):

    – First Name
    – Last Name
    – Company
    – Country (dropdown)
    – City
    – Occupation (dropdown)
    – Phone Number
    – Website URL
    – How did you hear about us? (dropdown)

    I’ve tried to search but couldn’t find a solution for the fields I need. Can someone please help me with a free solution for this?

    Thank you

    https://www.ads-software.com/plugins/woocommerce/

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter patyg

    (@patyg)

    EDIT:
    after more research I now have this code:

    /**
     * Plugin Name: WooCommerce Registration Fields
     * Plugin URI: https://claudiosmweb.com/
     * Description: My Custom registration fields.
     * Version: 1.0
     * Author: Claudio Sanches
     * Author URI: https://claudiosmweb.com/
     * License: GPL2
     */
    /**
     * Add new register fields for WooCommerce registration.
     *
     * @return string Register fields HTML.
     */
    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-last">
        <label for="reg_billing_company"><?php _e( 'Company', 'woocommerce' ); ?> <span class="required">*</span></label>
        <input type="text" class="input-text" name="billing_company" id="reg_billing_company" value="<?php if ( ! empty( $_POST['billing_company'] ) ) esc_attr_e( $_POST['billing_company'] ); ?>" />
        </p>
    
        <p class="form-row form-row-last">
        <label for="reg_billing_address_1"><?php _e( 'Job Title', 'woocommerce' ); ?> <span class="required">*</span></label>
        <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'] ); ?>" />
        </p>
    
        <p class="form-row form-row-last">
        <label for="reg_billing_address_2"><?php _e( 'Website', 'woocommerce' ); ?> <span class="required">*</span></label>
        <input type="text" class="input-text" name="billing_address_2" id="reg_billing_address_2" value="<?php if ( ! empty( $_POST['billing_address_2'] ) ) esc_attr_e( $_POST['billing_address_2'] ); ?>" />
        </p>
    
        <div class="clear"></div>
    
        <p class="form-row form-row-wide">
        <label for="reg_billing_phone"><?php _e( 'Phone', 'woocommerce' ); ?> <span class="required">*</span></label>
        <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'] ); ?>" />
        </p>
    
        <p class="form-row form-row-wide">
        <label for="reg_billing_city"><?php _e( 'City', 'woocommerce' ); ?> <span class="required">*</span></label>
        <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'] ); ?>" />
        </p>
    
        <p class="form-row form-row-wide">
        <label for="reg_billing_state"><?php _e( 'State', 'woocommerce' ); ?></label>
        <input type="text" class="input-text" name="billing_state" id="reg_billing_state" value="<?php if ( ! empty( $_POST['billing_state'] ) ) esc_attr_e( $_POST['billing_state'] ); ?>" />
        </p>
    
        <p class="form-row form-row-wide">
        <label for="reg_billing_country"><?php _e( 'Country', '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>
    
        <p class="form-row form-row-wide">
        <label for="reg_billing_postcode"><?php _e( 'How did you hear about Nomadous?', 'woocommerce' ); ?></label>
        <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'] ); ?>" />
        </p>
    
        <?php
    }
    
    add_action( 'woocommerce_register_form_start', 'wooc_extra_register_fields' );
    
    /**
     * Validate the extra register fields.
     *
     * @param  string $username          Current username.
     * @param  string $email             Current email.
     * @param  object $validation_errors WP_Error object.
     *
     * @return void
     */
    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', __( '<strong>Error</strong>: First name is required!', 'woocommerce' ) );
        }
    
        if ( isset( $_POST['billing_last_name'] ) && empty( $_POST['billing_last_name'] ) ) {
            $validation_errors->add( 'billing_last_name_error', __( '<strong>Error</strong>: Last name is required!.', 'woocommerce' ) );
        }
    
        if ( isset( $_POST['billing_company'] ) && empty( $_POST['billing_company'] ) ) {
            $validation_errors->add( 'billing_company_error', __( '<strong>Error</strong>: Company name is required!.', 'woocommerce' ) );
        }
    
        if ( isset( $_POST['billing_address_1'] ) && empty( $_POST['billing_address_1'] ) ) {
            $validation_errors->add( 'billing_address_1_error', __( '<strong>Error</strong>: Job title is required!.', 'woocommerce' ) );
        }
    
        if ( isset( $_POST['billing_address_2'] ) && empty( $_POST['billing_address_2'] ) ) {
            $validation_errors->add( 'billing_address_2_error', __( '<strong>Error</strong>: Website is required!.', 'woocommerce' ) );
        }
    
        if ( isset( $_POST['billing_phone'] ) && empty( $_POST['billing_phone'] ) ) {
            $validation_errors->add( 'billing_phone_error', __( '<strong>Error</strong>: Phone is required!.', 'woocommerce' ) );
        }
    
        if ( isset( $_POST['billing_city'] ) && empty( $_POST['billing_city'] ) ) {
            $validation_errors->add( 'billing_city_error', __( '<strong>Error</strong>: City is required!.', 'woocommerce' ) );
        }
    
        if ( isset( $_POST['billing_country'] ) && empty( $_POST['billing_country'] ) ) {
            $validation_errors->add( 'billing_country_error', __( '<strong>Error</strong>: Country is required!.', 'woocommerce' ) );
        }
    }
    
    add_action( 'woocommerce_register_post', 'wooc_validate_extra_register_fields', 10, 3 );
    
    /**
     * Save the extra register fields.
     *
     * @param  int  $customer_id Current customer ID.
     *
     * @return void
     */
    function wooc_save_extra_register_fields( $customer_id ) {
        if ( isset( $_POST['billing_first_name'] ) ) {
            // WordPress default first name field.
            update_user_meta( $customer_id, 'first_name', sanitize_text_field( $_POST['billing_first_name'] ) );
    
            // WooCommerce billing first name.
            update_user_meta( $customer_id, 'billing_first_name', sanitize_text_field( $_POST['billing_first_name'] ) );
        }
    
        if ( isset( $_POST['billing_last_name'] ) ) {
            // WordPress default last name field.
            update_user_meta( $customer_id, 'last_name', sanitize_text_field( $_POST['billing_last_name'] ) );
    
            // WooCommerce billing last name.
            update_user_meta( $customer_id, 'billing_last_name', sanitize_text_field( $_POST['billing_last_name'] ) );
        }
    
        if ( isset( $_POST['billing_company'] ) ) {
            // WooCommerce company
            update_user_meta( $customer_id, 'billing_company', sanitize_text_field( $_POST['billing_company'] ) );
        }
    
        if ( isset( $_POST['billing_address_1'] ) ) {
            // WooCommerce billing address 1
            update_user_meta( $customer_id, 'billing_address_1', sanitize_text_field( $_POST['billing_address_1'] ) );
        }
    
        if ( isset( $_POST['billing_address_2'] ) ) {
            // WooCommerce billing address 2
            update_user_meta( $customer_id, 'billing_address_2', sanitize_text_field( $_POST['billing_address_2'] ) );
        }
    
        if ( isset( $_POST['billing_phone'] ) ) {
            // WooCommerce billing phone
            update_user_meta( $customer_id, 'billing_phone', sanitize_text_field( $_POST['billing_phone'] ) );
        }
    
        if ( isset( $_POST['billing_city'] ) ) {
            // WooCommerce billing city
            update_user_meta( $customer_id, 'billing_city', sanitize_text_field( $_POST['billing_city'] ) );
        }
    
        if ( isset( $_POST['billing_state'] ) ) {
            // WooCommerce billing state
            update_user_meta( $customer_id, 'billing_state', sanitize_text_field( $_POST['billing_state'] ) );
        }
    
        if ( isset( $_POST['billing_country'] ) ) {
            // WooCommerce billing country
            update_user_meta( $customer_id, 'billing_country', sanitize_text_field( $_POST['billing_country'] ) );
        }
    
        if ( isset( $_POST['billing_postcode'] ) ) {
            // WooCommerce billing postcode
            update_user_meta( $customer_id, 'billing_postcode', sanitize_text_field( $_POST['billing_postcode'] ) );
        }
    }
    
    add_action( 'woocommerce_created_customer', 'wooc_save_extra_register_fields' );

    It now works on showing the fields in the registration for, but it is not adding it to checkout page. Help please?

    Thread Starter patyg

    (@patyg)

    Also, how do I transform the country part in the register form to be a dropdown such as in the checkout page?

    You use
    billing_address_2 to difine something else (the Website i think). Is there not other way to save that data?

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Woocommerce – custom register form’ is closed to new replies.