Multi User Magento 2 - Adding and Modifying Users (restAPI)
Adding User accounts using the ReST API
It is possible for developers to use the ReST API to allow the creation, editing and deletion of the user account from external sources without the need to have login credentials as either a User or as an Administrator on the site.
To do this the developer will need session tokens for authentication. The documentation for the initial setup of the ReST API can be found by following this link.
Installing and Using API
If you have not yet installed the API, click here to find instructions to do so.
If you do not have experience with using API, the Postman team has a free tutorial: Postman Course.
Affecting Master (Parent) account data
The following four methods are used for working with Parent accounts via the API:
- Get Customer Data - [ method GET ]
- path: /rest/V1/parentaccount/{ID}
ID - parent customer id. All available customer information will be returned, including address information
- path: /rest/V1/parentaccount/{ID}
- Create Parent Customer Account - [ method POST ]
- path: /rest/V1/parentaccount/
Example if input JSON of the method listed below. The method will create a parent account regarding the passed data.
- path: /rest/V1/parentaccount/
- Update Parent Customer Account By Id - [ method PUT ]
- path: /rest/V1/parentaccount/{ID}
ID - parent customer id. Sample body of request listed below.
note: in PUT you can omit every parameter you don’t want to change .
- path: /rest/V1/parentaccount/{ID}
- Delete Parent Customer Account By Id- [ method DELETE ]
- path: /rest/V1/parentaccount/{ID}
ID - parent customer ID. Customer will certain ID will be
- path: /rest/V1/parentaccount/{ID}
For POST / PUT the body of the API call should look like this:
"firstname": "Account",
"lastname": "LastName",
"email": "team@cminds.com",
"password": "Password123",
"id": "5",
"website_id": "1",
"group_id": "1",
"prefix": null,
"middlename": null,
"suffix": null,
"dob": "1973-03-19",
"taxvat": null,
"gender": "1",
"is_active": "1",
"can_manage_subaccounts": "1",
"supplier_approve": "",
"company": "",
"city": "Calder",
"country_id": "US",
"region": "33",
"postcode": "49628-7978",
"telephone": "(555) 229-3320",
"fax": "",
"vat_id": "",
"street_1": "6146 Honey Bluff Parkway",
"street_2": ""
}
Parameters details and options
- firstname - The first name of the user of the account being created/modified.
- lastname - The last name of the user of the account being created/modified.
- email - The email address for the account.
- password - The initial password to be assigned to the sub-account being created.
- id - The ID number of the user for cases of getting account information, is ignored for creating and modifying
- website_id - This is the ID of the website. To obtain this go to System → Manage Stores. Then hover over 'Main Store'.
- group_id - The ID of the group to which the account belongs.
- prefix - e.g. Mrs, Ms., Mr., etc
- middlename - The middle name of the user
- suffix - The suffix of the user
- dob - The date of birth of the user being created
- taxvat - The VAT number for the User account if applicable
- gender - 0 = undefined; 1 = male; 2 = female
- is_active - 0 = inactive; 1 = active
- can_manage_subaccounts - 0 = cannot; 1 = can
- company - The company associated with the user account
- city - The city in which the user is registering or lives
- country_id - The country code for the country in which the user account is registered
- region - The region in which the user is registered
- postcode - The postcode for the account address
- telephone - The telephone number of the user
- fax - The fax number of the user
- vat_id - The Vat ID of the user
- street_1 - The first line of the street address of the user
- street_2 - The second line of the street address of the user
NOTES:
The above is a list of available customer attributes for use with the API and some corresponding parameters or examples of parameters.
For instances of Parent user account details being modified the API call must include the parent id then the body of the call needs only contain the attribute - parameters that are to be modified.
e.g. to change a user's name and their city you'd send the following call to /rest/V1/parentaccount/{id of parent}
"firstname": "Example",
"lastname": "person",
"city": "Dublin"
}
A GET response for the customer with this id with updated firstname, lastname and city attributes are then returned.
Converting Parent Account into Sub-user Account
- Update Parent Customer Account By Id - [ method PUT ]
- /rest/V1/parentaccount/{Parent ID}/subaccounts/{Sub-user ID}
When the Parent account is converted to a Sub-user account, they will need to be assigned a parent/master account.
The {Parent ID} above is the Customer ID of the new parent/master account for the converted parent account.
The {Sub-user ID} is the customer ID of the parent being converted to a sub-user
The "parent_email": "demo@cminds.com" is the email address of the new parent account.
"parent_email": "demo@email.com"
}
Affecting Subaccounts using the API
Working with Subaccounts using the API is similar to working with Masteraccounts with some slight differences. The API calls must include the Parent ID.
The following four methods are used for working with Subaccounts via the API:
- Get All Customer Sub Accounts Data - [ method GET ]
- path: /rest/V1/parentaccount/{parentID}/subaccounts/
- Get Customer Sub Account Data By Id - [ method GET ]
- path: /rest/V1/parentaccount/{parentID}/subaccounts/{subID}
- Create Sub Customer Account - [ method POST ]
- path: /rest/V1/parentaccount/{parentID}/subaccounts/
- Update Sub Customer Account By Id - [ method PUT ]
- path: /rest/V1/parentaccount/{parentID}/subaccounts/{subID}
- Delete Sub Customer Account By Id- [ method DELETE ]
- path: /rest/V1/parentaccount/{parentID}/subaccounts/{subID}
For POST / PUT, the body of a API call should look like so:
"firstname": "Account",
"lastname": "LastName",
"email": "team@cminds.com",
"password": "Password123",
"website_id": "1",
"group_id": "1",
"prefix": null,
"middlename": null,
"suffix": null,
"dob": "1973-03-19",
"taxvat": null,
"gender": "1",
"is_active": "1",
"manage_subaccounts": "1",
"supplier_approve": "",
"company": "",
"city": "Calder",
"country_id": "US",
"region": "33",
"postcode": "49628-7978",
"telephone": "(555) 229-3320",
"fax": "",
"vat_id": "",
"street_1": "6146 Honey Bluff Parkway",
"street_2": ""
}
Moving Sub-user Account from one Parent to another
The rest-API can be used to change the parent account of a sub-user account.
- Changing the parent account of a sub-user account by Id - [ method PUT ]
- path: /rest/V1/parentaccount/{Parent ID}/subaccounts/{Sub-user ID}
The {Parent ID} above is the Customer ID of the new parent/master account for the sub-user account.
The {Sub-user ID} is the customer ID of the sub-user who is having a new parent account assigned.
The "parent_email": "demo@cminds.com" is the email address of the new parent account.
"parent_email": "demo@email.com"
}
Promoting Sub-user Accounts to Parent account status
Sub-user accounts can be promoted to parent accounts quite easily through API calls.
- Changing the parent account of a sub-user account by Id - [ method PUT ]
- path: /rest/V1/parentaccount/{Parent ID}/subaccounts/{Sub-user ID}
Then use the parameter:-
"promote": "1"
}
Adding Permissions to Sub-user Account
You can easily set permissions to a sub-user account with the following request.
- Setting subaccount permissions by parent customer ID + sub-user account customer ID - [ method PUT ]
- path: /V1/parentaccount/{parent_customer_id}/subaccounts/{subaccount_customer_id}/permissions
Then use the parameters in the request body:-
"permissions": {
"account_data_modification_permission":"1", "account_order_history_view_permission":"1", "checkout_order_create_permission":"1", "checkout_order_approval_permission":"1", "checkout_cart_view_permission":"1", "checkout_view_permission":"1", "checkout_order_placed_notification_permission":"0", "force_usage_parent_company_name_permission":"0", "force_usage_parent_vat_permission":"0", "force_usage_parent_addresses_permission":"0", "manage_subaccounts":"0" }
Get more information about the Multi User Account M2 Magento Extension Find other Magento products at the CreativeMinds Magento Store |
|
Let us know how we can Improve this Product Documentation Page To open a Support Ticket visit our support center |