diff --git a/CHANGELOG.md b/CHANGELOG.md index 59bc358cba..0009d4a004 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ - Fixed a bug where it was possible to select soft-deleted tax categories. - Fixed a PHP error that occurred when sending an email with a missing PDF filename format. ([#3309](https://github.com/craftcms/commerce/issues/3309)) - Product GQL queries now support `promotable`, `availableForPurchase`, `freeShipping`, `defaultSku`, `defaultPrice`, `defaultVariant`, `defaultHeight`, `defaultLength`, `defaultWidth`, and `defaultWeight` params. ([#3307](https://github.com/craftcms/commerce/pull/3307)) +- Fixed a PHP error that occurred when viewing trashed orders. ([#3308](https://github.com/craftcms/commerce/issues/3308)) - Fixed an incorrect validation error that occurred when saving a Shipping Zone. ([#3317](https://github.com/craftcms/commerce/issues/3317)) ## 4.3.1 - 2023-10-18 diff --git a/src/elements/Order.php b/src/elements/Order.php index 16e6308328..d8a544278b 100644 --- a/src/elements/Order.php +++ b/src/elements/Order.php @@ -2887,7 +2887,11 @@ public function getShippingAddress(): ?AddressElement { if (!isset($this->_shippingAddress) && $this->shippingAddressId) { /** @var AddressElement|null $address */ - $address = AddressElement::find()->ownerId($this->id)->id($this->shippingAddressId)->one(); + $address = AddressElement::find() + ->owner($this) + ->id($this->shippingAddressId) + ->one(); + $this->_shippingAddress = $address; } @@ -2981,7 +2985,11 @@ public function getBillingAddress(): ?AddressElement { if (!isset($this->_billingAddress) && $this->billingAddressId) { /** @var AddressElement|null $address */ - $address = AddressElement::find()->ownerId($this->id)->id($this->billingAddressId)->one(); + $address = AddressElement::find() + ->owner($this) + ->id($this->billingAddressId) + ->one(); + $this->_billingAddress = $address; } diff --git a/src/services/Orders.php b/src/services/Orders.php index 26a6dd418e..313d335339 100644 --- a/src/services/Orders.php +++ b/src/services/Orders.php @@ -146,16 +146,32 @@ public function eagerLoadAddressesForOrders(array $orders): array $billingAddressIds = array_filter(ArrayHelper::getColumn($orders, 'billingAddressId')); $ids = array_unique(array_merge($shippingAddressIds, $billingAddressIds)); - /** @var Address[] $addresses */ - $addresses = Address::find()->id($ids)->indexBy('id')->all(); + // Query addresses as array to avoid instantiating elements immediately + $query = Address::find() + ->id($ids) + ->indexBy('id') + ->asArray(); + /** @var array $addresses */ + $addresses = $query->all(); foreach ($orders as $key => $order) { if (isset($order['shippingAddressId'], $addresses[$order['shippingAddressId']])) { - $order->setShippingAddress($addresses[$order['shippingAddressId']]); + $data = $addresses[$order['shippingAddressId']]; + $data['owner'] = $order; + /** @var Address $address */ + $address = $query->createElement($data); + + $order->setShippingAddress($address); } if (isset($order['billingAddressId'], $addresses[$order['billingAddressId']])) { - $order->setBillingAddress($addresses[$order['billingAddressId']]); + $data = $addresses[$order['billingAddressId']]; + $data['owner'] = $order; + + /** @var Address $address */ + $address = $query->createElement($data); + + $order->setBillingAddress($address); } $orders[$key] = $order;