/** * 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())); } }
/** * Search for customers and return json. */ public static function json_search_customers() { ob_start(); check_ajax_referer('search-customers', 'security'); if (!current_user_can('edit_shop_orders')) { die(-1); } $term = wc_clean(stripslashes($_GET['term'])); $exclude = array(); if (empty($term)) { die; } $data_store = WC_Data_Store::load('customer'); $ids = $data_store->search_customers($term); $found_customers = array(); if (!empty($_GET['exclude'])) { $ids = array_diff($ids, (array) $_GET['exclude']); } foreach ($ids as $id) { $customer = new WC_Customer($id); /* translators: 1: user display name 2: user ID 3: user email */ $found_customers[$id] = sprintf(esc_html__('%1$s (#%2$s – %3$s)', 'woocommerce'), $customer->get_first_name() . ' ' . $customer->get_last_name(), $customer->get_id(), $customer->get_email()); } wp_send_json(apply_filters('woocommerce_json_search_found_customers', $found_customers)); }
/** * Update a single user. * * @param WP_REST_Request $request Full details about the request. * @return WP_Error|WP_REST_Response */ public function update_item($request) { try { $id = (int) $request['id']; $customer = new WC_Customer($id); if (!$customer->get_id()) { throw new WC_REST_Exception('woocommerce_rest_invalid_id', __('Invalid resource ID.', 'woocommerce'), 400); } if (!empty($request['email']) && email_exists($request['email']) && $request['email'] !== $customer->get_email()) { throw new WC_REST_Exception('woocommerce_rest_customer_invalid_email', __('Email address is invalid.', 'woocommerce'), 400); } if (!empty($request['username']) && $request['username'] !== $customer->get_username()) { throw new WC_REST_Exception('woocommerce_rest_customer_invalid_argument', __("Username isn't editable.", 'woocommerce'), 400); } // Customer email. if (isset($request['email'])) { $customer->set_email(sanitize_email($request['email'])); } // Customer password. if (isset($request['password'])) { $customer->set_password(wc_clean($request['password'])); } $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 $customer 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, false); $request->set_param('context', 'edit'); $response = $this->prepare_item_for_response($user_data, $request); $response = rest_ensure_response($response); return $response; } catch (Exception $e) { return new WP_Error($e->getErrorCode(), $e->getMessage(), array('status' => $e->getCode())); } }
/** * Prepare a single customer output for response. * * @param WP_User $user_data User object. * @param WP_REST_Request $request Request object. * @return WP_REST_Response $response Response data. */ public function prepare_item_for_response($user_data, $request) { $customer = new WC_Customer($user_data->ID); $last_order_data = $customer->get_last_order(); $last_order = null; if ($last_order_data) { $last_order = array('id' => $last_order_data->get_id(), 'date' => wc_rest_prepare_date_response($last_order_data->get_date_created())); } $data = array('id' => $customer->get_id(), 'date_created' => wc_rest_prepare_date_response(date('Y-m-d H:i:s', $customer->get_date_created())), 'date_modified' => $customer->get_date_modified() ? wc_rest_prepare_date_response(date('Y-m-d H:i:s', $customer->get_date_modified())) : null, 'email' => $customer->get_email(), 'first_name' => $customer->get_first_name(), 'last_name' => $customer->get_last_name(), 'username' => $customer->get_username(), 'last_order' => $last_order, 'orders_count' => $customer->get_order_count(), 'total_spent' => $customer->get_total_spent(), 'avatar_url' => $customer->get_avatar_url(), 'billing' => array('first_name' => $customer->get_billing_first_name(), 'last_name' => $customer->get_billing_last_name(), 'company' => $customer->get_billing_company(), 'address_1' => $customer->get_billing_address_1(), 'address_2' => $customer->get_billing_address_2(), 'city' => $customer->get_billing_city(), 'state' => $customer->get_billing_state(), 'postcode' => $customer->get_billing_postcode(), 'country' => $customer->get_billing_country(), 'email' => $customer->get_billing_email(), 'phone' => $customer->get_billing_phone()), 'shipping' => array('first_name' => $customer->get_shipping_first_name(), 'last_name' => $customer->get_shipping_last_name(), 'company' => $customer->get_shipping_company(), 'address_1' => $customer->get_shipping_address_1(), 'address_2' => $customer->get_shipping_address_2(), 'city' => $customer->get_shipping_city(), 'state' => $customer->get_shipping_state(), 'postcode' => $customer->get_shipping_postcode(), 'country' => $customer->get_shipping_country())); $context = !empty($request['context']) ? $request['context'] : 'view'; $data = $this->add_additional_fields_to_object($data, $request); $data = $this->filter_response_by_context($data, $context); // Wrap the data in a response object. $response = rest_ensure_response($data); $response->add_links($this->prepare_links($user_data)); /** * Filter customer data returned from the REST API. * * @param WP_REST_Response $response The response object. * @param WP_User $user_data User object used to create response. * @param WP_REST_Request $request Request object. */ return apply_filters('woocommerce_rest_prepare_customer', $response, $user_data, $request); }
/** * Get the customer for the given ID * * @since 2.1 * @param int $id the customer ID * @param string $fields * @return array */ public function get_customer($id, $fields = null) { global $wpdb; $id = $this->validate_request($id, 'customer', 'read'); if (is_wp_error($id)) { return $id; } $customer = new WC_Customer($id); $last_order = $customer->get_last_order(); $customer_data = array('id' => $customer->get_id(), 'created_at' => $this->server->format_datetime($customer->get_date_created(), false, true), 'email' => $customer->get_email(), 'first_name' => $customer->get_first_name(), 'last_name' => $customer->get_last_name(), 'username' => $customer->get_username(), 'last_order_id' => is_object($last_order) ? $last_order->get_id() : null, 'last_order_date' => is_object($last_order) ? $this->server->format_datetime($last_order->get_date_created(), false, true) : null, 'orders_count' => $customer->get_order_count(), 'total_spent' => wc_format_decimal($customer->get_total_spent(), 2), 'avatar_url' => $customer->get_avatar_url(), 'billing_address' => array('first_name' => $customer->get_billing_first_name(), 'last_name' => $customer->get_billing_last_name(), 'company' => $customer->get_billing_company(), 'address_1' => $customer->get_billing_address_1(), 'address_2' => $customer->get_billing_address_2(), 'city' => $customer->get_billing_city(), 'state' => $customer->get_billing_state(), 'postcode' => $customer->get_billing_postcode(), 'country' => $customer->get_billing_country(), 'email' => $customer->get_billing_email(), 'phone' => $customer->get_billing_phone()), 'shipping_address' => array('first_name' => $customer->get_shipping_first_name(), 'last_name' => $customer->get_shipping_last_name(), 'company' => $customer->get_shipping_company(), 'address_1' => $customer->get_shipping_address_1(), 'address_2' => $customer->get_shipping_address_2(), 'city' => $customer->get_shipping_city(), 'state' => $customer->get_shipping_state(), 'postcode' => $customer->get_shipping_postcode(), 'country' => $customer->get_shipping_country())); return array('customer' => apply_filters('woocommerce_api_customer_response', $customer_data, $customer, $fields, $this->server)); }