Basics: Customers

Basics: Customers

Introduction

This section will cover the basics of customer related API's, including customer addresses management.

API Reference

You can find all available BigCommerce module methods and their description in the API Reference.

Register / Creating a new customer.

To create a new customer, you can use the createCustomer method.

The channel id will be automatically picked up from the requests header by the middleware. Once the registration has been successful and a new customer has been created, the middleware will generate a JWT token using the customer's ID returned from the registration's response, and store the JWT token as a customer-data cookie which will be included with the subsequent requests. This cookie is used in ensuring the security over operating over the customer specific data.

import { sdk } from '~/sdk.config.ts';
 
const createCustomerResponse = await sdk.bigcommerce.createCustomer({
  email: "email@account.com",
  first_name: "test",
  last_name: "test",
  password: "y0u1pa55w$rd",
  accepts_product_review_abandoned_cart_emails: true,
  custom_fields: []
});

Getting a Customer

To fetch data about a customer, you can use the getCustomer method. The customer must be authenticated first and the id of the customer will be picked up from the requests's cookie.

import { sdk } from '~/sdk.config.ts';
const customer = await sdk.bigcommerce.getCustomers()

You can specify the customer sub-resources you want to include in the response by passing them as a string in the include property.

import { sdk } from '~/sdk.config.ts';
const customer = await sdk.bigcommerce.getCustomers({
 include: 'formfields'
})

Update Customer

To update customer's data, you can use the updateCustomer method. The customer must be authenticated first and the id of the customer will be picked up from the requests's cookie. Only the fields that are sent in the payload with the request will be updated. For example, if you send the payload with validation and first_name properties, the last_name, email or any other fields that exist on the customer but were not included in the request will remain as they were. Please note that in order to update customer data, the customer will also be required to provide the credentials. The profile will be updated only if the credentials are validated.

import { sdk } from '~/sdk.config.ts';
const updateCustomerResponse = await sdk.bigcommerce.updateCustomer({
 validation: {
   email: 'john@doe.com',
   password: 'Pa$$w0r4'
 },
 first_name: 'Nicholas'
})

Example of changing the customer's password

import { sdk } from '~/sdk.config.ts';
const updateCustomerResponse = await sdk.bigcommerce.updateCustomer({
 validation: {
   email: 'john@doe.com',
   password: 'Pa$$w0r4'
 },
 authentication: {
   new_password: 'N3Wpa55w0rd009#'
 }
})

Update Customer Form Fields

To update the form field data on a customer, you can use the updateCustomerFormFields method. The customer must be authenticated first and the id of the customer will be picked up from the requests's cookie.

import { sdk } from '~/sdk.config.ts';

const updateCustomerFormFieldsResponse = await sdk.bigcommerce.updateCustomerFormFields({
 data: [{
   name: "Cart ID",
   value: "3e13llqq198hZueAhemahuehue91gbFre"
 }]
})

One of the use cases for the customer form fields is the storage of the Cart ID that will be fetched when the user is authenticated.

To retrieve the formfields data, you can specify the customer sub-resources you want to include in the getCustomers request by passing formfields in the include property.

import { sdk } from '~/sdk.config.ts';
const customer = await sdk.bigcommerce.getCustomers({
 include: 'formfields'
})

Get Customer Settings

To fetch customer settings, you can use the getCustomerSettings method.

import { sdk } from '~/sdk.config.ts';

const customerSettings = await sdk.bigcommerce.getCustomerSettings();

Create Customer Address

To create a customer address you can use the createCustomerAddress method. The associated customer id will be automatically picked up from the cookie in the requests header.

import { sdk } from '~/sdk.config.ts';
 
const createAddress = await sdk.bigcommerce.createCustomerAddress({
  first_name: 'Name',
  last_name: 'Surname',
  company: 'Company',
  address1: 'Address',
  address2: '2',
  city: 'Edinburgh',
  postal_code: 'EH1 1ST',
  country_code: 'GB',
  phone: '074743473312'
})

Update Customer Address

To update a customer address you can use the updateCustomerAddress method and pass the id of the address. The associated customer id will be automatically picked up from the cookie in the requests header.

import { sdk } from '~/sdk.config.ts';
 
const createAddress = await sdk.bigcommerce.updateCustomerAddress({
  id: 131,
  address_type: 'residential',
  city: 'Isengard'
});

Get Customer Address

To get a customer address you can use the getCustomerAddress method and pass the id of the address. The associated customer id will be automatically picked up from the cookie in the requests header.

import { sdk } from '~/sdk.config.ts';
 
const customerAddress = await sdk.bigcommerce.getCustomerAddress({
  ['id:in']: 131,
});

To get all customer addresses you can call the method without passing the customer id:

import { sdk } from '~/sdk.config.ts';
 
const customerAddresses = await sdk.bigcommerce.getCustomerAddress();

Delete Customer Address

To delete a customer address you can use the deleteCustomerAddress method and pass the id of the address. The associated customer id will be automatically picked up from the cookie in the requests header.

import { sdk } from '~/sdk.config.ts';
 
const deleteAddress = await sdk.bigcommerce.deleteCustomerAddress({
  'id:in': [112]
});