[英]Check programmatically if a coupon is valid for a user in WooCommerce
[英]Update WooCommerce User programmatically when creating a user
我試圖通過表單以編程方式創建一個新用戶,但是我在通過wp_create_user
獲取電話號碼和國家/地區設置時wp_create_user
-為什么不采用這些值? 名字和姓氏按預期工作。
相關代碼:
$user_id = wp_create_user( $username, $random_password, $user_email );
wp_update_user([
'ID' => $user_id,
'first_name' => rgar( $entry, '20.3' ),
'last_name' => rgar( $entry, '20.6' ),
'phone' => rgar( $entry, '16' ),
'country' => rgar( $entry, '24.6' )
]);
在 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
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 );
您可以將country
和phone
添加為用戶元並將其保存在用戶元表中:
add_user_meta( $user_id, 'country', rgar( $entry, '24.6'));
add_user_meta( $user_id, 'phone', rgar( $entry, '16'));
WordPress Codex: 點擊這里
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.