2016-07-11 3 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

Я думаю, что проблема заключается в том, что $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 

Ссылка:

0

Вы можете использовать

wp_update_user ($ USERDATA)

где $ UserData являются список полей.

Использование wp_update_user вы можете обновить поле

Смежные вопросы