Skip to content

Commit 02c90e4

Browse files
authored
Merge pull request #4162 from craftcms/nathaniel/com-479-5x-resaving-products-queue-job-for-46k-products-but-there
[5.x] Improve adding of pricing catalog jobs on product and purchasable save
2 parents 8ea0e21 + 225bda6 commit 02c90e4

File tree

5 files changed

+21
-11
lines changed

5 files changed

+21
-11
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Release Notes for Craft Commerce
22

3+
## Unreleased
4+
5+
- Fixed a bug where duplicate pricing catalog jobs were being queued. ([#4136](https://github.com/craftcms/commerce/issues/4136))
6+
- Deprecated `craft\commerce\services\CatalogPricing::afterSavePurchasableHandler()`.
7+
38
## 5.4.9 - 2025-10-29
49

510
- Fixed a bug where line items could display values in the wrong currency on Edit Order pages. ([#4147](https://github.com/craftcms/commerce/issues/4147))

src/Plugin.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -847,7 +847,6 @@ function(DefineBehaviorsEvent $event) {
847847
});
848848

849849
Event::on(Purchasable::class, Elements::EVENT_BEFORE_RESTORE_ELEMENT, [$this->getPurchasables(), 'beforeRestorePurchasableHandler']);
850-
Event::on(Purchasable::class, Purchasable::EVENT_AFTER_SAVE, [$this->getCatalogPricing(), 'afterSavePurchasableHandler']);
851850

852851
Event::on(Elements::class, Elements::EVENT_AUTHORIZE_VIEW, [$this->getStoreSettings(), 'authorizeStoreLocationView']);
853852
Event::on(Elements::class, Elements::EVENT_AUTHORIZE_SAVE, [$this->getStoreSettings(), 'authorizeStoreLocationEdit']);

src/base/Purchasable.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1218,10 +1218,12 @@ public function afterPropagate(bool $isNew): void
12181218
{
12191219
parent::afterPropagate($isNew);
12201220

1221-
Plugin::getInstance()->getCatalogPricing()->createCatalogPricingJob([
1222-
'purchasableIds' => [$this->getCanonicalId()],
1223-
'storeId' => $this->getStoreId(),
1224-
]);
1221+
if (!$this->getIsDraft() && !$this->getIsRevision()) {
1222+
Plugin::getInstance()->getCatalogPricing()->createCatalogPricingJob([
1223+
'purchasableIds' => [$this->getCanonicalId()],
1224+
'storeId' => $this->getStoreId(),
1225+
]);
1226+
}
12251227
}
12261228

12271229
/**

src/elements/Product.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2041,10 +2041,12 @@ public function afterPropagate(bool $isNew): void
20412041
parent::afterPropagate($isNew);
20422042

20432043
// @TODO improve performance by collating all purchasable IDs updated during request
2044-
Plugin::getInstance()->getCatalogPricing()->createCatalogPricingJob([
2045-
'purchasableIds' => $this->getVariants()->pluck('id')->all(),
2046-
'storeId' => $this->storeId,
2047-
]);
2044+
if (!$this->getIsDraft()) {
2045+
Plugin::getInstance()->getCatalogPricing()->createCatalogPricingJob([
2046+
'purchasableIds' => $this->getVariants()->pluck('id')->all(),
2047+
'storeId' => $this->storeId,
2048+
]);
2049+
}
20482050

20492051
// Save a new revision?
20502052
if ($this->_shouldSaveRevision()) {

src/services/CatalogPricing.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -477,14 +477,16 @@ private function _createCatalogPricesQuery(int $storeId, ?CatalogPricingConditio
477477
* @return void
478478
* @throws InvalidConfigException
479479
* @since 5.0.0
480+
* @deprecated in 5.5.0
480481
*/
481482
public function afterSavePurchasableHandler(ModelEvent $event): void
482483
{
483-
if (!$event->sender instanceof Purchasable || $event->sender->propagating) {
484+
$purchasable = $event->sender;
485+
if (!$purchasable instanceof Purchasable || $purchasable->propagating || $purchasable->getIsDraft() || $purchasable->getIsRevision()) {
484486
return;
485487
}
486488

487-
$this->createCatalogPricingJob(['purchasableIds' => [$event->sender->id]]);
489+
$this->createCatalogPricingJob(['purchasableIds' => [$purchasable->id], 'storeId' => $purchasable->storeId]);
488490
}
489491

490492
/**

0 commit comments

Comments
 (0)