Skip to content

Commit

Permalink
Merge pull request #1547 from craftcms/feature/commerce-pause-catalog…
Browse files Browse the repository at this point in the history
…-pricing-job-during-import

Run catalog pricing generation after Commerce products have been imported
  • Loading branch information
angrybrad authored Nov 15, 2024
2 parents c627253 + 2509ec2 commit d61cefd
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 && !$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 d61cefd

Please sign in to comment.