在 WooCommerce 中,电话和国家是计费字段,因此正确的用户元键是:
-
billing_country(记住你需要设置一个有效的国家代码)
billing_phone
您还需要设置billing_email、billing_first_name 和billing_last_name
因此,您的代码将改为将您的 wp_create_user() 函数替换为:
$username = rgar( $entry, '20.3' );
$email = rgar( $entry, '10' );
$password = wp_generate_password( 12, false );
$user_data = array(
'user_login' => $username,
'user_pass' => $password,
'user_email' => $email,
'role' => 'customer',
'first_name' => rgar( $entry, '20.3' ),
'last_name' => rgar( $entry, '20.6' ),
);
$user_id = wp_insert_user( $user_data ); // Create user with specific user data
那么添加 WooCommerce 用户数据有两种方式:
1)。使用WC_Customer 对象和方法:
$customer = new WC_Customer( $user_id ); // Get an instance of the WC_Customer Object from user Id
$customer->set_billing_first_name( rgar( $entry, '20.3' ) );
$customer->set_billing_last_name( rgar( $entry, '20.6' ) );
$customer->set_billing_country( rgar( $entry, '24.6') );
$customer->set_billing_phone( rgar( $entry, '16' ) );
$customer->set_billing_email( $email );
$customer->save(); // Save data to database (add the user meta data)
2) 或者使用WordPress update_user_meta()函数 (老办法):
update_user_meta( $user_id, 'billing_first_name', rgar( $entry, '20.3') );
update_user_meta( $user_id, 'billing_last_name', rgar( $entry, '20.6') );
update_user_meta( $user_id, 'billing_country', rgar( $entry, '24.6') );
update_user_meta( $user_id, 'billing_phone', rgar( $entry, '16') );
update_user_meta( $user_id, 'billing_email', $email );