diff --git a/src/events/CatalogPricingJobEvent.php b/src/events/CatalogPricingJobEvent.php new file mode 100644 index 0000000000..8622c263ea --- /dev/null +++ b/src/events/CatalogPricingJobEvent.php @@ -0,0 +1,34 @@ + + * @since 5.3.0 + */ +class CatalogPricingJobEvent extends CancelableEvent +{ + /** + * @var int[]|null + */ + public ?array $purchasableIds = null; + + /** + * @var int[]|null + */ + public ?array $catalogPricingRuleIds = null; + + /** + * @var int[]|null + */ + public ?array $storeId = null; +} diff --git a/src/services/CatalogPricing.php b/src/services/CatalogPricing.php index 4ba3039695..c76f1276ee 100755 --- a/src/services/CatalogPricing.php +++ b/src/services/CatalogPricing.php @@ -12,6 +12,7 @@ use craft\commerce\db\Table; use craft\commerce\elements\conditions\purchasables\CatalogPricingCondition; use craft\commerce\elements\conditions\purchasables\CatalogPricingCustomerConditionRule; +use craft\commerce\events\CatalogPricingJobEvent; use craft\commerce\models\CatalogPricing as CatalogPricingModel; use craft\commerce\models\CatalogPricingRule; use craft\commerce\Plugin; @@ -40,6 +41,11 @@ */ class CatalogPricing extends Component { + /** + * @since 5.3.0 + */ + public const EVENT_BEFORE_CREATE_CATALOG_PRICING_JOB = 'beforeCreateCatalogPricingJob'; + /** * @var array|null */ @@ -500,7 +506,21 @@ public function createCatalogPricingJob(array $config = [], int $priority = 100) $catalogPricingRuleIds = $config['catalogPricingRuleIds'] ?? null; $purchasableIds = $config['purchasableIds'] ?? null; $storeId = $config['storeId'] ?? null; - $this->markPricesAsUpdatePending($catalogPricingRuleIds, $purchasableIds, $storeId); + + /** @var CatalogPricingJobEvent $event */ + $event = Craft::createObject( + CatalogPricingJobEvent::class, compact('catalogPricingRuleIds', 'purchasableIds', 'storeId'), + ); + + if ($this->hasEventHandlers(self::EVENT_BEFORE_CREATE_CATALOG_PRICING_JOB)) { + $this->trigger(self::EVENT_BEFORE_CREATE_CATALOG_PRICING_JOB, $event); + } + + if (!$event->isValid) { + return; + } + + $this->markPricesAsUpdatePending($event->catalogPricingRuleIds, $event->purchasableIds, $event->storeId); $config = array_merge([ 'class' => CatalogPricingJob::class,