Customer
Retrieve all customers for a shop
All retrieval examples utilise the Yaspa\AdminApi\Customer\CustomerService::getCustomers
method, which
returns a Yaspa\Builders\PagedResultsIterator
that will provide an iterate-able list of
Yaspa\AdminApi\Customer\Models\Customer
models.
The getCustomers
method accepts a Yaspa\AdminApi\Customer\Builders\GetCustomersRequest
instance,
which is a request builder that will provide hints to what options are available assuming one is using
an IDE such as PHPStorm.
Get all customers for a shop changed after a certain date
The example demonstrates how to get customers updated since July 16th, 2017.
The response is a Yaspa\Builders\PagedResultsIterator
that will provide an iterate-able list of
Yaspa\AdminApi\Customer\Models\Customer
models.
use DateTime;
use Yaspa\AdminApi\Customer\Builders\GetCustomersRequest;
use Yaspa\AdminApi\Customer\CustomerService;
use Yaspa\Authentication\Factory\ApiCredentials;
use Yaspa\Factory;
// Prepare the request
$credentials = Factory::make(ApiCredentials::class)
->makeOAuth('johns-apparel', 'a190000000000000000000000000046a');
$request = Factory::make(GetCustomersRequest::class)
->withCredentials($credentials)
->withUpdatedAtMin(new DateTime('2017-07-16 23:07:25'));
// Submit the request and get a list of customers
$service = Factory::make(CustomerService::class);
$customers = $service->getCustomers($request);
Get a list of specific customers
The example demonstrates how to get customers with ids 207119551
and 1073339457
.
The response is a Yaspa\Builders\PagedResultsIterator
that will provide an iterate-able list of
Yaspa\AdminApi\Customer\Models\Customer
models.
use Yaspa\AdminApi\Customer\Builders\GetCustomersRequest;
use Yaspa\AdminApi\Customer\CustomerService;
use Yaspa\Authentication\Factory\ApiCredentials;
use Yaspa\Factory;
// Prepare the request
$credentials = Factory::make(ApiCredentials::class)
->makeOAuth('johns-apparel', 'a190000000000000000000000000046a');
$request = Factory::make(GetCustomersRequest::class)
->withCredentials($credentials)
->withIds([207119551, 1073339457]);
// Submit the request and get a list of customers
$service = Factory::make(CustomerService::class);
$customers = $service->getCustomers($request);
Get all customers for a shop
The example demonstrates how to get all customers for a shop.
The response is a Yaspa\Builders\PagedResultsIterator
that will provide an iterate-able list of
Yaspa\AdminApi\Customer\Models\Customer
models.
use Yaspa\AdminApi\Customer\Builders\GetCustomersRequest;
use Yaspa\AdminApi\Customer\CustomerService;
use Yaspa\Authentication\Factory\ApiCredentials;
use Yaspa\Factory;
// Prepare the request
$credentials = Factory::make(ApiCredentials::class)
->makeOAuth('johns-apparel', 'a190000000000000000000000000046a');
$request = Factory::make(GetCustomersRequest::class)
->withCredentials($credentials);
// Submit the request and get a list of customers
$service = Factory::make(CustomerService::class);
$customers = $service->getCustomers($request);
Get all customers for a shop after a specified ID
The example demonstrates how to get all customers after a specified customer id.
The response is a Yaspa\Builders\PagedResultsIterator
that will provide an iterate-able list of
Yaspa\AdminApi\Customer\Models\Customer
models.
use Yaspa\AdminApi\Customer\Builders\GetCustomersRequest;
use Yaspa\AdminApi\Customer\CustomerService;
use Yaspa\Authentication\Factory\ApiCredentials;
use Yaspa\Factory;
// Prepare the request
$credentials = Factory::make(ApiCredentials::class)
->makeOAuth('johns-apparel', 'a190000000000000000000000000046a');
$request = Factory::make(GetCustomersRequest::class)
->withCredentials($credentials)
->withSinceId(207119551);
// Submit the request and get a list of customers
$service = Factory::make(CustomerService::class);
$customers = $service->getCustomers($request);
Search for customers matching supplied query
The example demonstrates how to get all customers with an address in the US and a name like "Bob".
The response is a Yaspa\Builders\PagedResultsIterator
that will provide an iterate-able list of
Yaspa\AdminApi\Customer\Models\Customer
models.
The official Shopify API documentation reference in the source link does not provide an extensive
explanation of what search fields such as country:
are available. Nevertheless, most
search parameters are passed through the query string that can be set using the withQuery
method
on Yaspa\AdminApi\Customer\Builders\SearchCustomersRequest
use Yaspa\AdminApi\Customer\Builders\SearchCustomersRequest;
use Yaspa\AdminApi\Customer\CustomerService;
use Yaspa\Authentication\Factory\ApiCredentials;
use Yaspa\Factory;
// Prepare the request
$credentials = Factory::make(ApiCredentials::class)
->makeOAuth('johns-apparel', 'a190000000000000000000000000046a');
$request = Factory::make(SearchCustomersRequest::class)
->withCredentials($credentials)
->withQuery('Bob country:United States');
// Submit the request and get a list of matching customers
$service = Factory::make(CustomerService::class);
$customers = $service->searchCustomers($request);
Retrieve a single customer
The example shows how to get a single customer by ID.
The response is a Yaspa\AdminApi\Customer\Models\Customer
model.
use Yaspa\AdminApi\Customer\CustomerService;
use Yaspa\Authentication\Factory\ApiCredentials;
use Yaspa\Factory;
// Get credentials
$credentials = Factory::make(ApiCredentials::class)
->makeOAuth('johns-apparel', 'a190000000000000000000000000046a');
// Get customer
$service = Factory::make(CustomerService::class);
$customer = $service->getCustomerById($credentials, 207119551);
Create a new customer
Creating a new customer involves creating and populating a Yaspa\AdminApi\Customer\Models\Customer
instance.
The method Yaspa\AdminApi\Customer\CustomerService::createNewCustomer
will generally return a customer model
back. Although it seems redundant, one should still use the Shopify returned customer model as the trusted version of
the created customer given that Shopify is the source of truth.
Trying to create a customer without an email or name will return an error
The request attempts to create a customer with no details.
The result is a Guzzle exception. Although it may vary depending on how the Guzzle client is configured. See Guzzle http_errors options for more information.
use Yaspa\AdminApi\Customer\CustomerService;
use Yaspa\AdminApi\Customer\Models\Customer;
use Yaspa\Authentication\Factory\ApiCredentials;
use Yaspa\Factory;
// Get credentials
$credentials = Factory::make(ApiCredentials::class)
->makeOAuth('johns-apparel', 'a190000000000000000000000000046a');
// Prepare request
$customer = new Customer();
$request = Factory::make(CreateNewCustomerRequest::class)
->withCredentials($credentials)
->withCustomer($customer);
/**
* Create customer, a Guzzle exception will be thrown
*/
$service = Factory::make(CustomerService::class);
$newCustomer = $service->createNewCustomer($request);
Create a new customer with send_email_invite
The example shows how to create a customer, with an address, with automated invite sending.
The response is a Yaspa\AdminApi\Customer\Models\Customer
model.
use Yaspa\AdminApi\Customer\Builders\CreateNewCustomerRequest;
use Yaspa\AdminApi\Customer\CustomerService;
use Yaspa\AdminApi\Customer\Models\Address;
use Yaspa\AdminApi\Customer\Models\Customer;
use Yaspa\Authentication\Factory\ApiCredentials;
use Yaspa\Factory;
// Get credentials
$credentials = Factory::make(ApiCredentials::class)
->makeOAuth('johns-apparel', 'a190000000000000000000000000046a');
// Prepare request
$address = (new Address())
->setAddress1('123 Oak St')
->setCity('Ottawa')
->setProvince('ON')
->setPhone('555-1212')
->setZip('123 ABC')
->setLastName('Lastnameson')
->setFirstName('Mother')
->setCountry('CA');
$customer = (new Customer())
->setFirstName('Steve')
->setLastName('Lastnameson')
->setEmail('[email protected]')
->setVerifiedEmail(true)
->setAddresses([$address]);
$request = Factory::make(CreateNewCustomerRequest::class)
->withCredentials($credentials)
->withCustomer($customer)
->withSendEmailInvite(true); // Send email invite specified at request builder level
// Create customer
$service = Factory::make(CustomerService::class);
$newCustomer = $service->createNewCustomer($request);
Create a new customer with a metafield
The example shows how to create a new customer with a metafield.
The response is a Yaspa\AdminApi\Customer\Models\Customer
model.
use Yaspa\AdminApi\Customer\Builders\CreateNewCustomerRequest;
use Yaspa\AdminApi\Customer\CustomerService;
use Yaspa\AdminApi\Customer\Models\Customer;
use Yaspa\Authentication\Factory\ApiCredentials;
use Yaspa\Factory;
// Get credentials
$credentials = Factory::make(ApiCredentials::class)
->makeOAuth('johns-apparel', 'a190000000000000000000000000046a');
// Prepare request
$customer = (new Customer())
->setFirstName('Steve')
->setLastName('Lastnameson')
->setEmail('[email protected]');
$metafield = (new Metafield())
->setKey('new')
->setValue('newvalue')
->setValueType('string')
->setNamespace('global');
$request = Factory::make(CreateNewCustomerRequest::class)
->withCredentials($credentials)
->withCustomer($customer)
->withMetafields([$metafield]);
// Create customer
$service = Factory::make(CustomerService::class);
$newCustomer = $service->createNewCustomer($request);
Create a new customer record
The example shows how to create a new customer with an address.
The response is a Yaspa\AdminApi\Customer\Models\Customer
model.
use Yaspa\AdminApi\Customer\Builders\CreateNewCustomerRequest;
use Yaspa\AdminApi\Customer\CustomerService;
use Yaspa\AdminApi\Customer\Models\Address;
use Yaspa\AdminApi\Customer\Models\Customer;
use Yaspa\Authentication\Factory\ApiCredentials;
use Yaspa\Factory;
// Get credentials
$credentials = Factory::make(ApiCredentials::class)
->makeOAuth('johns-apparel', 'a190000000000000000000000000046a');
// Prepare request
$address = (new Address())
->setAddress1('123 Oak St')
->setCity('Ottawa')
->setProvince('ON')
->setPhone('555-1212')
->setZip('123 ABC')
->setLastName('Lastnameson')
->setFirstName('Mother')
->setCountry('CA');
$customer = (new Customer())
->setFirstName('Steve')
->setLastName('Lastnameson')
->setEmail('[email protected]')
->setVerifiedEmail(true)
->setAddresses([$address]);
$request = Factory::make(CreateNewCustomerRequest::class)
->withCredentials($credentials)
->withCustomer($customer);
// Create customer
$service = Factory::make(CustomerService::class);
$newCustomer = $service->createNewCustomer($request);
Create a new customer with password and password_confirmation and skip sending the welcome email
The example hows how to create a customer with a password and password confirmation whilst skipping the welcome email.
The response is a Yaspa\AdminApi\Customer\Models\Customer
model.
use Yaspa\AdminApi\Customer\Builders\CreateNewCustomerRequest;
use Yaspa\AdminApi\Customer\CustomerService;
use Yaspa\AdminApi\Customer\Models\Customer;
use Yaspa\Authentication\Factory\ApiCredentials;
use Yaspa\Factory;
// Get credentials
$credentials = Factory::make(ApiCredentials::class)
->makeOAuth('johns-apparel', 'a190000000000000000000000000046a');
// Prepare request
$customer = (new Customer())
->setFirstName('Steve')
->setLastName('Lastnameson')
->setEmail('[email protected]')
->setVerifiedEmail(true);
$request = Factory::make(CreateNewCustomerRequest::class)
->withCredentials($credentials)
->withCustomer($customer)
->withPassword('foo-bar') // Password and confirmation set at request builder level
->withPasswordConfirmation('foo-bar')
->withSendEmailInvite(false);
// Create customer
$service = Factory::make(CustomerService::class);
$newCustomer = $service->createNewCustomer($request);
Modify an existing customer
Modifying, or updating, a customer involves populating a Yaspa\AdminApi\Customer\Models\Customer
instance,
however, the id
attribute must be set.
Please note that Yaspa does not defensively check that an id exists in a customer model when used in an update request as the expectation is that Shopify will return an error.
Update a customer's tags
The example updates the tags associated with a customer.
The response is a Yaspa\AdminApi\Customer\Models\Customer
model.
use Yaspa\AdminApi\Customer\Builders\ModifyExistingCustomerRequest;
use Yaspa\AdminApi\Customer\CustomerService;
use Yaspa\AdminApi\Customer\Models\Customer;
use Yaspa\Authentication\Factory\ApiCredentials;
use Yaspa\Factory;
// Get credentials
$credentials = Factory::make(ApiCredentials::class)
->makeOAuth('johns-apparel', 'a190000000000000000000000000046a');
// Prepare request
$customerUpdates = (new Customer())
->setId(207119551)
->setTags(['New Customer', 'Repeat Customer']);
// Modify customer
$request = Factory::make(ModifyExistingCustomerRequest::class)
->withCredentials($credentials)
->withCustomer($customerUpdates);
$service = Factory::make(CustomerService::class);
$modifiedCustomer = $service->modifyExistingCustomer($request);
Update details for a customer
The example updates the details for a customer.
The response is a Yaspa\AdminApi\Customer\Models\Customer
model.
use Yaspa\AdminApi\Customer\Builders\ModifyExistingCustomerRequest;
use Yaspa\AdminApi\Customer\CustomerService;
use Yaspa\AdminApi\Customer\Models\Customer;
use Yaspa\Authentication\Factory\ApiCredentials;
use Yaspa\Factory;
// Get credentials
$credentials = Factory::make(ApiCredentials::class)
->makeOAuth('johns-apparel', 'a190000000000000000000000000046a');
// Prepare request
$customerUpdates = (new Customer())
->setId(207119551)
->setEmail('[email protected]')
->setNote('Customer is a great guy');
// Modify customer
$request = Factory::make(ModifyExistingCustomerRequest::class)
->withCredentials($credentials)
->withCustomer($customerUpdates);
$service = Factory::make(CustomerService::class);
$modifiedCustomer = $service->modifyExistingCustomer($request);
Add metafield to an existing customer
use Yaspa\AdminApi\Customer\Builders\ModifyExistingCustomerRequest;
use Yaspa\AdminApi\Customer\CustomerService;
use Yaspa\AdminApi\Customer\Models\Customer;
use Yaspa\Authentication\Factory\ApiCredentials;
use Yaspa\Factory;
// Get credentials
$credentials = Factory::make(ApiCredentials::class)
->makeOAuth('johns-apparel', 'a190000000000000000000000000046a');
// Prepare request
$customer = (new Customer())
->setId(207119551);
$metafield = (new Metafield())
->setKey('new')
->setValue('newvalue')
->setValueType('string')
->setNamespace('global');
// Modify customer
$request = Factory::make(ModifyExistingCustomerRequest::class)
->withCredentials($credentials)
->withCustomer($customer)
->withMetafields([$metafield]);
$service = Factory::make(CustomerService::class);
$modifiedCustomer = $service->modifyExistingCustomer($request);
Create account activation URL
Please note the Shopify explanation for this feature:
This endpoint allows you to generate and retrieve an account activation URL for a customer who is not yet enabled. This is useful if you've imported a large number of customers and want to send them activation emails all at once (using this approach, you'll need to generate and send the activation emails yourself).
The account activation URL generated by this endpoint is one time use and will expire after 7 days. If you make a new POST request to this endpoint, a new URL will be generated which will be again valid for 7 days, but the previous URL will no longer be valid.
Attempting to create an account activation URL for an enabled customer returns an error
The example shows how one would make an account activation URL. The example assumes that customer with
id 207119551
is already enabled.
The result will be a thrown Guzzle exception with response code 422
.
use Yaspa\AdminApi\Customer\CustomerService;
use Yaspa\Authentication\Factory\ApiCredentials;
use Yaspa\Factory;
// Get credentials
$credentials = Factory::make(ApiCredentials::class)
->makeOAuth('johns-apparel', 'a190000000000000000000000000046a');
// A Guzzle exception will be thrown
$service = Factory::make(CustomerService::class);
$service->createAccountActivationUrlForCustomerId($credentials, 207119551);
Create account activation URL for an invited or disabled customer
The example shows how to create an account activation URL. The code is basically the
same as the previous example. However, this time we are assuming the customer with id 207119551
has not yet been enabled.
The response is a GuzzleHttp\Psr7\Uri
instance.
use Yaspa\AdminApi\Customer\CustomerService;
use Yaspa\Authentication\Factory\ApiCredentials;
use Yaspa\Factory;
// Get credentials
$credentials = Factory::make(ApiCredentials::class)
->makeOAuth('johns-apparel', 'a190000000000000000000000000046a');
// Create account activation URL
$service = Factory::make(CustomerService::class);
$url = $service->createAccountActivationUrlForCustomerId($credentials, 207119551);
Send an invite
Please note that when testing invite sending, the provided customer must have a valid email address.
Send the default invite
The example shows how to send a default invite message with a customer id.
The response is a Yaspa\AdminApi\Customer\Models\CustomerInvite
model.
use Yaspa\AdminApi\Customer\CustomerService;
use Yaspa\Authentication\Factory\ApiCredentials;
use Yaspa\Factory;
// Get credentials
$credentials = Factory::make(ApiCredentials::class)
->makeOAuth('johns-apparel', 'a190000000000000000000000000046a');
// Send invite
$service = Factory::make(CustomerService::class);
$invite = $service->sendAccountInviteForCustomerId($credentials, 207119551);
Send a customized invite
The example shows how to send a custom invite message with a customer id.
The response is a Yaspa\AdminApi\Customer\Models\CustomerInvite
model.
use Yaspa\AdminApi\Customer\CustomerService;
use Yaspa\AdminApi\Customer\Models\CustomerInvite;
use Yaspa\Authentication\Factory\ApiCredentials;
use Yaspa\Factory;
// Get credentials
$credentials = Factory::make(ApiCredentials::class)
->makeOAuth('johns-apparel', 'a190000000000000000000000000046a');
// Prepare customised invite
$invite = (new CustomerInvite())
->setSubject('Welcome to my new shop')
->setCustomMessage('My awesome new store');
// Send invite
$service = Factory::make(CustomerService::class);
$invite = $service->sendAccountInviteForCustomerId($credentials, 207119551, $invite);
Remove an existing customer
The example shows how to delete a customer using their id.
The response is an empty object.
use Yaspa\AdminApi\Customer\CustomerService;
use Yaspa\Authentication\Factory\ApiCredentials;
use Yaspa\Factory;
// Get credentials
$credentials = Factory::make(ApiCredentials::class)
->makeOAuth('johns-apparel', 'a190000000000000000000000000046a');
// Delete customer
$service = Factory::make(CustomerService::class);
$result = $service->deleteCustomerById($credentials, 207119551);
Get count of all customers for a shop
The example shows how to get a count of the number of customers in a shop.
The response is an integer value representing the count.
use Yaspa\AdminApi\Customer\CustomerService;
use Yaspa\Authentication\Factory\ApiCredentials;
use Yaspa\Factory;
// Get credentials
$credentials = Factory::make(ApiCredentials::class)
->makeOAuth('johns-apparel', 'a190000000000000000000000000046a');
// Count customers
$service = Factory::make(CustomerService::class);
$customerCount = $service->countAllCustomers($credentials);
Get all orders from this customer in this shop
@todo Write docs once order related classes are implemented
// @todo Provide example