0

i use code to separate regiter form in woocomerce this code work on wamp local server but i have error in my website not work(https://khutwabooks.com/reg/ ) . so what the problem here ? and how i can add jquery check for this form before submit my code is

/**
 * @snippet       WooCommerce User Registration Shortcode
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 4.0
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */
   
add_shortcode( 'wc_reg_form_bbloomer', 'bbloomer_separate_registration_form' );
    
function bbloomer_separate_registration_form() {
   if ( is_admin() ) return;
   //if ( is_user_logged_in() ) return;
   ob_start();
   // if (is_user_logged_in() ){ header( 'Location:http://127.0.0.1/2021/my-account' ); die; }
if ( is_user_logged_in() ) {
   wc_add_notice( sprintf( __( 'أنت الآن مسجل الدخول. إذا كنت ترغب في التسجيل بحساب مختلف من فضلك قم بـ <a href="%s">تسجيل الخروج</a> ', 'bbloomer' ), wc_logout_url() ) );
   wc_print_notices();
  
   
  // echo get_site_url();
   ?>
   <br> <p class="woocommerce-message">  سيتم تحويل إلى الصفحة الرئيسية خلال 5 ثواني</p>
  <meta http-equiv="refresh" content="5;url= <?php echo get_site_url()?>" />
  

<?php
} else {
   // NOTE: THE FOLLOWING <FORM></FORM> IS COPIED FROM woocommerce\templates\myaccount\form-login.php
   // IF WOOCOMMERCE RELEASES AN UPDATE TO THAT TEMPLATE, YOU MUST CHANGE THIS ACCORDINGLY
 
   do_action( 'woocommerce_before_customer_login_form' );
 
   ?>
      <form method="post" class="woocommerce-form woocommerce-form-register register" <?php do_action( 'woocommerce_register_form_tag' ); ?> >
 
         <?php do_action( 'woocommerce_register_form_start' ); ?>
 
         <?php if ( 'no' === get_option( 'woocommerce_registration_generate_username' ) ) : ?>
 
            <p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
               <label for="reg_username"><?php esc_html_e( 'Username', 'woocommerce' ); ?> <span class="required">*</span></label>
               <input type="text" class="woocommerce-Input woocommerce-Input--text input-text" name="username" id="reg_username" autocomplete="username" value="<?php echo ( ! empty( $_POST['username'] ) ) ? esc_attr( wp_unslash( $_POST['username'] ) ) : ''; ?>" /><?php // @codingStandardsIgnoreLine ?>
            </p>
 
         <?php endif; ?>
 
         <p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
            <label for="reg_email"><?php esc_html_e( 'Email address', 'woocommerce' ); ?> <span class="required">*</span></label>
            <input type="email" class="woocommerce-Input woocommerce-Input--text input-text" name="email" id="reg_email" autocomplete="email" value="<?php echo ( ! empty( $_POST['email'] ) ) ? esc_attr( wp_unslash( $_POST['email'] ) ) : ''; ?>" /><?php // @codingStandardsIgnoreLine ?>
         </p>
 
         <?php if ( 'no' === get_option( 'woocommerce_registration_generate_password' ) ) : ?>
 
            <p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
               <label for="reg_password"><?php esc_html_e( 'Password', 'woocommerce' ); ?> <span class="required">*</span></label>
               <input type="password" class="woocommerce-Input woocommerce-Input--text input-text" name="password" id="reg_password" autocomplete="new-password" />
            </p>
 
         <?php else : ?>
 
            <p><?php esc_html_e( 'A password will be sent to your email address.', 'woocommerce' ); ?></p>
 
         <?php endif; ?>
 
         <?php do_action( 'woocommerce_register_form' ); ?>
 
         <p class="woocommerce-FormRow form-row">
            <?php wp_nonce_field( 'woocommerce-register', 'woocommerce-register-nonce' ); ?>
            <button type="submit" class="woocommerce-Button woocommerce-button button woocommerce-form-register__submit" name="register" value="<?php esc_attr_e( 'Register', 'woocommerce' ); ?>"><?php esc_html_e( 'Register', 'woocommerce' ); ?></button>
         </p>
 
         <?php do_action( 'woocommerce_register_form_end' ); ?>
 
      </form>
 
   <?php
     
   return ob_get_clean();
}}

and

// Custom function with all extra field data arrays
function extra_register_fields() {
    $text_domain  = 'woocommerce';
    return array(
        'first_name' => array('type' => 'text',    'class' => ['form-row-first'], 'required' => 1, 'label' => __('First name', $text_domain) ),
        'last_name'  => array('type' => 'text',    'class' => ['form-row-last'],  'required' => 1, 'label' => __('Last name', $text_domain) ),
        'country'    => array('type' => 'country', 'class' => ['address-field'],  'required' => 1, 'label' => __( 'المنطقة', $text_domain ) ),
        'state'      => array('type' => 'state',   'class' => ['address-field'],  'required' => 1, 'label' => __( 'State', $text_domain ) ),
        'address_1'   => array('type' => 'text',  'placeholder'=>'الحي',   'class' => ['form-row-wide'],  'required' => 1, 'label' => __( 'عنوان الشارع / الحي', $text_domain ) ),
        'address_2'   => array('type' => 'text',  'placeholder'=>'عنوان الشارع',   'class' => ['form-row-wide'],  'required' => 1, 'label' => __( '', $text_domain ) ),
        'phone'      => array('type' => 'tel', 'placeholder'=>'هاتفك الأساسي',    'class' => ['form-row-wide'],  'required' => 1, 'label' => __( 'Phone', $text_domain ) ),
        'phone2'      => array('type' => 'tel','placeholder'=>'هاتف إضافي في حال انشغال السابق',   'class' => ['form-row-wide'],  'required' => 1, 'label' => __( 'هاتف إضافي آخر', $text_domain ) ),
        'facebook'   => array('type' => 'text', 'placeholder'=>'للتواصل في حال عدم الرد على الهاتف',   'class' => ['form-row-wide'],  'required' => 1, 'label' => __( 'حساب فيس بوكـ أو انستغرام ', $text_domain ) ),
    );
}
//billing_address_2
// Add extra register fields
add_action( 'woocommerce_register_form_start', 'wooc_extra_register_fields' );
function wooc_extra_register_fields() {
    foreach ( extra_register_fields() as $fkey => $values ) {
        if( $fkey === 'phone' ) $values['clear'] = 1;
        if( $fkey === 'state' ) $values['validate'] = ['state'];

        $value = isset($_POST['billing_'.$fkey]) ? esc_attr($_POST['billing_'.$fkey]) : '';

        woocommerce_form_field( 'billing_'.$fkey, $values, $value );
    }
    wp_enqueue_script('wc-country-select', get_site_url().'/wp-content/plugins/woocommerce/assets/js/frontend/country-select.min.js', array('jquery'), true);
}

// Extra register fields validation
add_action( 'woocommerce_register_post', 'wc_validate_reg_form_fields', 10, 3 );
function wc_validate_reg_form_fields( $username, $email, $validation_errors ) {
    foreach ( extra_register_fields() as $fkey => $values ) {
        if (isset($_POST['billing_'.$fkey]) && empty($_POST['billing_'.$fkey]) && $values['required'] ) {
            $validation_errors->add( 'extra_fields', sprintf('%s is a required field', $values['label']) );
        }
    }
    return $validation_errors;
}

// Save extra register fields values
add_action( 'woocommerce_created_customer', 'wooc_save_extra_register_fields' );
function wooc_save_extra_register_fields( $customer_id ) {
    foreach( extra_register_fields() as $fkey => $values ) {
        if ( isset($_POST['billing_'.$fkey]) ) {
            $value = in_array($fkey, ['country', 'state']) ? sanitize_text_field($_POST['billing_'.$fkey]) : esc_attr($_POST['billing_'.$fkey]);

            update_user_meta( $customer_id, 'billing_'.$fkey, $value );

            if ( in_array($fkey, ['first_name', 'last_name','facebook', 'address_1','address_2', 'phone2']) )
                update_user_meta( $customer_id, $fkey, $value );
        }
    }
}
?>
<?php
// Display Facebook and WhatsApp fields in My account eddit billing address
add_filter( 'woocommerce_billing_fields', 'additional_billing_fields', 20, 1 );
function additional_billing_fields($billing_fields) {
    if ( is_wc_endpoint_url( 'edit-address' ) ) {
        foreach ( extra_register_fields() as $fkey => $values ) {
            if ( in_array($fkey, ['address_1','address_2','facebook', 'whatsapp', 'phone2']) ) {
                $billing_fields['billing_'.$fkey] = $values;
            }
        }
    }
    return $billing_fields;
}
?>
<?php
// WordPress User: Add Facebook and WhatsApp fields to billing section
add_filter('woocommerce_customer_meta_fields', 'wordpress_user_account_billing_birthdate_field');
function wordpress_user_account_billing_birthdate_field( $fields ) {
    foreach ( extra_register_fields() as $fkey => $values ) {
        if ( in_array($fkey, ['address_1','address_2','facebook', 'whatsapp','phone2']) ) {
            $fields['billing']['fields']['billing_'.$fkey] = array(
                'label'       => $values['label'],
                'description' => ''
            );
        }
    }
    return $fields;
}
?> ``` 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.