From 4e2f1b4c5f404c5bb76e2784dfcf2451a7effe59 Mon Sep 17 00:00:00 2001 From: Nathaniel Hammond Date: Wed, 13 Nov 2024 16:32:07 +0000 Subject: [PATCH] Use queue event to prevent catalog pricing job from running mid import --- src/elements/CommerceProduct.php | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/elements/CommerceProduct.php b/src/elements/CommerceProduct.php index dd58176c..91186c11 100644 --- a/src/elements/CommerceProduct.php +++ b/src/elements/CommerceProduct.php @@ -13,6 +13,7 @@ use craft\commerce\models\inventory\UpdateInventoryLevel; use craft\commerce\models\InventoryLevel; use craft\commerce\Plugin as Commerce; +use craft\commerce\queue\jobs\CatalogPricing; use craft\db\Query; use craft\feedme\base\Element; use craft\feedme\events\FeedProcessEvent; @@ -28,6 +29,8 @@ use DateTime; use Exception; use yii\base\Event; +use yii\queue\PushEvent; +use yii\queue\Queue; /** * @@ -52,6 +55,12 @@ class CommerceProduct extends Element */ public static string $class = ProductElement::class; + /** + * @var bool + * @since x.x.x + */ + private bool $_runCatalogPricingJob = false; + // Templates // ========================================================================= @@ -95,7 +104,7 @@ public function init(): void $this->_checkForVariantMatches($event); } }); - + Event::on(Process::class, Process::EVENT_STEP_BEFORE_PARSE_CONTENT, function(FeedProcessEvent $event) { if ($event->feed['elementType'] === ProductElement::class) { // at this point we've matched existing elements; @@ -126,6 +135,19 @@ public function init(): void $this->_inventoryUpdate($event); } }); + + // While imports are happening don't process any catalog pricing jobs + Event::on(Queue::class, Queue::EVENT_BEFORE_PUSH, function(PushEvent $event) { + if ($event->job instanceof CatalogPricing::class && !$this->_runCatalogPricingJob) { + $event->handled = true; + } + }); + + // After the feed has run, create a catalog pricing job to update the pricing + Event::on(Process::class, Process::EVENT_AFTER_PROCESS_FEED, function(FeedProcessEvent $event) { + $this->_runCatalogPricingJob = true; + Commerce::getInstance()->getCatalogPricing()->createCatalogPricingJob(); + }); } /**