Skip to content

Commit

Permalink
Merge pull request #3760 from craftcms/feature/pricing-catalog-event
Browse files Browse the repository at this point in the history
New pricing catalog generation event
  • Loading branch information
lukeholder authored Nov 13, 2024
2 parents d13b99f + 55a4965 commit 7ae858e
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
34 changes: 34 additions & 0 deletions src/events/CatalogPricingJobEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/

namespace craft\commerce\events;

use craft\events\CancelableEvent;

/**
* Class CreateSubscriptionEvent
*
* @author Pixel & Tonic, Inc. <[email protected]>
* @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;
}
22 changes: 21 additions & 1 deletion src/services/CatalogPricing.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -40,6 +41,11 @@
*/
class CatalogPricing extends Component
{
/**
* @since 5.3.0
*/
public const EVENT_BEFORE_CREATE_CATALOG_PRICING_JOB = 'beforeCreateCatalogPricingJob';

/**
* @var array|null
*/
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 7ae858e

Please sign in to comment.