diff --git a/backend/app/plugins/wiaas/includes/class-wiaas-user.php b/backend/app/plugins/wiaas/includes/class-wiaas-user.php index ff9fb7d..5bda54c 100644 --- a/backend/app/plugins/wiaas/includes/class-wiaas-user.php +++ b/backend/app/plugins/wiaas/includes/class-wiaas-user.php @@ -10,10 +10,33 @@ class Wiaas_User { public static function init() { add_action('init', array(__CLASS__, 'load_user_organization')); add_action('plugins_loaded', array(__CLASS__, 'remove_default_user_groups'), 30); + add_action('woocommerce_rest_insert_customer', array(__CLASS__, 'test_method'), 10, 3); + add_filter('woocommerce_rest_prepare_customer', array(__CLASS__, 'transform_rest_customer'), 10, 3); add_filter('jwt_auth_token_before_dispatch', array(__CLASS__, 'transform_jwt_token_response'), 10, 2); } + /** + * 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. + */ + public static function test_method($customer, $request, $creating){ + if (isset($request['phone'])){ + update_user_meta( $customer->ID, 'phone', $request['phone'] ); + } + + if (isset($request['vatCode'])){ + update_user_meta( $customer->ID, 'vat_code', $request['vatCode']); + } + + if (isset($request['companyName'])){ + update_user_meta( $customer->ID, 'company_name', $request['companyName']); + } + } + public static function load_user_organization() { if (class_exists('WP_User_Taxonomy')) { require_once dirname( __FILE__ ) . '/user/class-wiaas-user-organization.php'; @@ -38,18 +61,19 @@ class Wiaas_User { */ public static function transform_rest_customer($response, $order, $request) { $data = $response->get_data(); + $user_id = $data['id']; $result = array( - 'id' => $data['id'], + 'id' => $user_id, 'idCompany' => 0, 'isCompanyAdmin' => 1, 'mail' => $data['email'], - 'phone' => $phone, 'name' => $data['first_name'] . ' ' . $data['last_name'], - 'companyName' => '', + 'phone' => get_user_meta($user_id, 'phone', true), + 'companyName' => get_user_meta($user_id, 'company_name', true), + 'vatCode' => get_user_meta($user_id, 'vat_code', true), 'billingAddresses' => [], 'profileAddresses' => [], - 'userType' => $data['role'], - 'vatCode' => 0, + 'userType' => $data['role'] ); return new WP_REST_Response($result); diff --git a/frontend/src/actions/profileSettings/profileSettingsActions.js b/frontend/src/actions/profileSettings/profileSettingsActions.js index ecfdbd8..58018d9 100644 --- a/frontend/src/actions/profileSettings/profileSettingsActions.js +++ b/frontend/src/actions/profileSettings/profileSettingsActions.js @@ -68,6 +68,7 @@ export const saveProfileInfo = (idUser, profile) => { data: { 'first_name': parsedFullName[0], 'last_name': parsedFullName[1], + 'phone': profile.phone } }) .then(response => { @@ -90,9 +91,12 @@ export const saveCompanyInfo = (idUser, companyInfo) => { return dispatch => { dispatch(requestSaveCompany()); return client.fetch({ - url: `${API_SERVER}/profileSettings/api/saveCompanyInfo`, - method: 'post', - data: {companyInfo: JSON.stringify(companyInfo)} + url: `${API_SERVER}/wp-json/wc/v2/customers/${idUser}`, + method: 'put', + data: { + vatCode: companyInfo.vatCode, + companyName: companyInfo.companyName + } }) .then(response => { if(response.data && response.data.messages){