Skip to main content

Identifying Customers

During the checkout

You might need to check if a customer is a returning customer for your store during the checkout process, so as to apply specific discounts and offer special pricing such as gifts. In that case, make sure you also visit our Coupons & Discounts Tutorial.

When the shopper types their email in the Simpler Checkout form, a quote request may be issued, including the customer email, in order to retrieve any special pricing or discounts the customer may qualify for. You can use the email field to locate the customer in your database, and apply any special logic to your carts before serializing the cart to a quote.

src/Controller/Simpler/QuoteController.php
class SimplerQuoteController extends AbstractController {
public function postData(Request $request): Response {
$requestData = json_decode($request->getContent(), true);

$response = [
'request_id' => $requestData['request_id'],
'quotes' => []
];

$cart = CartService::createCart();

if ($email = $request['quotation']['email']) {
$customer = CustomerService::getByEmail($email);
if ($customer) {
$cart->setCustomerId($customer->id);
}
}

try {
$this->addQuotationItemsToCart($request['quotation']['items'], $cart);
if ($coupon = $request['quotation']['coupon']) {
$this->applyCoupon($coupon, $cart);
}

$shippingAddress = $request['quotation']['address'];
$options = ShippingService::getShippingOptions($cart, $shippingAddress);

foreach ($options as $option) {
$response['quotes'][] = $this->buildQuote($cart, $option);
}

return $this->json($response);
} finally {
$cart->delete();
}
}
}

During order submission

The full shopper details will be made available for you during the order submission call. At this point, you have the shopper consent and the legitimate interest to create a customer entry on your database.

In order to make the best out of your integration, you should check if the customer already exists, and merge the submitted record to their already existing customer account. Let's update our SubmitController to handle the customer information:

src/Controller/Simpler/SubmitController.php
class SimplerSubmitController extends AbstractController {
public function postData(Request $request): Response {
$requestData = json_decode($request->getContent(), true);

$cart = CartService::createCart();
foreach ($request['order']['items'] as $item) {
$product = CatalogService::getProductBySKU($item['sku']);
$cart->addProduct($product, $item['quantity']);
}
$cart->setSelectedShippingOptionID($request['order']['shipping_method_id']);

$customer = $this->getOrCreateCustomer($request['user']);
$order = CheckoutService::createOrder($cart, $customer);

return $this->json([
'request_id' => $requestData['request_id'],
'order_id' => $order->getID()
]);
}

private function getOrCreateCustomer($userData): Customer {
$customer = CustomerService::getByEmail($userData['email']);
if ($customer) {
return $customer;
} else {
return CustomerService::createCustomer(new Customer([
'email' => $userData['email'],
'first_name' => $userData['first_name'],
'last_name' => $userData['last_name']
]));
}
}
}