/** * Creates a customer in the tests DB. */ public static function create_customer($username = '******', $password = '******', $email = '*****@*****.**') { $customer = new WC_Customer(); $customer->set_billing_country('US'); $customer->set_first_name('Justin'); $customer->set_billing_state('PA'); $customer->set_billing_postcode('19123'); $customer->set_billing_city('Philadelphia'); $customer->set_billing_address('123 South Street'); $customer->set_billing_address_2('Apt 1'); $customer->set_shipping_country('US'); $customer->set_shipping_state('PA'); $customer->set_shipping_postcode('19123'); $customer->set_shipping_city('Philadelphia'); $customer->set_shipping_address('123 South Street'); $customer->set_shipping_address_2('Apt 1'); $customer->set_username($username); $customer->set_password($password); $customer->set_email($email); $customer->save(); return $customer; }
/** * 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); }
/** * Store customer data to meta. * @since 2.7.0 */ protected function update_customer_data() { if ($this->customer_id) { if (apply_filters('woocommerce_checkout_update_customer_data', true, $this)) { $customer = new WC_Customer($this->customer_id); if ($keys = array_keys($this->checkout_fields['billing'])) { foreach ($keys as $key) { if (is_callable(array($customer, "set_{$key}"))) { $customer->{"set_{$key}"}($this->get_posted_address_data(str_replace(array('billing_', 'shipping_'), '', $key))); } } } if (WC()->cart->needs_shipping() && ($keys = array_keys($this->checkout_fields['shipping']))) { foreach ($keys as $key) { if (is_callable(array($customer, "set_{$key}"))) { $customer->{"set_{$key}"}($this->get_posted_address_data(str_replace(array('billing_', 'shipping_'), '', $key), 'shipping')); } } } $customer->save(); } do_action('woocommerce_checkout_update_user_meta', $this->customer_id, $this->posted); } }
/** * Test WC_Customer's session handling code. * @since 2.7.0 */ public function test_customer_sessions() { $customer = WC_Helper_Customer::create_customer(); $session = WC_Helper_Customer::create_mock_customer(); // set into session.... $this->assertEquals('19123', $session->get_billing_postcode()); $this->assertEquals('123 South Street', $session->get_billing_address()); $this->assertEquals('Philadelphia', $session->get_billing_city()); $session->set_billing_address('124 South Street'); $session->save_to_session(); $session = new WC_Customer(0, true); $session->load_session(); $this->assertEquals('124 South Street', $session->get_billing_address()); $session = new WC_Customer(0, true); $session->load_session(); $session->set_billing_postcode('32191'); $session->save(); // should still be session ID, not a created row, since we are working with guests/sessions $this->assertFalse($session->get_id() > 0); $this->assertEquals('32191', $session->get_billing_postcode()); }
/** * Create a customer * * @since 2.2 * @param array $data * @return array */ public function create_customer($data) { try { if (!isset($data['customer'])) { throw new WC_API_Exception('woocommerce_api_missing_customer_data', sprintf(__('No %1$s data specified to create %1$s', 'woocommerce'), 'customer'), 400); } $data = $data['customer']; // Checks with can create new users. if (!current_user_can('create_users')) { throw new WC_API_Exception('woocommerce_api_user_cannot_create_customer', __('You do not have permission to create this customer', 'woocommerce'), 401); } $data = apply_filters('woocommerce_api_create_customer_data', $data, $this); // Checks with the email is missing. if (!isset($data['email'])) { throw new WC_API_Exception('woocommerce_api_missing_customer_email', sprintf(__('Missing parameter %s', 'woocommerce'), 'email'), 400); } // Create customer. $customer = new WC_Customer(); $customer->set_username(!empty($data['username']) ? $data['username'] : ''); $customer->set_password(!empty($data['password']) ? $data['password'] : ''); $customer->set_email($data['email']); $customer->save(); if (!$customer->get_id()) { throw new WC_API_Exception('woocommerce_api_user_cannot_create_customer', __('This resource cannot be created.', 'woocommerce'), 400); } // Added customer data. $this->update_customer_data($customer->get_id(), $data, $customer); $customer->save(); do_action('woocommerce_api_create_customer', $customer->get_id(), $data); $this->server->send_status(201); return $this->get_customer($customer->get_id()); } catch (Exception $e) { return new WP_Error($e->getErrorCode(), $e->getMessage(), array('status' => $e->getCode())); } }
/** * Create a single customer. * * @param WP_REST_Request $request Full details about the request. * @return WP_Error|WP_REST_Response */ public function create_item($request) { try { if (!empty($request['id'])) { throw new WC_REST_Exception('woocommerce_rest_customer_exists', __('Cannot create existing resource.', 'woocommerce'), 400); } // Sets the username. $request['username'] = !empty($request['username']) ? $request['username'] : ''; // Sets the password. $request['password'] = !empty($request['password']) ? $request['password'] : ''; // Create customer. $customer = new WC_Customer(); $customer->set_username($request['username']); $customer->set_password($request['password']); $customer->set_email($request['email']); $customer->save(); if (!$customer->get_id()) { throw new WC_REST_Exception('woocommerce_rest_cannot_create', __('This resource cannot be created.', 'woocommerce'), 400); } $this->update_customer_meta_fields($customer, $request); $customer->save(); $user_data = get_userdata($customer->get_id()); $this->update_additional_fields_for_object($user_data, $request); /** * Fires after a customer is created or updated via the REST API. * * @param WP_User $user_data Data used to create the customer. * @param WP_REST_Request $request Request object. * @param boolean $creating True when creating customer, false when updating customer. */ do_action('woocommerce_rest_insert_customer', $user_data, $request, true); $request->set_param('context', 'edit'); $response = $this->prepare_item_for_response($user_data, $request); $response = rest_ensure_response($response); $response->set_status(201); $response->header('Location', rest_url(sprintf('/%s/%s/%d', $this->namespace, $this->rest_base, $customer->get_id()))); return $response; } catch (Exception $e) { return new WP_Error($e->getErrorCode(), $e->getMessage(), array('status' => $e->getCode())); } }