• Resolved indrabhusanroy

    (@indrabhusanroy)


    Hi, I’m using a snippet to add Billing fields in Woocommerce Registration. Everything works fine except that it is taking shop base country and state and even If I use a country which is not shop base country. I need to allow users to able to select any country and its corresponding state. Can you help.

    Snippet—

    add_action( 'woocommerce_register_form_start', function() {
     
        $countries = new WC_Countries();
        $billingfields = $countries->get_address_fields( $countries->get_base_country(), 'billing_' );
        unset( $billingfields['billing_email'] );
        $fields = apply_filters( 'woocommerce_billing_fields', $billingfields );
     
        foreach( $fields as $key => $field_args ) {
            woocommerce_form_field( $key, $field_args );
        }
     
    }, 15 );
     
    // Billing Fields Registration Validation
    add_action( 'woocommerce_register_post', function( $username, $email, $validation_errors ) {
     
        if( empty( $_POST['billing_first_name'] ) ) {
            $validation_errors->add(
                'billing_first_name_error', __( 'Please provide a first name.', 'woocommerce' )
            );
        }
     
        if( empty( $_POST['billing_last_name'] ) ) {
            $validation_errors->add(
                'billing_last_name_error', __( 'Please provide a last name.', 'woocommerce' )
            );
        }
     
        if( empty( $_POST['billing_country'] ) ) {
            $validation_errors->add(
                'billing_country_error', __( 'Please select a country.', 'woocommerce' )
            );
        }
     
        if( empty( $_POST['billing_address_1'] ) ) {
            $validation_errors->add(
                'billing_address_1_error', __( 'Please provide a street address.', 'woocommerce' )
            );
        }
     
        if( empty( $_POST['billing_city'] ) ) {
            $validation_errors->add(
                'billing_city_error', __( 'Please provide a city or region.', 'woocommerce' )
            );
        }
     
        if( empty( $_POST['billing_state'] ) ) {
            $validation_errors->add(
                'billing_state_error', __( 'Please select a state or province.', 'woocommerce' )
            );
        }
     
        if( empty( $_POST['billing_postcode'] ) ) {
            $validation_errors->add(
                'billing_postcode_error', __( 'Please provide a postal code.', 'woocommerce' )
            );
        }
     
        if( empty( $_POST['billing_phone'] ) ) {
            $validation_errors->add(
                'billing_phone_error', __( 'Please provide a phone number.', 'woocommerce' )
            );
        }
     
        return $validation_errors;
     
    }, 10, 3 );
     
    // Save Billing Fields At Registration
    add_action( 'woocommerce_created_customer', function( $customer_id ) {
     
        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'] )
            );
        }
     
        if( isset( $_POST['billing_company'] ) ) {
            update_user_meta(
                $customer_id, 'billing_company', sanitize_text_field( $_POST['billing_company'] )
            );
        }
     
        if( isset( $_POST['billing_country'] ) ) {
            update_user_meta(
                $customer_id, 'billing_country', sanitize_text_field( $_POST['billing_country'] )
            );
        }
     
        if( isset( $_POST['billing_address_1'] ) ) {
            update_user_meta(
                $customer_id, 'billing_address_1', sanitize_text_field( $_POST['billing_address_1'] )
            );
        }
     
        if( isset( $_POST['billing_address_2'] ) ) {
            update_user_meta(
                $customer_id, 'billing_address_2', sanitize_text_field( $_POST['billing_address_2'] )
            );
        }
     
        if( isset( $_POST['billing_city'] ) ) {
            update_user_meta(
                $customer_id, 'billing_city', sanitize_text_field( $_POST['billing_city'] )
            );
        }
     
        if( isset( $_POST['billing_state'] ) ) {
            update_user_meta(
                $customer_id, 'billing_state', sanitize_text_field( $_POST['billing_state'] )
            );
        }
     
        if( isset( $_POST['billing_postcode'] ) ) {
            update_user_meta(
                $customer_id, 'billing_postcode', sanitize_text_field( $_POST['billing_postcode'] )
            );
        }
     
        if( isset( $_POST['billing_phone'] ) ) {
            update_user_meta(
                $customer_id, 'billing_phone', sanitize_text_field( $_POST['billing_phone'] )
            );
        }
     
    }, 10, 1 );
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Add Billing fields in registration’ is closed to new replies.