diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index 1a211d58..373e04f5 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -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`) \ No newline at end of file diff --git a/src/BigCommerce/ResourceModels/Customer/AttributeValue.php b/src/BigCommerce/ResourceModels/Customer/AttributeValue.php new file mode 100644 index 00000000..bcb5d863 --- /dev/null +++ b/src/BigCommerce/ResourceModels/Customer/AttributeValue.php @@ -0,0 +1,14 @@ +buildObjectArray('addresses', CustomerAddress::class); + $this->buildObjectArray('attributes', AttributeValue::class); + $this->buildObjectArray('form_fields', CustomerFormFieldValue::class); + $this->buildPropertyObject('authentication', CustomerAuthentication::class); + } } diff --git a/src/BigCommerce/ResourceModels/Customer/CustomerAttributeValue.php b/src/BigCommerce/ResourceModels/Customer/CustomerAttributeValue.php index a6d4fdf1..448d4f26 100644 --- a/src/BigCommerce/ResourceModels/Customer/CustomerAttributeValue.php +++ b/src/BigCommerce/ResourceModels/Customer/CustomerAttributeValue.php @@ -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; } diff --git a/src/BigCommerce/ResourceModels/Customer/CustomerAuthentication.php b/src/BigCommerce/ResourceModels/Customer/CustomerAuthentication.php new file mode 100644 index 00000000..beb48961 --- /dev/null +++ b/src/BigCommerce/ResourceModels/Customer/CustomerAuthentication.php @@ -0,0 +1,11 @@ + $value) { + $this->optionObject = $optionObject; + + $this->beforeBuildObject(); + + if (!is_null($this->optionObject)) { + foreach ($this->optionObject as $key => $value) { $this->$key = $value; } } @@ -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 + { + } } diff --git a/tests/BigCommerce/Api/Customers/CustomersApiTest.php b/tests/BigCommerce/Api/Customers/CustomersApiTest.php index c22ebb04..d9adb79a 100644 --- a/tests/BigCommerce/Api/Customers/CustomersApiTest.php +++ b/tests/BigCommerce/Api/Customers/CustomersApiTest.php @@ -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 @@ -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() diff --git a/tests/BigCommerce/responses/customers__get_all.json b/tests/BigCommerce/responses/customers__get_all.json index 286d6661..d0629ac7 100644 --- a/tests/BigCommerce/responses/customers__get_all.json +++ b/tests/BigCommerce/responses/customers__get_all.json @@ -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": {