Skip to content

customer ​

Customer management

Overview ​

k.commerce.customer provides customer-related operations, including creation, query, login, order management and other functions.

create() ​

Create a customer.

ts
k.api.post(() => {
    const customer = k.commerce.customer.create({
        email: 'test@example.com',
        firstName: 'Test',
        lastName: 'User',
        password: '123456',
        phone: '15291101010'
    })
    return { id: customer.id, email: customer.email }
})

parameter:

ParameterTypeRequiredDescription
valueobjectyescustomer information
value.emailstringyesMail
value.firstNamestringnoname
value.lastNamestringnosurname
value.passwordstringnopassword
value.phonestringnoTelephone

Return: Customer

get() ​

Get the customer based on customer ID.

ts
k.api.get(() => {
    const customer = k.commerce.customer.get('customer-id')
    return { id: customer.id, email: customer.email }
})

parameter:

ParameterTypeRequiredDescription
IDstringyesCustomer ID

Return: Customer

getByEmail() ​

Get customers based on their email addresses.

ts
k.api.get(() => {
    const customer = k.commerce.customer.getByEmail('test@example.com')
    return { id: customer.id, email: customer.email }
})

parameter:

ParameterTypeRequiredDescription
emailstringyesCustomer email

Return: Customer

getByPhone() ​

Obtain customers based on their mobile phone numbers.

ts
k.api.get(() => {
    const customer = k.commerce.customer.getByPhone('15291101010')
    return { id: customer.id, phone: customer.phone }
})

parameter:

ParameterTypeRequiredDescription
phonestringyesPhone number

Return: Customer

login() ​

Customer login.

ts
k.api.post(() => {
    const result = k.commerce.customer.login('test@example.com', '123456')
    if (!result) {
        return { error: 'email or password is incorrect' }
    }
    return { id: result.id, email: result.email }
})

parameter:

ParameterTypeRequiredDescription
emailstringyesMail
passwordstringyespassword

Return: Customer | string - Returns Customer if login is successful, error message is returned if failed

setPassword() ​

Set customer password.

ts
k.api.post(() => {
    k.commerce.customer.setPassword('customer-id', 'new-password')
    return 'password set success'
})

parameter:

ParameterTypeRequiredDescription
customerIdstringyesCustomer ID
passwordstringyesNew Password

Return: Customer

setOAuth() ​

Set up the customer's OAuth information.

ts
k.api.post(() => {
    k.commerce.customer.setOAuth('customer-id', 'open-id-123', 'Apple')
    return 'set OAuth success'
})

parameter:

ParameterTypeRequiredDescription
customerIdstringyesCustomer ID
openIdstringyesopenId of third-party platform
TypestringyesThird-party platform type (such as Apple)

Return: void

getByOAuth() ​

Get customers based on OAuth information.

ts
k.api.get(() => {
    const customer = k.commerce.customer.getByOAuth('open-id-123', 'Apple')
    return { id: customer.id, email: customer.email }
})

parameter:

ParameterTypeRequiredDescription
openIdstringyesThird-party platform openId
TypestringyesThird-party platform type

Return: Customer

oAuthList() ​

Get the OAuth list bound to the customer.

ts
k.api.get(() => {
    const list = k.commerce.customer.oAuthList('customer-id')
    return { count: list.length, items: list }
})

parameter:

ParameterTypeRequiredDescription
customerIdstringyesCustomer ID

Return: CustomerOAuthModel[]

orders() ​

Get the customer's order list.

ts
k.api.get(() => {
    const orders = k.commerce.customer.orders('customer-id', {
        pageIndex: 1,
        pageSize: 10,
        paid: true,
        delivered: true,
        canceled: false
    })
    return { count: orders.count, orders: orders.list }
})

parameter:

ParameterTypeRequiredDescription
customerIdstringyesCustomer ID
queryobjectnoquery parameters
query.pageIndexnumbernoPage number, default 1
query.pageSizenumbernoNumber of items per page, default 10
query.paidbooleannoHas it been paid?
query.deliveredbooleannoHas it been shipped?
query.canceledbooleannoHas it been cancelled?
query.startDateDatenostart time
query.endDateDatenoDeadline

return:

ts
{
    list: OrderDetail[];
    count: number;
    pageIndex: number;
    pageSize: number;
}

updateInfo() ​

Update customer information.

ts
k.api.post(() => {
    k.commerce.customer.updateInfo('customer-id', {
        firstName: 'Updated',
        lastName: 'Name',
        phone: '13900139000',
        tags: ['vip', 'active']
    })
    return 'update success'
})

parameter:

ParameterTypeRequiredDescription
customerIdstringyesCustomer ID
infoobjectyesUpdate content
info.emailstringnoMail
info.firstNamestringnoname
info.lastNamestringnosurname
info.phonestringnoTelephone
info.tagsstring[]noLabel

Return: void

updateAddresses() ​

Update the customer's address list.

note

This operation is overwriting, that is, the passed array is the final address list.

ts
k.api.post(() => {
    k.commerce.customer.updateAddresses('customer-id', [{
        country: 'China',
        province: 'Fujian',
        city: 'Xiamen',
        address1: 'address1',
        address2: 'address2',
        zip: '361000',
        firstName: 'firstName',
        lastName: 'lastName',
        phone: '13800138000',
        isDefault: true
    }])
    return 'update success'
})

parameter:

ParameterTypeRequiredDescription
customerIdstringyesCustomer ID
addressesAddressModel[]yesaddress list

Return: void

remove() ​

Delete customer.

ts
k.api.post(() => {
    k.commerce.customer.remove('customer-id')
    return 'remove success'
})

parameter:

ParameterTypeRequiredDescription
customerIdstringyesCustomer ID

Return: void

structure ​

Customer attribute ​

PropertyDescriptionType
IDCustomer IDstring
emailCustomer emailstring
firstNameCustomer namestring
lastNameCustomer last namestring
phoneCustomer phone numberstring
passwordCustomer passwordstring
lastPlaceOrderDateLast order timestring
addressesaddress listAddressModel[]
tagstag liststring[]
createdAtcreation timestring
updatedAtUpdate timestring

AddressModel Property ​

PropertyDescriptionType
IDAddress IDstring
isDefaultWhether the default addressboolean
countrynationstring
provinceprovincestring
cityCitystring
address1Address 1string
address2Address 2string
firstNameConsignee namestring
lastNameConsignee's last namestring
phoneTelephonestring
zippost codestring

CustomerOAuthModel Properties ​

PropertyDescriptionType
CustomerIdCustomer IDstring
OpenIdThird-party platform openIdstring
TypeThird-party platform typestring
CreatedAtcreation timestring