Skip to content

Commit

Permalink
Fixed PHP error when eager loading variant owner
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeholder committed Dec 17, 2024
1 parent d2440a2 commit 5eacab1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Release Notes for Craft Commerce

## Unreleased

- Fixed a PHP error when eager loading a variant’s owner. ([#3817](https://github.com/craftcms/commerce/issues/3817))

## 5.2.9.1 - 2024-12-13

- Fixed a bug where line item promotional prices weren’t updated when upgrading to Commerce 5.
Expand Down
10 changes: 7 additions & 3 deletions src/elements/Variant.php
Original file line number Diff line number Diff line change
Expand Up @@ -887,7 +887,7 @@ public static function hasStatuses(): bool
*/
public static function eagerLoadingMap(array $sourceElements, string $handle): array|null|false
{
if ($handle == 'product') {
if (in_array($handle, ['product', 'owner', 'primaryOwner'])) {
// Get the source element IDs
$sourceElementIds = [];

Expand Down Expand Up @@ -1093,10 +1093,14 @@ public function afterSave(bool $isNew): void
*/
public function setEagerLoadedElements(string $handle, array $elements, EagerLoadPlan $plan): void
{
if ($handle == 'product') {
if (in_array($handle, ['product', 'owner', 'primaryOwner'])) {
$product = $elements[0] ?? null;
if ($product instanceof Product) {
$this->setOwner($product);
if ($handle == 'primaryOwner') {
$this->setPrimaryOwner($product);
} else {
$this->setOwner($product);
}
}
} else {
$this->traitSetEagerLoadedElements($handle, $elements, $plan);
Expand Down

0 comments on commit 5eacab1

Please sign in to comment.