Skip to content

Commit

Permalink
Merge branch 'feature/5.x-purgeevent' into 5.3
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeholder committed Dec 11, 2024
2 parents 87db3ad + 8d538c5 commit ff35432
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
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

0 comments on commit ff35432

Please sign in to comment.