![](/img/trans.png)
[英]Add custom WooCommerce registration fields to admin WordPress user contact fields
[英]Sync additional Billing registration fields with default Wordpress fields in WooCommerce
我已將以下代碼添加到Woocommerce用戶注冊表單中,以在注冊頁面上獲得“帳單詳細信息”。
現在,當新用戶注冊時,正在發生的事情, 名字和姓氏將在賬單明細數據庫以及默認的wordpress用戶帳戶中注冊 。 如果用戶在其帳戶(wordpress用戶帳戶)上更新了他的名字和姓氏,則應在賬單明細上進行更新。
Woocommerce設置:
訪客結帳已禁用。 結帳頁面用戶注冊已啟用。 登錄頁面注冊已啟用。 只有注冊用戶可以購買。
僅當管理員或商店經理轉到用戶配置文件(從后端)並手動按下“更新”按鈕時,數據才會同步/更新,然后才會生效。 當注冊用戶對其帳戶(前端)進行任何更改時,我希望數據自動同步/更新。
任何幫助將不勝感激。
請檢查以下代碼:
// Custom function to display the Billing Address form to registration page
add_action('woocommerce_register_form_start','zk_add_billing_form_to_registration');
function zk_add_billing_form_to_registration(){
$checkout = WC()->checkout;
foreach ( $checkout->get_checkout_fields( 'billing' ) as $key => $field ) :
if($key!='billing_email')
woocommerce_form_field( $key, $field, $checkout->get_value( $key ) );
endforeach;
}
// Custom function to save Usermeta or Billing Address of registered user
add_action('woocommerce_created_customer','zk_save_billing_address');
function zk_save_billing_address($user_id){
$address = $_POST;
foreach ($address as $key => $field){
// Only billing fields values
if( strpos( $key, 'billing_' ) !== false ){
// Condition to add firstname and last name to user meta table
if($key == 'billing_first_name' || $key == 'billing_last_name'){
$new_key = str_replace( 'billing_', '', $key );
update_user_meta( $user_id, $new_key, $_POST[$key] );
}
update_user_meta( $user_id, $key, $_POST[$key] );
}
}
}
// Checking & validation of the additional fields in registration form.
add_action('woocommerce_register_post','zk_validation_billing_address', 10, 3 );
function zk_validation_billing_address( $username, $email, $validation_errors ){
foreach ($_POST as $key => $field) :
// Validation: Required fields
if( strpos( $key, 'billing_' ) !== false ){
if($key == 'billing_country' && empty($field) ){
$validation_errors->add( $key.'_error', __( 'Please select a country.', 'woocommerce' ));
}
if($key == 'billing_first_name' && empty($field) ){
$validation_errors->add( $key.'_error', __( 'Please enter first name.', 'woocommerce' ) );
}
if($key == 'billing_last_name' && empty($field) ){
$validation_errors->add( $key.'_error', __( 'Please enter last name.', 'woocommerce' ) );
}
if($key == 'billing_address_1' && empty($field) ){
$validation_errors->add( $key.'_error', __( 'Please enter address.', 'woocommerce' ) );
}
if($key == 'billing_city' && empty($field) ){
$validation_errors->add( $key.'_error', __( 'Please enter city.', 'woocommerce' ) );
}
if($key == 'billing_state' && empty($field) ){
if(count( WC()->countries->get_states($_POST['billing_country']) ) > 0)
$validation_errors->add( $key.'_error', __( 'Please enter state.', 'woocommerce' ) );
}
if($key == 'billing_postcode' && empty($field) ){
$validation_errors->add( $key.'_error', __( 'Please enter a postcode.', 'woocommerce' ) );
}
/*
if($key == 'billing_email' && empty($field) ){
$validation_errors->add( $key.'_error', __( 'Please enter billing email address.', 'woocommerce' ) );
}
*/
if($key == 'billing_phone' && empty($field) ){
$validation_errors->add( $key.'_error', __( 'Please enter phone number.', 'woocommerce' ) );
}
}
endforeach;
}
add_filter( 'woocommerce_billing_fields', 'sv_required_billing_fields' );
function sv_required_billing_fields( $fields ) {
$fields['billing_phone']['required'] = true;
$fields['billing_city']['required'] = true;
$fields['billing_country']['required'] = true;
$fields['billing_address_1']['required'] = true;
return $fields;
}
// Hidding some billing fields (Wordpress edit user pages)
add_action( 'edit_user_profile', 'user_profile_hide_some_fields_css', 1, 1 );
function user_profile_hide_some_fields_css( $user ){
?>
<style>
.user-edit-php table#fieldset-billing tr:first-child,
.user-edit-php table#fieldset-billing tr:nth-child(2),
.user-edit-php table#fieldset-billing tr:last-child {
display:none;
}
</style>
<?php
}
// Sync hidden billing fields (Wordpress edit user pages)
add_action( 'personal_options_update', 'sync_user_data_wp_and_billing_wc', 100, 1 );
add_action( 'edit_user_profile_update', 'sync_user_data_wp_and_billing_wc', 100, 1 );
function sync_user_data_wp_and_billing_wc( $user_id )
{
if( ! empty($_POST['first_name']) ) {
update_user_meta( $user_id, 'billing_first_name', sanitize_text_field( $_POST['first_name'] ) );
}
if( ! empty($_POST['last_name']) ) {
update_user_meta( $user_id, 'billing_last_name', sanitize_text_field( $_POST['last_name'] ) );
}
if( ! empty($_POST['email']) ) {
update_user_meta( $user_id, 'billing_email', sanitize_text_field( $_POST['email'] ), sanitize_text_field( $_POST['billing_email'] ) );
}
}
我稍微回顧了一下您的代碼,例如,可以將最后四個功能合並為一個,以及其他功能……
數據更新和同步
現在,當客戶在其“我的帳戶”頁面上更新其數據時,所有數據都會由woocommerce同步到各地, 除了他現有的過往訂單上 ……
如果客戶更改結帳字段和流程結帳,數據也會在各處更新…
因此, 您無需擔心客戶同步數據。
注意:鈎在woocommerce_billing_fields
的函數將在您的其他注冊字段和結帳字段中啟用,因為您正在使用checkout對象生成其他注冊字段… 您可以使用條件! is_checkout()
! is_checkout()
僅定位到我的帳戶注冊字段。
這是您重新訪問的代碼:
// Custom function to display the Billing Address form to registration page
add_action('woocommerce_register_form_start','zk_add_billing_form_to_registration');
function zk_add_billing_form_to_registration(){
$checkout = WC()->checkout;
foreach ( $checkout->get_checkout_fields( 'billing' ) as $key => $field ) :
if($key!='billing_email')
woocommerce_form_field( $key, $field, $checkout->get_value( $key ) );
endforeach;
}
// Custom function to save Usermeta or Billing Address of registered user
add_action('woocommerce_created_customer','zk_save_billing_address');
function zk_save_billing_address($user_id){
$address = $_POST;
foreach ($address as $key => $field){
// Only billing fields values
if( strpos( $key, 'billing_' ) !== false ){
// Condition to add firstname and last name to user meta table
if($key == 'billing_first_name' || $key == 'billing_last_name'){
$new_key = str_replace( 'billing_', '', $key );
update_user_meta( $user_id, $new_key, $_POST[$key] );
}
update_user_meta( $user_id, $key, $_POST[$key] );
}
}
}
// Checking & validation of the additional fields in registration form.
add_action('woocommerce_register_post','zk_validation_billing_address', 10, 3 );
function zk_validation_billing_address( $username, $email, $validation_errors ){
foreach ($_POST as $key => $field) :
// Validation: Required fields
if( strpos( $key, 'billing_' ) !== false ){
if($key == 'billing_country' && empty($field) ){
$validation_errors->add( $key.'_error', __( 'Please select a country.', 'woocommerce' ));
}
if($key == 'billing_first_name' && empty($field) ){
$validation_errors->add( $key.'_error', __( 'Please enter first name.', 'woocommerce' ) );
}
if($key == 'billing_last_name' && empty($field) ){
$validation_errors->add( $key.'_error', __( 'Please enter last name.', 'woocommerce' ) );
}
if($key == 'billing_address_1' && empty($field) ){
$validation_errors->add( $key.'_error', __( 'Please enter address.', 'woocommerce' ) );
}
if($key == 'billing_city' && empty($field) ){
$validation_errors->add( $key.'_error', __( 'Please enter city.', 'woocommerce' ) );
}
if($key == 'billing_state' && empty($field) ){
if(count( WC()->countries->get_states($_POST['billing_country']) ) > 0)
$validation_errors->add( $key.'_error', __( 'Please enter state.', 'woocommerce' ) );
}
if($key == 'billing_postcode' && empty($field) ){
$validation_errors->add( $key.'_error', __( 'Please enter a postcode.', 'woocommerce' ) );
}
/*
if($key == 'billing_email' && empty($field) ){
$validation_errors->add( $key.'_error', __( 'Please enter billing email address.', 'woocommerce' ) );
}
*/
if($key == 'billing_phone' && empty($field) ){
$validation_errors->add( $key.'_error', __( 'Please enter phone number.', 'woocommerce' ) );
}
}
endforeach;
}
add_filter( 'woocommerce_billing_fields', 'sv_required_billing_fields' );
function sv_required_billing_fields( $fields ) {
$fields['billing_phone']['required'] = true;
$fields['billing_city']['required'] = true;
$fields['billing_country']['required'] = true;
$fields['billing_address_1']['required'] = true;
return $fields;
}
客戶無法(永遠)無法訪問WordPress后端用戶編輯頁面。 只有店鋪經理和管理員才能執行此操作…
要在后端同步Wordpress用戶數據,您需要選擇優先使用的字段:
最好優先考慮WordPress默認字段並隱藏必要的帳單字段…
此代碼將隱藏3個帳單字段(名字,姓氏和電子郵件),並將它們與默認字段的更新值同步:
// Hidding some billing fields (Wordpress edit user pages)
add_action( 'edit_user_profile', 'user_profile_hide_some_fields_css', 1, 1 );
function user_profile_hide_some_fields_css( $user ){
?>
<style>
.user-edit-php table#fieldset-billing tr:first-child,
.user-edit-php table#fieldset-billing tr:nth-child(2),
.user-edit-php table#fieldset-billing tr:last-child {
display:none;
}
</style>
<?php
}
// Sync hidden billing fields (Wordpress edit user pages)
add_action( 'personal_options_update', 'sync_user_data_wp_and_billing_wc', 100, 1 );
add_action( 'edit_user_profile_update', 'sync_user_data_wp_and_billing_wc', 100, 1 );
function sync_user_data_wp_and_billing_wc( $user_id )
{
if( ! empty($_POST['first_name']) ) {
update_user_meta( $user_id, 'billing_first_name', sanitize_text_field( $_POST['first_name'] ) );
}
if( ! empty($_POST['last_name']) ) {
update_user_meta( $user_id, 'billing_last_name', sanitize_text_field( $_POST['last_name'] ) );
}
if( ! empty($_POST['email']) ) {
update_user_meta( $user_id, 'billing_email', sanitize_text_field( $_POST['email'] ), sanitize_text_field( $_POST['billing_email'] ) );
}
}
代碼在您的活動子主題(或主題)的function.php文件中,或者在任何插件文件中。
經過測試並可以工作…
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.