|
60 | 60 | * @property-read string $gqlTypeName
|
61 | 61 | * @property-read string $skuAsText
|
62 | 62 | * @property string $salePriceAsCurrency
|
63 |
| - * @method Product|null getOwner() |
64 |
| - * @method Product|null getPrimaryOwner() |
65 | 63 | * @author Pixel & Tonic, Inc. <[email protected]>
|
66 | 64 | * @since 2.0
|
67 | 65 | */
|
@@ -524,6 +522,59 @@ public function setOwner(?ElementInterface $owner): void
|
524 | 522 | $this->traitSetOwner($owner);
|
525 | 523 | }
|
526 | 524 |
|
| 525 | + /** |
| 526 | + * @inheritdoc |
| 527 | + * @TODO remove implementation when `NestedElementTrait::getOwner()` is updated |
| 528 | + */ |
| 529 | + public function getPrimaryOwner(): ?Product |
| 530 | + { |
| 531 | + if (!isset($this->_primaryOwner)) { |
| 532 | + $primaryOwnerId = $this->getPrimaryOwnerId(); |
| 533 | + if (!$primaryOwnerId) { |
| 534 | + return null; |
| 535 | + } |
| 536 | + |
| 537 | + $this->_primaryOwner = Craft::$app->getElements()->getElementById($primaryOwnerId, Product::class, $this->siteId, [ |
| 538 | + 'trashed' => null, |
| 539 | + ]) ?? false; |
| 540 | + if (!$this->_primaryOwner) { |
| 541 | + throw new InvalidConfigException("Invalid owner ID: $primaryOwnerId"); |
| 542 | + } |
| 543 | + } |
| 544 | + |
| 545 | + /** @phpstan-ignore-next-line */ |
| 546 | + return $this->_primaryOwner ?: null; |
| 547 | + } |
| 548 | + |
| 549 | + /** |
| 550 | + * @inheritdoc |
| 551 | + * @TODO remove implementation when `NestedElementTrait::getOwner()` is updated |
| 552 | + */ |
| 553 | + public function getOwner(): ?Product |
| 554 | + { |
| 555 | + if (!isset($this->_owner)) { |
| 556 | + $ownerId = $this->getOwnerId(); |
| 557 | + if (!$ownerId) { |
| 558 | + return null; |
| 559 | + } |
| 560 | + |
| 561 | + // If ownerId and primaryOwnerId are the same, return the primary owner |
| 562 | + if ($ownerId === $this->getPrimaryOwnerId()) { |
| 563 | + return $this->getPrimaryOwner(); |
| 564 | + } |
| 565 | + |
| 566 | + $this->_owner = Craft::$app->getElements()->getElementById($ownerId, Product::class, $this->siteId, [ |
| 567 | + 'trashed' => null, |
| 568 | + ]) ?? false; |
| 569 | + if (!$this->_owner) { |
| 570 | + throw new InvalidConfigException("Invalid owner ID: $ownerId"); |
| 571 | + } |
| 572 | + } |
| 573 | + |
| 574 | + /** @phpstan-ignore-next-line */ |
| 575 | + return $this->_owner ?: null; |
| 576 | + } |
| 577 | + |
527 | 578 | /**
|
528 | 579 | * Returns the product associated with this variant.
|
529 | 580 | *
|
|
0 commit comments