Skip to content

Commit

Permalink
Merge pull request #3316 from craftcms/bugfix/3308-trashed-orders-ind…
Browse files Browse the repository at this point in the history
…ex-error

Fixed error when viewing trashed order index
  • Loading branch information
lukeholder authored Oct 31, 2023
2 parents 9591d9d + 4064390 commit 4496ad1
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 10 additions & 2 deletions src/elements/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down
24 changes: 20 additions & 4 deletions src/services/Orders.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 4496ad1

Please sign in to comment.