Skip to content

Commit

Permalink
Use queue event to prevent catalog pricing job from running mid import
Browse files Browse the repository at this point in the history
  • Loading branch information
nfourtythree committed Nov 13, 2024
1 parent c627253 commit 4e2f1b4
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/elements/CommerceProduct.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,6 +29,8 @@
use DateTime;
use Exception;
use yii\base\Event;
use yii\queue\PushEvent;
use yii\queue\Queue;

/**
*
Expand All @@ -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
// =========================================================================

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
});
}

/**
Expand Down

0 comments on commit 4e2f1b4

Please sign in to comment.