我已将以下代码添加到Woocommerce用户注册表中,以在注册页面上获取结算明细 .
现在当新用户注册时发生了什么, the first and last name will get registered in the database of billing details & as well as in the default wordpress user account . 如果用户在其帐户(wordpress用户帐户)上更新了他的名字和姓氏,则应更新帐单明细 .
Woocommerce设置:
访客结账被禁用 . 检查页面用户注册已启用 . 登录页面注册已启用 . 只有注册用户才能进行购买 .
- 这是用户注册表单,我在结账过程中提取这些额外的结算明细 .
- On "Account Details"我更新了"First Name",它在这里工作但我没有在"Billing Details"上获得相同的更新 . 如果用户在他的"Account Details"上更新这两个字段及其电子邮件地址,我希望在"Billing Details"上进行相同的"First Name"&"Last Name"更新 .
- 现在在"Account Details"上更新了"First Name"&"Last Name"之后,我又回到了"Billing Details"但它仍然显示了在注册过程中使用的旧"First Name"和"Last Name" . 此外,我想在结算明细,"First Name","Last Name"&"Email Address"中隐藏这三个字段,以免混淆注册用户 . 我只需要在数据库中的"Billing Details"上进行这些更新,因为这些信息将打印在发票和电子邮件中
如果管理员或商店经理转到用户配置文件(从后端)并手动按下“更新”按钮,那么数据将仅同步/更新,然后才会生效 . 当注册用户从他的帐户(前端)进行任何更改时,我希望数据自动同步/更新 .
任何帮助将受到高度赞赏 .
请检查以下代码:
// 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'] ) );
}
}
1 回答
我稍微重新审视了你的代码,例如最后4个函数可以合并为一个,其他的东西......
Note: woocommerce_billing_fields 中挂钩的函数将在您的其他注册字段和结帐字段中启用,因为您使用checkout对象生成其他注册字段...您可以使用条件
! is_checkout()
仅定位我的帐户注册字段 .这是您重新访问的代码:
客户无法访问(从不)WordPress后端用户编辑页面 . 只有店铺经理和管理员才能做到......
要在后端同步Wordpress用户数据,您需要选择具有优先级的字段:
Wordpress默认字段(或)
结算字段(来自WooCommerce) .
最好优先使用WordPress默认字段并隐藏必要的结算字段...
此代码将隐藏3个帐单字段(名字,姓氏和电子邮件),并将其与默认字段更新值同步:
代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中 .
经过测试和工作......