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

[5.3] Add cart purge event #3749

Merged
merged 6 commits into from
Dec 11, 2024
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
1 change: 1 addition & 0 deletions CHANGELOG-WIP.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
- Added `craft\commerce\elements\conditions\variants\ProductConditionRule`.
- Added `craft\commerce\elements\db\OrderQuery::$couponCode`.
- Added `craft\commerce\elements\db\OrderQuery::couponCode()`.
- Added `craft\commerce\events\CartPurgeEvent`.
- Added `craft\commerce\services\Inventory::updateInventoryLevel()`.
- Added `craft\commerce\services\Inventory::updatePurchasableInventoryLevel()`.

Expand Down
26 changes: 26 additions & 0 deletions src/events/CartPurgeEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/

namespace craft\commerce\events;

use craft\commerce\elements\Order;
use craft\db\Query;
use craft\events\CancelableEvent;

/**
* Class CartEvent
*
* @author Pixel & Tonic, Inc. <[email protected]>
* @since 2.0
*/
class CartPurgeEvent extends CancelableEvent
{
/**
* @var Query The query that identifies the order IDs to be purged.
*/
public Query $inactiveCartsQuery;
}
15 changes: 12 additions & 3 deletions src/services/Carts.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Craft;
use craft\commerce\db\Table;
use craft\commerce\elements\Order;
use craft\commerce\events\CartPurgeEvent;
use craft\commerce\Plugin;
use craft\db\Query;
use craft\errors\ElementNotFoundException;
Expand Down Expand Up @@ -405,7 +406,7 @@ public function purgeIncompleteCarts(): int
{
if (!Plugin::getInstance()->getSettings()->purgeInactiveCarts) {
return 0;
}
};

$configInterval = ConfigHelper::durationInSeconds(Plugin::getInstance()->getSettings()->purgeInactiveCartsDuration);
$edge = new DateTime();
Expand All @@ -418,16 +419,24 @@ public function purgeIncompleteCarts(): int
->andWhere('[[orders.dateUpdated]] <= :edge', ['edge' => Db::prepareDateForDb($edge)])
->from(['orders' => Table::ORDERS]);

$event = new CartPurgeEvent([
'inactiveCartsQuery' => $cartIdsQuery,
]);

if (!$event->isValid) {
return 0;
}

// Taken from craft\services\Elements::deleteElement(); Using the method directly
// takes too many resources since it retrieves the order before deleting it.
// Delete the elements table rows, which will cascade across all other InnoDB tables
Craft::$app->getDb()->createCommand()
->delete('{{%elements}}', ['id' => $cartIdsQuery])
->delete('{{%elements}}', ['id' => $event->inactiveCartsQuery])
->execute();

// The searchindex table is probably MyISAM, though
Craft::$app->getDb()->createCommand()
->delete('{{%searchindex}}', ['elementId' => $cartIdsQuery])
->delete('{{%searchindex}}', ['elementId' => $event->inactiveCartsQuery])
->execute();

return $cartIdsQuery->count();
Expand Down
Loading