Skip to content

Commit

Permalink
Improve variant query performance #3758
Browse files Browse the repository at this point in the history
  • Loading branch information
nfourtythree committed Nov 26, 2024
1 parent e598c90 commit a02a5d3
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

## Unreleased

- Fixed a bug where prices could display incorrectly when inline editing a variant. ([#3768](https://github.com/craftcms/commerce/issues/3768))
- Fixed a bug where variant prices could display incorrectly when inline editing. ([#3768](https://github.com/craftcms/commerce/issues/3768))
- Fixed a performance degradation bug with variant queries. ([#3758](https://github.com/craftcms/commerce/issues/3758))

## 5.2.5 - 2024-11-20

Expand Down
4 changes: 2 additions & 2 deletions src/elements/db/PurchasableQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ protected function afterPrepare(): bool
$this->subQuery->leftJoin(['purchasables_stores' => Table::PURCHASABLES_STORES], '[[purchasables_stores.storeId]] = [[sitestores.storeId]] AND [[purchasables_stores.purchasableId]] = [[commerce_purchasables.id]]');

$this->subQuery->leftJoin(['catalogprices' => $catalogPricesQuery], '[[catalogprices.purchasableId]] = [[commerce_purchasables.id]] AND [[catalogprices.storeId]] = [[sitestores.storeId]]');
$this->subQuery->leftJoin(['inventoryitems' => Table::INVENTORYITEMS], '[[inventoryitems.purchasableId]] = [[commerce_purchasables.id]] OR [[inventoryitems.purchasableId]] = [[elements.canonicalId]]');
$this->subQuery->leftJoin(['inventoryitems' => Table::INVENTORYITEMS], '[[inventoryitems.purchasableId]] = [[commerce_purchasables.id]]');

return parent::afterPrepare();
}
Expand Down Expand Up @@ -705,7 +705,7 @@ protected function beforePrepare(): bool

$this->query->leftJoin(Table::SITESTORES . ' sitestores', '[[elements_sites.siteId]] = [[sitestores.siteId]]');
$this->query->leftJoin(Table::PURCHASABLES_STORES . ' purchasables_stores', '[[purchasables_stores.storeId]] = [[sitestores.storeId]] AND [[purchasables_stores.purchasableId]] = [[commerce_purchasables.id]]');
$this->query->leftJoin(['inventoryitems' => Table::INVENTORYITEMS], '[[inventoryitems.purchasableId]] = [[commerce_purchasables.id]] OR [[inventoryitems.purchasableId]] = [[elements.canonicalId]]');
$this->query->leftJoin(['inventoryitems' => Table::INVENTORYITEMS], '[[inventoryitems.purchasableId]] = [[commerce_purchasables.id]]');

$this->subQuery->addSelect([
'catalogprices.price',
Expand Down
6 changes: 6 additions & 0 deletions src/fieldlayoutelements/PurchasableStockField.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ class PurchasableStockField extends BaseNativeField
*/
public function inputHtml(ElementInterface $element = null, bool $static = false): ?string
{
// If this is a revision get the canonical element to show the stock for.
// @TODO re-evaluate this when we have a better way to handle revisions and inventory.
if ($element->getIsRevision()) {
$element = $element->getCanonical();
}

$view = Craft::$app->getView();
$view->registerAssetBundle(InventoryAsset::class);

Expand Down

0 comments on commit a02a5d3

Please sign in to comment.