Skip to content

Latest commit

 

History

History
78 lines (46 loc) · 5.32 KB

service-create-customer.md

File metadata and controls

78 lines (46 loc) · 5.32 KB
layout title
default
Services Use Case—Creating a Customer

This topic discusses a simplified way to create customer records using a conceptualized client. We discuss how to get started by helping you examine service layer models and interfaces to get the mechanism you need; and how to use the data object builder and service data objects to send the data. After you read this topic, you should have a general idea of how to work with services.

This topic does not discuss the details of creating customers (for example, registering using Facebook credentials or copying customers from an external database).

Getting Started

Before you think about writing your own service, you should look at an existing Magento service to see what it offers you. For example, the Customer service provides the following resources:

  • Models

    Models interact with resources to do things like get objects, set passwords, and perform authentication. Customer service models have more than 70 public methods, including public function loadByEmail($customerEmail) in Customer, which gets a customer record using their e-mail address.

    One advantage of using a service is your client code doesn't interact directly with the model at all; the service does that for you.

  • Interfaces

    Clients interact with services using methods on their interfaces, as discussed in Service contracts. Customer service interfaces have more than 20 public methods, including public function createCustomer() in CustomerAccountServiceInterface, which creates a customer record.

  • Service data objects

    Service data objects send data to and from interfaces. Service data objects are "read-only", meaning they have getters but not setters. The Customer service has several service data objects, including Customer, which returns customer data.

  • Service data object builders

    Builders have the setters you can use to set data values in the service data object before sending them to the service to be consumed. For example, CustomerBuilder has a setFirstname method you can use to set a customer's first name. You can get the first name using the getFirstname method in Customer data object.

Creating the Customer Record

Create a customer record as follows:

Step 1: Locate the createCustomer method

Locate the createCustomer method on the CustomerAccountServiceInterface.

<p>Notice the code comments also:</p>

<script src="https://gist.github.com/xcomSteveJohnson/398aa808d3986351b972.js"></script>

<p>This is the interface your client code interacts with. </p></div>

<h3>Step 2: Declare a constructor dependency</h3>

In your client code, declare a constructor dependency:

<script src="https://gist.github.com/xcomSteveJohnson/4b9a08174a6aaa83a4e8.js"></script>

<p>Dependency injection passes (injects) dependencies to an object instead of the object pulling the dependencies from the environment. In other words, instead of objects configuring themselves, the objects are configured by an external entity. For more information, see <a href="https://wiki.magento.com/display/MAGE2DOC/Using+Dependency+Injection" target="_blank">Using Dependency Injection</a>.</p>

Constructor dependency injection uses a constructor to declare the dependencies. In dependencies in the preceding example are named:

Step 3: Create the customer record

In your client code, create the customer record.

<script src="https://gist.github.com/xcomSteveJohnson/d9c51387caa8f7f8d15f.js"></script>

This code uses the dependencies declared in step 2 to create a customer record.

Summary

The preceding section showed how to:

  • Create a customer record. The createCustomer call also sets a default password for the customer.
  • Create an address for the customer.
  • Specified the address as the default shipping and billing address.

Related topics