Examples

The following examples show how CRUD works with the API.

Please note that all examples utilise private authentication.

Create private authentication credentials

Credentials are stored in a Plain-Old-PHP-Object model.

All other examples assume the presence of the following code.

use Yaspa\Authentication\Factory\ApiCredentials;
use Yaspa\Factory;

$credentials = Factory::make(ApiCredentials::class)
    ->makePrivate(
        'my-shop',
        '4ac0000000000000000000000000035f',
        '59c0000000000000000000000000007f'
    );

Create a customer

Each Shopify API resource has a service class that encapsulates that resources methods. These exist purely for convenience, as one could use builders to create a Guzzle client request and send it with a Guzzle client instance.

This example creates a customer model and makes a calls a create new customer method in the customer service using a request builder. This is a common pattern in Yaspa with the general rule being that if there are more than 3 parameters accepted by a method, a builder will be used instead.

use Yaspa\AdminApi\Customer\Builders\CreateNewCustomerRequest;
use Yaspa\AdminApi\Customer\CustomerService;
use Yaspa\AdminApi\Customer\Models\Customer;
use Yaspa\Factory;

// Prepare creation request
$customer = (new Customer())
    ->setFirstName('Steve')
    ->setLastName('Lastnameson')
    ->setEmail(uniqid('steve-').'@example.com')
    ->setTags(['foo', 'bar'])
    ->setVerifiedEmail(true)
    ->setAcceptsMarketing(true);
$request = Factory::make(CreateNewCustomerRequest::class)
    ->withCredentials($credentials)
    ->withCustomer($customer);

// Create new customer, $newCustomer is a Customer model
$service = Factory::make(CustomerService::class);
$newCustomer = $service->createNewCustomer($request);

Get all customers created in the past 7 days

Getting a list of resources also requires a builder. However, Yaspa returns an interator when a collection of results are available. This means that you do not have to worry about incrementing pages. Although limits on records per page can still be set.

use Yaspa\AdminApi\Customer\Builders\GetCustomersRequest;
use Yaspa\AdminApi\Customer\CustomerService;
use Yaspa\Factory;

// Create request
$request = Factory::make(GetCustomersRequest::class)
    ->withCredentials($credentials)
    ->withCreatedAtMin(new DateTime('-7 days'));

// Get customers, $customers is an iterator
$service = Factory::make(CustomerService::class);
$customers = $service->getCustomers($request);

// Loop through customers, each $customer is a Customer model
// paging is automated, $index is the customer id
foreach ($customers as $index => $customer) {
    var_dump($customer);
}

Update a customer

Updating a customer is not as perfectly fluent as it could possibly be. However, the design decision here is to preference a functional nature rather than active record style stateful model that allows calling of methods such as save()

The example demonsrates how one would change the first name of an existing customer.

use Yaspa\AdminApi\Customer\Builders\ModifyExistingCustomerRequest;
use Yaspa\AdminApi\Customer\CustomerService;
use Yaspa\Factory;

// Create request
$customerUpdates = (new Customer())
    ->setId(6820000675)
    ->setFirstName('Alice')
$request = Factory::make(ModifyExistingCustomerRequest::class)
    ->withCredentials($credentials)
    ->withCustomer($customerUpdates);

// Modify an existing customer, $modifiedCustomer is a Customer model
$service = Factory::make(CustomerService::class);
$modifiedCustomer = $service->modifyExistingCustomer($request);
var_dump($modifiedCustomer);

Delete a customer

Deleting a customer demonstrates using a method that doe snot require a builder. The service method accepts a credential and a customer id.

use Yaspa\AdminApi\Customer\CustomerService;
use Yaspa\Factory;

// Delete an existing customer
$service = Factory::make(CustomerService::class);
$service->deleteCustomerById($credentials, 6820000675);

results matching ""

    No results matching ""