Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ModifyPurchasablesQueryEvent #3198

Merged
merged 6 commits into from
Jul 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

## Unreleased
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;
}
Loading