Skip to content

Commit

Permalink
Allow setting firstname and lastname in address data on order
Browse files Browse the repository at this point in the history
Fixes #3015
  • Loading branch information
lukeholder committed Jul 11, 2023
1 parent 1b98df2 commit b2e6a37
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/elements/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
use craft\db\Query;
use craft\elements\Address;
use craft\elements\Address as AddressElement;
use craft\elements\Address as AddressElement;
use craft\elements\User;
use craft\errors\ElementNotFoundException;
use craft\errors\InvalidElementException;
Expand Down Expand Up @@ -2897,6 +2898,7 @@ public function setShippingAddress(AddressElement|array|null $address): void
unset($address['id']);
$addressElement = $this->_shippingAddress ?: new AddressElement();
$addressElement->setAttributes($address);
$this->_populateAddressNameAttributes($addressElement, $address);
$addressElement->ownerId = $this->id;
$address = $addressElement;
}
Expand Down Expand Up @@ -2990,6 +2992,7 @@ public function setBillingAddress(AddressElement|array|null $address): void
unset($address['id']); // only ever allow setting of the address data
$addressElement = $this->_billingAddress ?: new AddressElement();
$addressElement->setAttributes($address);
$this->_populateAddressNameAttributes($addressElement, $address);
$addressElement->ownerId = $this->id;
$address = $addressElement;
}
Expand Down Expand Up @@ -3550,4 +3553,25 @@ private function _saveOrderHistory(?int $oldStatusId, ?int $currentOrderStatId):
}
}
}

/**
* Sets the first and last name attributes on the address model if no full name is set.
*
* @param AddressElement $addressElement
* @param array $address
* @return void
*/
private function _populateAddressNameAttributes(AddressElement $addressElement, array $address): void
{
if (!isset($address['fullName']) || !$address['fullName']) {
$firstName = $address['firstName'] ?? null;
$lastName = $address['lastName'] ?? null;

if ($firstName !== null || $lastName !== null) {
$addressElement->fullName = null;
$addressElement->firstName = $firstName ?? $addressElement->firstName;
$addressElement->lastName = $lastName ?? $addressElement->lastName;
}
}
}
}

0 comments on commit b2e6a37

Please sign in to comment.