将帐单地址添加到 Woocommerce 注册页面 [英] Add Billing Address to Woocommerce Registration Page

查看:31
本文介绍了将帐单地址添加到 Woocommerce 注册页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了解决此问题的方法并添加了此处找到的代码:在结帐页面隐藏帐单地址但保留信息

I found a solution to this problem and added the code found here: Hide billing address from checkout page but keep the information

在2) 在用户注册我的帐户页面中添加账单字段"

under "2) Adding Billing fields in user registration My Account page"

这已成功将帐单地址添加到注册表中.注册表中的字段将数据完美地输入到 Woocommerce 数据库中.

This successfully added the billing address to the registration form. The fields from the registration form feed the data into the Woocommerce database perfectly.

也就是说,当我更改国家/地区"下拉列表时,州"下拉列表不会更新.状态下拉列表确实会根据结帐页面上选择的国家/地区以及我的帐户"区域中的编辑地址下(但不会在新注册页面上)正确更新.因此,目前注册页面仅适用于美国.

That being said the "state" dropdown doesn't update when I change the "country" dropdown. The state dropdown does update properly depending on the country selected on the checkout page and also under edit address in the "My Account" area but not on the new registration page. So as it currently stands the registration page only works properly for the USA.

有关如何更改代码以正确更新状态"下拉列表的任何建议都会非常有帮助.

Any advice as to how the code needs to be changed to make the "State" dropdown update correctly would be very helpful.

谢谢!标记

推荐答案

您需要使用 woocommerce_registration_errors 进行验证.此代码与以上相同,但具有更新的验证方法.

You need to use woocommerce_registration_errors for validcation. This code is the same as above but with updated validation method.

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

这篇关于将帐单地址添加到 Woocommerce 注册页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆