2016-07-11 10 views
1

配送先住所を現在の顧客に追加しようとしていますが、動作しません。ここでWordPressプラグイン - WooCommerceでの配送先住所

は私のコードです:

$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 

私が間違っているのか?

答えて

1

を更新することができますwp_update_user使用私はこの問題は、$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'; 
} 

次にあなたがkeysで任意のアドレスのデータを挿入するupdate_user_meta()機能を使用することができます。

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 

参考:

0

あなたは

wp_update_user($のユーザデータ)$のユーザデータは、フィールドのリストです

を使用することができます。

あなたがフィールドに

関連する問題