Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.x] Improve product variant's query performance #3767

Merged
merged 3 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased

- Improved the performance of `Product::getVariants()`. ([#3578](https://github.com/craftcms/commerce/issues/3758))
- Fixed a SQL error that could occur when creating a variant. ([#3763](https://github.com/craftcms/commerce/issues/))

## 5.2.3 - 2024-11-13
Expand Down
15 changes: 15 additions & 0 deletions src/elements/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -1045,6 +1045,21 @@ public function getVariants(bool $includeDisabled = false): VariantCollection
}

$this->_variants = self::createVariantQuery($this)->status(null)->collect();
$this->_variants->map(function(Variant $v) {
if (!$this->id) {
return $v;
}

if ($v->primaryOwnerId === $this->id) {
$v->setPrimaryOwner($this);
}

if ($v->ownerId === $this->id) {
$v->setOwner($this);
}

return $v;
});
}

return $this->_variants->filter(fn(Variant $variant) => $includeDisabled || ($variant->getStatus() === self::STATUS_ENABLED));
Expand Down
55 changes: 53 additions & 2 deletions src/elements/Variant.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@
* @property-read string $gqlTypeName
* @property-read string $skuAsText
* @property string $salePriceAsCurrency
* @method Product|null getOwner()
* @method Product|null getPrimaryOwner()
* @author Pixel & Tonic, Inc. <[email protected]>
* @since 2.0
*/
Expand Down Expand Up @@ -524,6 +522,59 @@ public function setOwner(?ElementInterface $owner): void
$this->traitSetOwner($owner);
}

/**
* @inheritdoc
* @TODO remove implementation when `NestedElementTrait::getOwner()` is updated
*/
public function getPrimaryOwner(): ?Product
{
if (!isset($this->_primaryOwner)) {
$primaryOwnerId = $this->getPrimaryOwnerId();
if (!$primaryOwnerId) {
return null;
}

$this->_primaryOwner = Craft::$app->getElements()->getElementById($primaryOwnerId, Product::class, $this->siteId, [
'trashed' => null,
]) ?? false;
if (!$this->_primaryOwner) {
throw new InvalidConfigException("Invalid owner ID: $primaryOwnerId");
}
}

/** @phpstan-ignore-next-line */
return $this->_primaryOwner ?: null;
}

/**
* @inheritdoc
* @TODO remove implementation when `NestedElementTrait::getOwner()` is updated
*/
public function getOwner(): ?Product
{
if (!isset($this->_owner)) {
$ownerId = $this->getOwnerId();
if (!$ownerId) {
return null;
}

// If ownerId and primaryOwnerId are the same, return the primary owner
if ($ownerId === $this->getPrimaryOwnerId()) {
return $this->getPrimaryOwner();
}

$this->_owner = Craft::$app->getElements()->getElementById($ownerId, Product::class, $this->siteId, [
'trashed' => null,
]) ?? false;
if (!$this->_owner) {
throw new InvalidConfigException("Invalid owner ID: $ownerId");
}
}

/** @phpstan-ignore-next-line */
return $this->_owner ?: null;
}

/**
* Returns the product associated with this variant.
*
Expand Down
Loading