Navigation
This is archived content. Visit our new forum.

Tagged: 

  • Author
    Posts
  • #1057218

    eimkeith
    Participant

    Hey guys;

    I have an issue where not having a customer’s telephone number renders my Fedex label generation useless, so I need to force the requirement of telephone entry at login or checkout.

    I’ve read that Woocommerce used to make the telephone number compulsory – is there a way to add that field as part of the required shipping or account information?

    #1057566

    Prasant Rai
    Moderator

    Hello There,

    Thanks for writing in! To assist you with this issue, we’ll first need you to provide us with your URL. This is to ensure that we can provide you with a tailored answer to your situation. Once you have provided us with your URL, we will be happy to assist you with everything.

    Thanks.

    #1060068

    eimkeith
    Participant

    Hi there, I was able to correct this situation by using the following code that I found online in the child the functions.php: (seems to be working as desired now)

    /**
    * 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>

    <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>

    <?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’, __( ‘Error: First name is required!’, ‘woocommerce’ ) );
    }

    if ( isset( $_POST[‘billing_last_name’] ) && empty( $_POST[‘billing_last_name’] ) ) {
    $validation_errors->add( ‘billing_last_name_error’, __( ‘Error: Last name is required!.’, ‘woocommerce’ ) );
    }

    if ( isset( $_POST[‘billing_phone’] ) && empty( $_POST[‘billing_phone’] ) ) {
    $validation_errors->add( ‘billing_phone_error’, __( ‘Error: Phone 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_phone’] ) ) {
    // WooCommerce billing phone
    update_user_meta( $customer_id, ‘billing_phone’, sanitize_text_field( $_POST[‘billing_phone’] ) );
    }
    }

    add_action( ‘woocommerce_created_customer’, ‘wooc_save_extra_register_fields’ );

    #1060079

    Christopher
    Moderator

    Thanks for sharing.