/** * Create a new customer account if needed. * @param array $data * @throws Exception */ protected function process_customer($data) { $customer_id = get_current_user_id(); if (!is_user_logged_in() && ($this->is_registration_required() || !empty($data['createaccount']))) { $username = !empty($data['account_username']) ? $data['account_username'] : ''; $password = !empty($data['account_password']) ? $data['account_password'] : ''; $customer_id = wc_create_new_customer($data['billing_email'], $username, $password); if (is_wp_error($customer_id)) { throw new Exception($customer_id->get_error_message()); } wp_set_current_user($customer_id); wc_set_customer_auth_cookie($customer_id); // As we are now logged in, checkout will need to refresh to show logged in data WC()->session->set('reload_checkout', true); // Also, recalculate cart totals to reveal any role-based discounts that were unavailable before registering WC()->cart->calculate_totals(); } // Add customer info from other fields. if ($customer_id && apply_filters('woocommerce_checkout_update_customer_data', true, $this)) { $customer = new WC_Customer($customer_id); $customer->set_first_name($data['billing_first_name']); $customer->set_last_name($data['billing_last_name']); foreach ($data as $key => $value) { if (is_callable(array($customer, "set_{$key}"))) { $customer->{"set_{$key}"}($value); } } $customer->save(); } do_action('woocommerce_checkout_update_user_meta', $customer_id, $data); }
/** * Add/Update customer data. * * @since 2.2 * @param int $id the customer ID * @param array $data * @param WC_Customer $customer */ protected function update_customer_data($id, $data, $customer) { // Customer first name. if (isset($data['first_name'])) { $customer->set_first_name(wc_clean($data['first_name'])); } // Customer last name. if (isset($data['last_name'])) { $customer->set_last_name(wc_clean($data['last_name'])); } // Customer billing address. if (isset($data['billing_address'])) { foreach ($this->get_customer_billing_address() as $field) { if (isset($data['billing_address'][$field])) { if (is_callable(array($customer, "set_billing_{$field}"))) { $customer->{"set_billing_{$field}"}($data['billing_address'][$field]); } else { $customer->update_meta_data('billing_' . $field, wc_clean($data['billing_address'][$field]), $meta['id']); } } } } // Customer shipping address. if (isset($data['shipping_address'])) { foreach ($this->get_customer_shipping_address() as $field) { if (isset($data['shipping_address'][$field])) { if (is_callable(array($customer, "set_shipping_{$field}"))) { $customer->{"set_shipping_{$field}"}($data['shipping_address'][$field]); } else { $customer->update_meta_data('shipping_' . $field, wc_clean($data['shipping_address'][$field]), $meta['id']); } } } } do_action('woocommerce_api_update_customer_data', $id, $data, $customer); }
/** * Test reading a customer. * @since 2.7.0 */ public function test_read_customer() { $username = '******' . time(); $customer = new WC_Customer(); $customer->set_username($username); $customer->set_email('*****@*****.**'); $customer->set_password('hunter2'); $customer->set_first_name('Bob'); $customer->set_last_name('Bob'); $customer->create(); $customer_id = $customer->get_id(); $customer_read = new WC_Customer(); $customer_read->read($customer_id); $this->assertEquals($customer_id, $customer_read->get_id()); $this->assertEquals('*****@*****.**', $customer_read->get_email()); $this->assertEquals('Bob', $customer_read->get_first_name()); $this->assertEquals('Bob', $customer_read->get_last_name()); $this->assertEquals($username, $customer_read->get_username()); }
/** * Update customer meta fields. * * @param WC_Customer $customer * @param WP_REST_Request $request */ protected function update_customer_meta_fields($customer, $request) { $schema = $this->get_item_schema(); // Meta data if (isset($request['meta_data'])) { if (is_array($request['meta_data'])) { foreach ($request['meta_data'] as $meta) { $coupon->update_meta_data($meta['key'], $meta['value'], $meta['id']); } } } // Customer first name. if (isset($request['first_name'])) { $customer->set_first_name(wc_clean($request['first_name'])); } // Customer last name. if (isset($request['last_name'])) { $customer->set_last_name(wc_clean($request['last_name'])); } // Customer billing address. if (isset($request['billing'])) { foreach (array_keys($schema['properties']['billing']['properties']) as $field) { if (isset($request['billing'][$field]) && is_callable(array($customer, "set_billing_{$field}"))) { $customer->{"set_billing_{$field}"}($request['billing'][$field]); } } } // Customer shipping address. if (isset($request['shipping'])) { foreach (array_keys($schema['properties']['shipping']['properties']) as $field) { if (isset($request['shipping'][$field]) && is_callable(array($customer, "set_shipping_{$field}"))) { $customer->{"set_shipping_{$field}"}($request['shipping'][$field]); } } } }