Home Forums WoodMart support forum SOLUTION: Custom Register Form / Name,Tel,Country,2xPass, +php

SOLUTION: Custom Register Form / Name,Tel,Country,2xPass, +php

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #522747

    AlexWa
    Participant

    Maby Someone need this. Woodmart Admin to ๐Ÿ™‚

    Hi, At last day I try to fix a Register form in Woodmart. ( hm… or Fix Woocomerce Register Form :)) My result is in photo. I use many codes solution in different sources, all I test many time and work in my Theme Woodmart.

    FIXING / ADDING in Register Form:

    1. Field NAME and SURNAME
    2. FIELD Telephon number, need write + before number,
    3. FIELD CHOSE Country, default US,
    4. FIELD Password Confirm
    5. 2x CHECKBOX

    at 1 to 4 all function is automaticly adding to WP Data Base.

    OPTIONAL:
    In My theme Woodmart I use a plugin “Email Verification for WooCommerce” author: WPFactory. So when user register he must verification his email, he need click in email message.

    `//REGISTER FORM: Add Name and Sur name Field
    add_action(‘woocommerce_register_form_start’, ‘wooc_extra_register_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>

    <div class=”clear”></div>
    <p class=”form-row”>
    <label for=”reg_billing_phone”><?php _e(‘Phone (before add prefix e.g. +48)’, ‘woocommerce’); ?></label>
    <input type=”text” required class=”input-text” name=”billing_phone” id=”reg_billing_phone” title=”Please enter only digits (0-9).” />
    </p>
    <?php
    }

    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’] ) || strpos( $_POST[‘billing_phone’], ‘+’ ) !== 0 ) ) {
    $validation_errors->add( ‘billing_phone_error’, __( ‘Phone number must start with a prefix number e.g (+48).’, ‘woocommerce’ ) );
    }

    return $validation_errors;
    }

    add_action( ‘woocommerce_register_post’, ‘wooc_validate_extra_register_fields’, 10, 3 );

    function wooc_save_extra_register_fields( $customer_id ) {
    if ( isset( $_POST[‘billing_phone’] ) ) {
    update_user_meta( $customer_id, ‘billing_phone’, sanitize_text_field( $_POST[‘billing_phone’] ) );
    }

    if ( isset( $_POST[‘billing_first_name’] ) ) {
    update_user_meta( $customer_id, ‘first_name’, sanitize_text_field( $_POST[‘billing_first_name’] ) );
    update_user_meta( $customer_id, ‘billing_first_name’, sanitize_text_field( $_POST[‘billing_first_name’] ) );
    }

    if ( isset( $_POST[‘billing_last_name’] ) ) {
    update_user_meta( $customer_id, ‘last_name’, sanitize_text_field( $_POST[‘billing_last_name’] ) );
    update_user_meta( $customer_id, ‘billing_last_name’, sanitize_text_field( $_POST[‘billing_last_name’] ) );
    }
    }

    add_action( ‘woocommerce_created_customer’, ‘wooc_save_extra_register_fields’ );

    add_filter( ‘woocommerce_my_account_my_address_formatted_address’, function( $args, $customer_id, $name ){
    // the phone is saved as billing_phone and shipping_phone
    $args[‘phone’] = get_user_meta( $customer_id, $name . ‘_phone’, true );
    return $args;
    }, 10, 3 );
    // modify the address formats
    add_filter( ‘woocommerce_localisation_address_formats’, function( $formats ){
    foreach ( $formats as $key => &$format ) {
    // put a break and then the phone after each format.
    $format .= “\n{phone}”;
    }
    return $formats;
    } );
    // add the replacement value
    add_filter( ‘woocommerce_formatted_address_replacements’, function( $replacements, $args ){
    // we want to replace {phone} in the format with the data we populated
    $replacements[‘{phone}’] = $args[‘phone’];
    return $replacements;
    }, 10, 2 );

    //REGISTER FORM: ADD SECOND PASSWORD FIELD, VERIFICATION PASSWORD //
    function woocommerce_registration_errors_validation($reg_errors, $sanitized_user_login, $user_email) {
    global $woocommerce;
    extract( $_POST );
    if ( strcmp( $password, $password2 ) !== 0 ) {
    return new WP_Error( ‘registration-error’, __( ‘Passwords do not match.’, ‘woocommerce’ ) );
    }
    return $reg_errors;
    }
    add_filter(‘woocommerce_registration_errors’, ‘woocommerce_registration_errors_validation’, 10, 3);

    function woocommerce_register_form_password_repeat() {
    ?>
    <p class=”form-row form-row-wide”>
    <label for=”reg_password2″><?php _e( ‘Confirm password’, ‘woocommerce’ ); ?> <span class=”required”>*</span></label>
    <input type=”password” class=”input-text” name=”password2″ id=”reg_password2″ value=”<?php if ( ! empty( $_POST[‘password2’] ) ) echo esc_attr( $_POST[‘password2’] ); ?>” />
    </p>
    <?php
    }
    add_action( ‘woocommerce_register_form’, ‘woocommerce_register_form_password_repeat’ );

    // REGISTER FORM: ADDING CHECKBOX: Privacy Policy and Terms //

    add_action( ‘woocommerce_register_form’, ‘razztech_add_registration_privacy_policy’, 11 );

    function razztech_add_registration_privacy_policy() {

    woocommerce_form_field( ‘privacy_policy_reg’, array(
    ‘type’ => ‘checkbox’,
    ‘class’ => array(‘form-row privacy’),
    ‘label_class’ => array(‘woocommerce-form__label woocommerce-form__label-for-checkbox checkbox’),
    ‘input_class’ => array(‘woocommerce-form__input woocommerce-form__input-checkbox input-checkbox’),
    ‘required’ => true,
    ‘label’ => ‘I\’ve read and accept the Privacy & Cookies‘,
    ));

    }

    // Show error if user does not tick

    add_filter( ‘woocommerce_registration_errors’, ‘razztech_validate_privacy_registration’, 10, 3 );

    function razztech_validate_privacy_registration( $errors, $username, $email ) {
    if ( ! is_checkout() ) {
    if ( ! (int) isset( $_POST[‘privacy_policy_reg’] ) ) {
    $errors->add( ‘privacy_policy_reg_error’, __( ‘Privacy Policy consent is required!’, ‘woocommerce’ ) );
    }
    }
    return $errors;
    }
    // Add term and conditions check box on the registration form
    add_action( ‘woocommerce_register_form’, ‘njengah_terms_and_conditions_to_registration’, 20 );
    function njengah_terms_and_conditions_to_registration() {
    if ( wc_get_page_id( ‘terms’ ) > 0 && is_account_page() ) {
    ?>
    <p class=”form-row terms wc-terms-and-conditions”>
    <label class=”woocommerce-form__label woocommerce-form__label-for-checkbox checkbox”>
    <input type=”checkbox” class=”woocommerce-form__input woocommerce-form__input-checkbox input-checkbox” name=”terms” <?php checked( apply_filters( ‘woocommerce_terms_is_checked_default’, isset( $_POST[‘terms’] ) ), true ); ?> id=”terms” /> <span><?php printf( __( ‘I’ve read and accept the General Terms of Sale and Use‘, ‘woocommerce’ ), esc_url( wc_get_page_permalink( ‘terms’ ) ) ); ?></span> <span class=”required”>*</span>
    </label>
    <input type=”hidden” name=”terms-field” value=”1″ />
    </p>
    <?php
    }
    }
    // Validate required term and conditions check box
    add_action( ‘woocommerce_register_post’, ‘terms_and_conditions_validation’, 20, 3 );
    function terms_and_conditions_validation( $username, $email, $validation_errors ) {
    if ( ! isset( $_POST[‘terms’] ) )
    $validation_errors->add( ‘terms_error’, __( ‘Terms and condition are not checked!’, ‘woocommerce’ ) );
    return $validation_errors;
    }

    // REGISTER FORM: ADDING COUNTRY CHOSE //
    function wh_extra_register_fields()
    {
    $countries_obj = new WC_Countries();
    $countries = $countries_obj->get_allowed_countries();

    // Getin default country from WP data base if exist
    $default_country = get_user_meta(get_current_user_id(), ‘billing_country’, true);

    // If country is empty, default is a ‘US’
    $default_country = empty($default_country) ? ‘US’ : $default_country;

    ?>
    <p class=”form-row form-row-wide”>
    <label for=”billing_country”><?php esc_html_e( ‘Country’, ‘woocommerce’ ); ?> <span class=”required”>*</span></label>
    <select class=”input-text wc-country-select myClass full-width” name=”billing_country” id=”billing_country” data-placeholder=”<?php esc_attr_e( ‘Choose your country’, ‘woocommerce’ ); ?>” required>
    <?php
    foreach ( $countries as $key => $value ) {
    $selected = $default_country === $key ? ‘selected=”selected”‘ : ”;
    echo ‘<option value=”‘ . esc_attr( $key ) . ‘” ‘ . $selected . ‘>’ . esc_html( $value ) . ‘</option>’;
    }
    ?>
    </select>
    </p>
    <?php
    }

    add_action(‘woocommerce_register_form_start’, ‘wh_extra_register_fields’);

    // SAVE COUNTRY IN WP DATABASE //
    function wh_save_country_on_registration($customer_id)
    {
    if (isset($_POST[‘billing_country’]) && !empty($_POST[‘billing_country’]))
    {
    $billing_country = wc_clean($_POST[‘billing_country’]);
    update_user_meta($customer_id, ‘billing_country’, $billing_country);
    }
    }

    add_action(‘woocommerce_created_customer’, ‘wh_save_country_on_registration’);

    // Add Select2 in register page
    function wh_enqueue_select2_script() {
    if ( is_account_page() ) {
    wp_enqueue_script( ‘select2’, ‘https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/js/select2.min.js&#8217;, array( ‘jquery’ ), ‘4.0.13’, true );
    wp_enqueue_style( ‘select2’, ‘https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/css/select2.min.css&#8217;, array(), ‘4.0.13’ );
    }
    }

    add_action( ‘wp_enqueue_scripts’, ‘wh_enqueue_select2_script’ );

    // Inicjalizuj Select2
    function wh_initialize_select2() {
    if ( is_account_page() ) {
    ?>
    <script>
    jQuery( document ).ready( function( $ ) {
    $( ‘.wc-country-select.myClass’ ).select2({
    width: ‘100%’,
    });
    });
    </script>
    <?php
    }
    }

    add_action( ‘wp_footer’, ‘wh_initialize_select2’, 10 );

    Attachments:
    You must be logged in to view attached files.
    #522752

    AlexWa
    Participant

    Oh.. and Add this Code in your Child Theme Function.php or save in txt file change to php file, add to zip archive and add in your WP plugins ๐Ÿ™‚ All its work

    #522868

    Luke Nielsen
    Keymaster

    Hello,

    Yes, the register functionality is related to WooCommerce.

    Anyway, good solution. Thank you for your time and have a good day!

    Kind Regards

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘SOLUTION: Custom Register Form / Name,Tel,Country,2xPass, +php’ is closed to new replies.