Skip to content

Commit

Permalink
Merge pull request #68 from aligent/feature/67-additional-options-on-…
Browse files Browse the repository at this point in the history
…create-customers

Feature/67 additional options on create customers
  • Loading branch information
jswift authored May 10, 2021
2 parents 553bfe9 + 4e84fd8 commit fd360dc
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 6 deletions.
6 changes: 6 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@

### New Features

- Add the ability to specify attributes, addresses, form_fields, and authentication when creating customers (#67)

### Fixes

- Add missing Customer properties (`notes`, `tax_exempt_category`, `accepts_product_review_abandoned_cart_emails`,
`store_credit_amounts`, `origin_channel_id`, and `channel_ids`)
14 changes: 14 additions & 0 deletions src/BigCommerce/ResourceModels/Customer/AttributeValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace BigCommerce\ApiV3\ResourceModels\Customer;

use BigCommerce\ApiV3\ResourceModels\ResourceModel;

/**
* This is used when creating a customer with attribute values
*/
class AttributeValue extends ResourceModel
{
public int $attribute_id;
public string $attribute_value;
}
33 changes: 33 additions & 0 deletions src/BigCommerce/ResourceModels/Customer/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,37 @@ class Customer extends ResourceModel
public string $country;
public string $country_iso2;
public string $phone;
public int $customer_group_id;
public string $notes;
public string $tax_exempt_category;
public bool $accepts_product_review_abandoned_cart_emails;
public ?array $store_credit_amounts;
public ?int $origin_channel_id;
public ?array $channel_ids;

/**
* @var CustomerAddress[]
*/
public array $addresses;

/**
* @var AttributeValue[]
*/
public array $attributes;

/**
* @var CustomerFormFieldValue[]
*/
public array $form_fields;

public ?CustomerAuthentication $authentication;


protected function beforeBuildObject(): void
{
$this->buildObjectArray('addresses', CustomerAddress::class);
$this->buildObjectArray('attributes', AttributeValue::class);
$this->buildObjectArray('form_fields', CustomerFormFieldValue::class);
$this->buildPropertyObject('authentication', CustomerAuthentication::class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ class CustomerAttributeValue extends ResourceModel
{
public int $attribute_id;
public string $value;
public int $customer_id;
public int $id;
public ?int $customer_id;
public ?int $id;
}
11 changes: 11 additions & 0 deletions src/BigCommerce/ResourceModels/Customer/CustomerAuthentication.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace BigCommerce\ApiV3\ResourceModels\Customer;

use BigCommerce\ApiV3\ResourceModels\ResourceModel;

class CustomerAuthentication extends ResourceModel
{
public bool $force_password_reset;
public string $new_password;
}
37 changes: 35 additions & 2 deletions src/BigCommerce/ResourceModels/ResourceModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@

abstract class ResourceModel implements JsonSerializable
{
private ?stdClass $optionObject;

public function __construct(?stdClass $optionObject = null)
{
if (!is_null($optionObject)) {
foreach ($optionObject as $key => $value) {
$this->optionObject = $optionObject;

$this->beforeBuildObject();

if (!is_null($this->optionObject)) {
foreach ($this->optionObject as $key => $value) {
$this->$key = $value;
}
}
Expand All @@ -27,4 +33,31 @@ public function jsonSerialize(): array

return $data;
}

protected function buildObjectArray(string $property, string $className): void
{
if (!is_null($this->optionObject) && isset($this->optionObject->$property)) {
$this->$property = array_map(function ($m) use ($className) {
return new $className($m);
}, $this->optionObject->$property);

unset($this->optionObject->$property);
}
}

protected function buildPropertyObject(string $property, string $className): void
{
if (!is_null($this->optionObject) && isset($this->optionObject->$property)) {
$this->$property = new $className($this->optionObject->$property);

unset($this->optionObject->$property);
}
}

/**
* Override this function to implement custom build functionality
*/
protected function beforeBuildObject(): void
{
}
}
8 changes: 7 additions & 1 deletion tests/BigCommerce/Api/Customers/CustomersApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace BigCommerce\Tests\Api\Customers;

use BigCommerce\ApiV3\ResourceModels\Customer\AttributeValue;
use BigCommerce\ApiV3\ResourceModels\Customer\Customer;
use BigCommerce\ApiV3\ResourceModels\Customer\CustomerAddress;
use BigCommerce\Tests\BigCommerceApiTest;

class CustomersApiTest extends BigCommerceApiTest
Expand All @@ -11,8 +13,12 @@ public function testCanGetCustomers()
{
$this->setReturnData('customers__get_all.json');
$customersResponse = $this->getApi()->customers()->getAll();
$customers = $customersResponse->getCustomers();
$this->assertEquals(1, $customersResponse->getPagination()->total);
$this->assertEquals('John', $customersResponse->getCustomers()[0]->first_name);
$this->assertEquals('John', $customers[0]->first_name);
$this->assertInstanceOf(CustomerAddress::class, $customers[0]->addresses[0]);
$this->assertInstanceOf(AttributeValue::class, $customers[0]->attributes[0]);
$this->assertEquals('California', $customers[0]->addresses[0]->state_or_province);
}

public function testCanGetCustomerByEmail()
Expand Down
23 changes: 22 additions & 1 deletion tests/BigCommerce/responses/customers__get_all.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,28 @@
"date_created": "2020-08-27T06:18:07Z",
"date_modified": "2020-08-28T06:03:38Z",
"accepts_product_review_abandoned_cart_emails": true,
"channel_ids": null
"channel_ids": null,
"addresses": [
{
"address1": "Addr 1",
"address2": "",
"address_type": "residential",
"city": "San Francisco",
"company": "History",
"country_code": "US",
"first_name": "Ronald",
"last_name": "Swimmer",
"phone": "707070707",
"postal_code": "33333",
"state_or_province": "California"
}
],
"attributes": [
{
"attribute_id": 0,
"value": "string"
}
]
}
],
"meta": {
Expand Down

0 comments on commit fd360dc

Please sign in to comment.