Skip to content

Commit

Permalink
Enhance Address model to optionally include email and fixed date format
Browse files Browse the repository at this point in the history
annotations
  • Loading branch information
johnnynotsolucky committed Nov 5, 2024
1 parent 8a4bf03 commit 7c56b93
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
11 changes: 8 additions & 3 deletions src/models/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public function setEmail(string $email): void
$this->email = $email;
}

public static function fromCommerceAddress(CommerceOrder $commerceOrder, ?CraftAddress $commerceAddress): ?self
public static function fromCommerceAddress(CommerceOrder $commerceOrder, ?CraftAddress $commerceAddress, bool $includeEmail): ?self
{
if (! $commerceAddress instanceof CraftAddress) {
return null;
Expand All @@ -159,12 +159,11 @@ public static function fromCommerceAddress(CommerceOrder $commerceOrder, ?CraftA
$phoneNumberFieldHandle = Plugin::getInstance()?->settings->phoneNumberFieldHandle;
$phone = $commerceAddress->{$phoneNumberFieldHandle} ?? null;

return new self([
$address = new self([
'name' => $commerceAddress->fullName
?? $commerceOrder->getCustomer()?->fullName
?? 'Unknown',
'phone' => $phone,
'email' => $commerceOrder->email,
'company' => $commerceAddress->organization,
'address1' => $commerceAddress->addressLine1,
'address2' => $commerceAddress->addressLine2,
Expand All @@ -173,5 +172,11 @@ public static function fromCommerceAddress(CommerceOrder $commerceOrder, ?CraftA
'postalCode' => $commerceAddress->postalCode,
'country' => $commerceAddress->countryCode,
]);

if ($includeEmail) {
$address->setEmail($commerceOrder->email);
}

return $address;
}
}
4 changes: 2 additions & 2 deletions src/models/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ public static function fromCommerceOrder(CommerceOrder $commerceOrder): ?self
return $customer instanceof User
? new self([
'customerCode' => $customer->id,
'billToAddress' => Address::fromCommerceAddress($commerceOrder, $billingAddress),
'shipToAddress' => Address::fromCommerceAddress($commerceOrder, $shippingAddress),
'billToAddress' => Address::fromCommerceAddress($commerceOrder, $billingAddress, true),
'shipToAddress' => Address::fromCommerceAddress($commerceOrder, $shippingAddress, false),
])
: null;
}
Expand Down
4 changes: 2 additions & 2 deletions src/models/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class Order extends Base

#[Groups(['export'])]
#[SerializedName('LastModified')]
#[Type("DateTime<'n/j/Y H:m'>")]
#[Type("DateTime<'n/j/Y H:i'>")]
private ?\DateTime $lastModifiedDate = null;

#[Groups(['export'])]
Expand Down Expand Up @@ -82,7 +82,7 @@ class Order extends Base

#[Groups(['export'])]
#[SerializedName('OrderDate')]
#[Type("DateTime<'n/j/Y H:m'>")]
#[Type("DateTime<'n/j/Y H:i'>")]
private ?\DateTime $orderDate = null;

#[Exclude]
Expand Down

0 comments on commit 7c56b93

Please sign in to comment.