Skip to content

Commit

Permalink
Merge pull request #3198 from craftcms/feature/purchasables-order-edi…
Browse files Browse the repository at this point in the history
…t-event

Add `ModifyPurchasablesQueryEvent`
  • Loading branch information
lukeholder authored Jul 17, 2023
2 parents 8468b5d + bc4cf9a commit a189f18
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
33 changes: 33 additions & 0 deletions src/controllers/OrdersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
30 changes: 30 additions & 0 deletions src/events/ModifyPurchasablesTableQueryEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/

namespace craft\commerce\events;

use craft\db\Query;
use yii\base\Event;

/**
* ModifyPurchasablesTableQueryEvent class.
*
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @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;
}

0 comments on commit a189f18

Please sign in to comment.