• Resolved ctuxboy

    (@ctuxboy)


    Hello,

    I want the billing address information when with a new customer registration.
    At this moment it works, but when a new customer a required billing-field leave empty thenn it gives no error.

    Found a code-snippet here: https://stackoverflow.com/questions/39964783/add-billing-address-to-woocommerce-registration-page

    And that’s the code i’m use it:

    <?php
    
    // Function to check starting char of a string
    function startsWith($haystack, $needle){
        return $needle === '' || strpos($haystack, $needle) === 0;
    }
    
    // Custom function to display the Billing Address form to registration page
    function zk_add_billing_form_to_registration(){
        global $woocommerce;
        $checkout = $woocommerce->checkout();
        ?>
        <?php foreach ( $checkout->get_checkout_fields( 'billing' ) as $key => $field ) : ?>
    
            <?php if($key!='billing_email'){ 
                woocommerce_form_field( $key, $field, $checkout->get_value( $key ) );
            } ?>
    
        <?php endforeach; 
    }
    add_action('woocommerce_register_form_start','zk_add_billing_form_to_registration');
    
    // Custom function to save Usermeta or Billing Address of registered user
    function zk_save_billing_address($user_id){
        global $woocommerce;
        $address = $_POST;
        foreach ($address as $key => $field){
            if(startsWith($key,'billing_')){
                // Condition to add firstname and last name to user meta table
                if($key == 'billing_first_name' || $key == 'billing_last_name'){
                    $new_key = explode('billing_',$key);
                    update_user_meta( $user_id, $new_key[1], $_POST[$key] );
                }
                update_user_meta( $user_id, $key, $_POST[$key] );
            }
        }
    
    }
    add_action('woocommerce_created_customer','zk_save_billing_address');
    
    // Registration page billing address form Validation
    function zk_validation_billing_address(){
        global $woocommerce;
        $address = $_POST;
        foreach ($address as $key => $field) :
            // Validation: Required fields
            if(startsWith($key,'billing_')){
                if($key == 'billing_country' && $field == ''){
                    $woocommerce->add_error( '<strong>' . __( 'ERROR', 'woocommerce' ) . '</strong>: ' . __( 'Please select a country.', 'woocommerce' ) );
                }
                if($key == 'billing_first_name' && $field == ''){
                    $woocommerce->add_error( '<strong>' . __( 'ERROR', 'woocommerce' ) . '</strong>: ' . __( 'Please enter first name.', 'woocommerce' ) );
                }
                if($key == 'billing_last_name' && $field == ''){
                    $woocommerce->add_error( '<strong>' . __( 'ERROR', 'woocommerce' ) . '</strong>: ' . __( 'Please enter last name.', 'woocommerce' ) );
                }
                if($key == 'billing_address_1' && $field == ''){
                    $woocommerce->add_error( '<strong>' . __( 'ERROR', 'woocommerce' ) . '</strong>: ' . __( 'Please enter address.', 'woocommerce' ) );
                }
                if($key == 'billing_city' && $field == ''){
                    $woocommerce->add_error( '<strong>' . __( 'ERROR', 'woocommerce' ) . '</strong>: ' . __( 'Please enter city.', 'woocommerce' ) );
                }
                if($key == 'billing_state' && $field == ''){
                    $woocommerce->add_error( '<strong>' . __( 'ERROR', 'woocommerce' ) . '</strong>: ' . __( 'Please enter state.', 'woocommerce' ) );
                }
                if($key == 'billing_postcode' && $field == ''){
                    $woocommerce->add_error( '<strong>' . __( 'ERROR', 'woocommerce' ) . '</strong>: ' . __( 'Please enter a postcode.', 'woocommerce' ) );
                }
                /*
                if($key == 'billing_email' && $field == ''){
                    $woocommerce->add_error( '<strong>' . __( 'ERROR', 'woocommerce' ) . '</strong>: ' . __( 'Please enter billing email address.', 'woocommerce' ) );
                }
                */
                if($key == 'billing_phone' && $field == ''){
                    $woocommerce->add_error( '<strong>' . __( 'ERROR', 'woocommerce' ) . '</strong>: ' . __( 'Please enter phone number.', 'woocommerce' ) );
                }
    
            }
        endforeach;
    }
    add_action('register_post','zk_validation_billing_address');
    • This topic was modified 7 years ago by ctuxboy.
Viewing 6 replies - 1 through 6 (of 6 total)
  • We collect the customer’s first name and use this filter to ensure one is entered:

    add_filter( 'woocommerce_process_registration_errors', 'sg_validate_first_name', 10, 4 );
    function sg_validate_first_name ( $validation_error, $username, $password, $email ) {
      $first_name = isset( $_POST['first_name'] ) ? $_POST['first_name'] : '';
      if( !$first_name ) {
        $validation_error->add( 'first_name_missing', 'Please enter your first name.');
      }
      return $validation_error;
    }
    Thread Starter ctuxboy

    (@ctuxboy)

    Hi lorro,

    Thanks a lot for your example.
    I want try this implement i my code and hope it works. This would be awesome ??

    Happy with this help!!!

    Regards,
    Christophe

    Thread Starter ctuxboy

    (@ctuxboy)

    Hi @lorro,

    Tested your code, and it works well.
    But how can i adding more fields?
    Here are some fields that’s required:
    – billing_last_name
    – billing_address_1
    – billing_city
    – …

    making for every field a new function? Or better all this fields in one function?
    How can i do that?

    At this moment i change your snippet to this and it works for the First name field:

    function sg_validate_first_name ( $validation_error, $username, $password, $email ) {
      $first_name = isset( $_POST['billing_first_name'] ) ? $_POST['billing_first_name'] : '';
      if( !$first_name ) {
        $validation_error->add( 'first_name_missing', 'Please enter your first name.');
      }
      return $validation_error;
    }
    Thread Starter ctuxboy

    (@ctuxboy)

    It works when adding in the same function:

    function sg_validate_first_name ( $validation_error, $username, $password, $email ) {
      $first_name = isset( $_POST['billing_first_name'] ) ? $_POST['billing_first_name'] : '';
      if( !$first_name ) {
        $validation_error->add( 'first_name_missing', 'Please enter your first name.');
      }
      
      $first_name = isset( $_POST['billing_last_name'] ) ? $_POST['billing_last_name'] : '';
      if( !$first_name ) {
        $validation_error->add( 'last_name_missing', 'Please enter your last name.');
      }
      return $validation_error;
    }
    Caleb Burks

    (@icaleb)

    Automattic Happiness Engineer

    Right, you can have as many validation lines in the function as you need. It just needs to be hooked into the woocommerce_process_registration_errors hook.

    Can change your function name and the hook callback to be something more generic if you want multiple validations. Also be careful about re-using variable names ??

    Thread Starter ctuxboy

    (@ctuxboy)

    Hi Caleb,

    Thanks for the info.
    It would be better in a loop (i think ‘foreach’), but it works perfectly at this moment ??

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Add required billing address on registration’ is closed to new replies.