diff --git a/CHANGELOG.md b/CHANGELOG.md index 06336a357b..ccdc2c1d23 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ## 4.3.0 - Unreleased +- It’s now possible to modify the purchasables shown in the add line item table on the Edit Order page. ([#3194](https://github.com/craftcms/commerce/issues/3194)) +- Added `craft\commerce\events\ModifyPurchasablesQueryEvent`. +- Added `craft\commerce\controllers\OrdersController::EVENT_MODIFY_PURCHASABLES_QUERY`. - Guest customers registering during checkout now have their addresses saved to their account. - Deprecated `craft\commerce\elements\Order::setEmail()`. `Order::setCustomer()` should be used instead. diff --git a/src/controllers/OrdersController.php b/src/controllers/OrdersController.php index 819c077ee8..9c6a78d3af 100644 --- a/src/controllers/OrdersController.php +++ b/src/controllers/OrdersController.php @@ -18,6 +18,7 @@ use craft\commerce\errors\OrderStatusException; use craft\commerce\errors\RefundException; use craft\commerce\errors\TransactionException; +use craft\commerce\events\ModifyPurchasablesTableQueryEvent; use craft\commerce\gateways\MissingGateway; use craft\commerce\helpers\Currency; use craft\commerce\helpers\DebugPanel; @@ -72,6 +73,27 @@ */ class OrdersController extends Controller { + /** + * @event Event The event that’s triggered when retrieving the purchasables for the add line item table on the order edit page. + * @since 4.3.0 + * + * --- + * ```php + * use craft\commerce\controllers\OrdersController; + * use craft\commerce\events\ModifyPurchasablesQueryEvent; + * use yii\base\Event; + * + * Event::on( + * OrdersController::class, + * OrdersController::EVENT_MODIFY_PURCHASABLES_TABLE_QUERY, + * function(ModifyCartInfoEvent $e) { + * $e->query->andWhere(['sku' => 'foo']); + * } + * ); + * ``` + */ + public const EVENT_MODIFY_PURCHASABLES_TABLE_QUERY = 'modifyPurchasablesTableQuery'; + /** * @throws HttpException * @throws InvalidConfigException @@ -542,10 +564,21 @@ public function actionPurchasablesTable(): Response $sqlQuery->orderBy(['id' => 'asc']); } + // Trigger event before working out the total and limiting the results for pagination + if ($this->hasEventHandlers(self::EVENT_MODIFY_PURCHASABLES_TABLE_QUERY)) { + $event = new ModifyPurchasablesTableQueryEvent([ + 'query' => $sqlQuery, + 'search' => $search, + ]); + $this->trigger(self::EVENT_MODIFY_PURCHASABLES_TABLE_QUERY, $event); + $sqlQuery = $event->query; + } + $total = $sqlQuery->count(); $sqlQuery->limit($limit); $sqlQuery->offset($offset); + $result = $sqlQuery->all(); $purchasables = $this->_addLivePurchasableInfo($result); diff --git a/src/events/ModifyPurchasablesTableQueryEvent.php b/src/events/ModifyPurchasablesTableQueryEvent.php new file mode 100644 index 0000000000..723d36b22b --- /dev/null +++ b/src/events/ModifyPurchasablesTableQueryEvent.php @@ -0,0 +1,30 @@ + + * @since 4.3.0 + */ +class ModifyPurchasablesTableQueryEvent extends Event +{ + /** + * @var Query + */ + public Query $query; + + /** + * @var string|null The search term that is being used in the query, if any + */ + public ?string $search = null; +}