From 0b60e605613da51ec395c6601426cf831e3c2098 Mon Sep 17 00:00:00 2001 From: Luke Holder Date: Wed, 6 Nov 2024 17:16:57 +0800 Subject: [PATCH 1/4] Add Cart purge event --- src/events/CartPurgeEvent.php | 27 +++++++++++++++++++++++++++ src/services/Carts.php | 15 ++++++++++++--- 2 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 src/events/CartPurgeEvent.php diff --git a/src/events/CartPurgeEvent.php b/src/events/CartPurgeEvent.php new file mode 100644 index 0000000000..2dfce555f0 --- /dev/null +++ b/src/events/CartPurgeEvent.php @@ -0,0 +1,27 @@ + + * @since 2.0 + */ +class CartPurgeEvent extends CancelableEvent +{ + /** + * @var Query The query that identifies the order IDs to be purged. + */ + public Query $inactiveCartsQuery; +} diff --git a/src/services/Carts.php b/src/services/Carts.php index 2ef74ad12f..2db378adcf 100644 --- a/src/services/Carts.php +++ b/src/services/Carts.php @@ -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; @@ -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(); @@ -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(); From 3c3b05f146b670826ad99c776a54bec435fdb150 Mon Sep 17 00:00:00 2001 From: Luke Holder Date: Wed, 20 Nov 2024 22:00:45 +0800 Subject: [PATCH 2/4] cleanup --- src/events/CartPurgeEvent.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/events/CartPurgeEvent.php b/src/events/CartPurgeEvent.php index 2dfce555f0..3ab4dd2d65 100644 --- a/src/events/CartPurgeEvent.php +++ b/src/events/CartPurgeEvent.php @@ -8,7 +8,6 @@ namespace craft\commerce\events; use craft\commerce\elements\Order; -use craft\commerce\models\LineItem; use craft\db\Query; use craft\events\CancelableEvent; From a1fa1bf3810e1de7578d9550cb449d1ecef7b182 Mon Sep 17 00:00:00 2001 From: Luke Holder Date: Wed, 20 Nov 2024 22:44:36 +0800 Subject: [PATCH 3/4] release notes --- CHANGELOG-WIP.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG-WIP.md b/CHANGELOG-WIP.md index c9e510bf7d..f1f1a72551 100644 --- a/CHANGELOG-WIP.md +++ b/CHANGELOG-WIP.md @@ -2,6 +2,7 @@ ## Administration +- Added `craft\commerce\events\CartPurgeEvent`. - Added support for `to`, `bcc`, and `cc` email fields to support environment variables. ([#3738](https://github.com/craftcms/commerce/issues/3738)) - Added an `originalCart` value to the `commerce/update-cart` failed ajax response. ([#430](https://github.com/craftcms/commerce/issues/430)) - Added a new "Payment Gateway" order condition rule. ([#3722](https://github.com/craftcms/commerce/discussions/3722)) From 8d538c5119be4a1f3a39b8ab84e159923f15a4db Mon Sep 17 00:00:00 2001 From: Luke Holder Date: Wed, 11 Dec 2024 19:44:58 +0800 Subject: [PATCH 4/4] Release notes --- CHANGELOG-WIP.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG-WIP.md b/CHANGELOG-WIP.md index ec7aa2422e..c830b64dc6 100644 --- a/CHANGELOG-WIP.md +++ b/CHANGELOG-WIP.md @@ -6,8 +6,10 @@ - Order conditions can now have a “Payment Gateway” rule. ([#3722](https://github.com/craftcms/commerce/discussions/3722)) - Variant conditions can now have a “Product” rule. -### Development +### Administration - Added support for `to`, `bcc`, and `cc` email fields to support environment variables. ([#3738](https://github.com/craftcms/commerce/issues/3738)) + +### Development - Added the `couponCode` order query param. - Added an `originalCart` value to the `commerce/update-cart` failed ajax response. ([#430](https://github.com/craftcms/commerce/issues/430))