我试图向当前客户添加送货地址,但我没有工作。
这是我的代码:
$userdata = array(
'user_login' => $email,
'user_email' => $email,
'user_pass' => $password,
'first_name' => $first_name,
'last_name' => $last_name
);
$user = wp_insert_user( $userdata ); // User successfully inserted
$customer = new WC_Customer();
$customer->set_shipping_city("Bangalore"); //Doesn't work
global $woocommerce;
$woocommerce->customer->set_shipping_city("Bangalore"); //Doesn't work
我做错了什么?
答案 0 :(得分:1)
我认为问题在于$user = wp_insert_user( $userdata );
没有插入任何数据,只是将其存储到$user
变量中,然后在您的代码中就不再使用它了。
然后set_shipping_city();
也是会话数据功能,您需要使用现有的客户或购物车会话(WC_cart)。
所以我稍微更改了你的代码:
$userdata = array (
'user_login' => $email,
'user_email' => $email,
'user_pass' => $password,
'first_name' => $first_name,
'last_name' => $last_name
) ;
$user_id = wp_insert_user( $userdata ) ;
// Here you insert your user data with a 'customer' user role
wp_update_user( array ('ID' => $user_id, 'role' => 'customer') ) ;
//Once customer user is inserted/created, you can retrieve his ID
global $current_user;
$user = wp_get_current_user();
$user_id = $user->ID;
// or use instead: $user_id = get_current_user_id();
// Checking if user Id exist
if( !empty( $user_id ) ) {
// You will need also to add this billing meta data
update_user_meta( $user_id, 'billing_first_name', $first_name );
update_user_meta( $user_id, 'billing_last_name', $last_name );
update_user_meta( $user_id, 'billing_email', $email );
// optionally shipping meta data, that could be different
update_user_meta( $user_id, 'shipping_first_name', $first_name );
update_user_meta( $user_id, 'shipping_last_name', $last_name );
update_user_meta( $user_id, 'shipping_email', $email );
// Now you can insert the required billing and shipping city
update_user_meta( $user_id, 'billing_city', 'Bangalore' );
// optionally shipping_city can be different…
update_user_meta( $user_id, 'shipping_city', 'Bangalore' );
} else {
// echo 'User Id doesn't exist';
}
然后,您可以使用update_user_meta()
功能通过 keys
插入任何地址数据:
billing_first_name
billing_last_name
billing_company
billing_email
billing_phone
billing_address_1
billing_address_2
billing_country
billing_state
billing_postcode
shipping_first_name
shipping_last_name
shipping_company
shipping_email
shipping_phone
shipping_address_1
shipping_address_2
shipping_country
shipping_state
shipping_postcode
参考:
答案 1 :(得分:0)
您可以使用
wp_update_user($ userdata)
其中$ userdata是字段列表。
使用wp_update_user可以更新字段