PHP code example of paulchiu / yaspa

1. Go to this page and download the library: Download paulchiu/yaspa library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

paulchiu / yaspa example snippets


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

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

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);
var_dump($newCustomer);

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
foreach ($customers as $index => $customer) {
    var_dump($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);

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

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