将帐单地址添加到Woocommerce注册页面

时间:2016-10-10 18:44:17

标签: woocommerce registration billing

我找到了解决此问题的方法,并添加了此处的代码:Hide billing address from checkout page but keep the information

在“2)”中添加用户注册“我的帐户”页面中的“开票”字段“

这已成功将结算地址添加到注册表单中。注册表单中的字段将数据完美地提供给Woocommerce数据库。

据说当我更改“国家/地区”下拉列表时,“状态”下拉列表不会更新。状态下拉列表正确更新,具体取决于在结帐页面上选择的国家/地区以及“我的帐户”区域中的编辑地址,但不会在新注册页面上。因此,目前只有注册页面适用于美国。

有关如何更改代码以正确更新“状态”下拉列表的任何建议都会非常有用。

谢谢! 标记

2 个答案:

答案 0 :(得分:2)

您需要使用woocommerce_registration_errors进行验证。该代码与above相同,但具有更新的验证方法。

<?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( $errors ) {
    $address = $_POST;
    foreach ($address as $key => $field) :
        if(startsWith($key,'billing_')){
            if($key == 'billing_country' && $field == ''){
                add_the_error($errors, $key, 'Country');
            }
            if($key == 'billing_first_name' && $field == ''){
                add_the_error($errors, $key, 'First Name');
            }
            if($key == 'billing_last_name' && $field == ''){
                add_the_error($errors, $key, 'Last Name');
            }
            if($key == 'billing_address_1' && $field == ''){
                add_the_error($errors, $key, 'Address');
            }
            if($key == 'billing_city' && $field == ''){
                add_the_error($errors, $key, 'City');
            }
            if($key == 'billing_state' && $field == ''){
                add_the_error($errors, $key, 'State');
            }
            if($key == 'billing_postcode' && $field == ''){
                add_the_error($errors, $key, 'Post Code');
            }
            if($key == 'billing_phone' && $field == ''){
                add_the_error($errors, $key, 'Phone Number');
            }

        }
    endforeach;

    return $errors;
}
add_filter( 'woocommerce_registration_errors', 'zk_validation_billing_address', 10 );

function add_the_error( $errors, $key, $field_name ) {
    $message = sprintf( __( '%s is a required field.', 'iconic' ), '<strong>' . $field_name . '</strong>' );
    $errors->add( $key, $message );
}

答案 1 :(得分:1)

要在注册页面上添加结算字段,您可以使用以下代码段:

<?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');