Skip to content

Commit a46c16d

Browse files
committed
#4175 Event once causing multiple table aliases to be added
1 parent f788325 commit a46c16d

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
## Unreleased
44

5+
- Added the missing Preview Targets UI to product type settings. ([#4127](https://github.com/craftcms/commerce/issues/4127))
56
- Fixed a bug where tax and shipping categories weren't getting saved on the Edit variant screen. ([#4180](https://github.com/craftcms/commerce/issues/4180))
67
- Fixed a bug where newly created variants weren’t visible on Edit product screens.
78
- Fixed a SQL error that could occur when viewing product indexes.
89
- Fixed a PHP error that occurred when applying project config changes after updating. ([#4185](https://github.com/craftcms/commerce/issues/4185))
910
- Fixed a bug where an Order’s origin was set incorrectly when creating an order in the control panel.
10-
- Added the missing Preview Targets UI to product type settings. ([#4127](https://github.com/craftcms/commerce/issues/4127))
1111
- Fixed a bug where prices weren’t showing formatted per the user’s formatting locale, in payment modals on Edit Order pages.
12+
- Fixed a SQL error that could occur when generating the pricing catalog. ([#4175](https://github.com/craftcms/commerce/issues/4175))
13+
- Added `craft\commerce\models\CatalogPricingRule::afterPreparePurchasableQuery()`.
1214

1315
## 5.5.0.1 - 2025-11-24
1416

src/models/CatalogPricingRule.php

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -322,23 +322,16 @@ public function getPurchasableIds(): ?array
322322
}
323323

324324
// We are unable to use `siteId()` on the purchasable query as it is only the subquery part that is used.
325-
Event::once(ElementQuery::class, ElementQuery::EVENT_AFTER_PREPARE, function(CancelableEvent $event) use ($siteIds) {
326-
foreach ($event->sender->subQuery->where as &$value) {
327-
if (is_array($value) && isset($value['elements_sites.siteId'])) {
328-
$value['elements_sites.siteId'] = $siteIds;
329-
}
330-
}
331-
332-
$event->sender->subQuery->join[] = ['LEFT JOIN', ['sitestores' => Table::SITESTORES], '[[elements_sites.siteId]] = [[sitestores.siteId]]'];
333-
$event->sender->subQuery->join[] = ['LEFT JOIN', ['purchasables_stores' => Table::PURCHASABLES_STORES], '[[purchasables_stores.storeId]] = [[sitestores.storeId]] AND [[purchasables_stores.purchasableId]] = [[elements.id]]'];
334-
});
335325

336326
// If the rule is generating a promotional price, we need to make sure the purchasable is promotable
337327
if ($this->isPromotionalPrice) {
338328
$purchasableQuery->andWhere(Db::parseBooleanParam('purchasables_stores.promotable', true));
339329
}
340330

331+
// Do this adjustment to the query once (was previously using `Event::once` but this caused issues in some edge cases)
332+
$purchasableQuery->on(ElementQuery::EVENT_AFTER_PREPARE, [$this, 'afterPreparePurchasableQuery'], ['siteIds' => $siteIds]);
341333
$this->_purchasableIds = $purchasableQuery->ids();
334+
$purchasableQuery->off(ElementQuery::EVENT_AFTER_PREPARE, [$this, 'afterPreparePurchasableQuery']);
342335
}
343336

344337
$this->_purchasableIds = $this->_purchasableIds !== null ? array_unique($this->_purchasableIds) : null;
@@ -347,6 +340,23 @@ public function getPurchasableIds(): ?array
347340
return $this->_purchasableIds;
348341
}
349342

343+
/**
344+
* @param CancelableEvent $event
345+
* @return void
346+
* @since 5.5.1
347+
*/
348+
public function afterPreparePurchasableQuery(CancelableEvent $event): void
349+
{
350+
foreach ($event->sender->subQuery->where as &$value) {
351+
if (is_array($value) && isset($value['elements_sites.siteId'])) {
352+
$value['elements_sites.siteId'] = $event->data['siteIds'];
353+
}
354+
}
355+
356+
$event->sender->subQuery->join[] = ['LEFT JOIN', ['sitestores' => Table::SITESTORES], '[[elements_sites.siteId]] = [[sitestores.siteId]]'];
357+
$event->sender->subQuery->join[] = ['LEFT JOIN', ['purchasables_stores' => Table::PURCHASABLES_STORES], '[[purchasables_stores.storeId]] = [[sitestores.storeId]] AND [[purchasables_stores.purchasableId]] = [[elements.id]]'];
358+
}
359+
350360
/**
351361
* @return ElementConditionInterface
352362
*/

0 commit comments

Comments
 (0)