Skip to content

Commit

Permalink
Fixed missing cart purge event trigger
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeholder committed Jan 31, 2025
1 parent 9040973 commit 7cc1a2f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Release Notes for Craft Commerce

## Unreleased

- Fixed a bug where the purge carts event wasn’t being triggered.
- Added `craft\commerce\services\Carts::EVENT_BEFORE_PURGE_INACTIVE_CARTS`.

## 5.3.0 - 2025-01-30

### Store Management
Expand Down
4 changes: 2 additions & 2 deletions src/events/CartPurgeEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
use craft\events\CancelableEvent;

/**
* Class CartEvent
* Class CartPurgeEvent
*
* @author Pixel & Tonic, Inc. <[email protected]>
* @since 2.0
* @since 5.3
*/
class CartPurgeEvent extends CancelableEvent
{
Expand Down
26 changes: 26 additions & 0 deletions src/services/Carts.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,28 @@
*/
class Carts extends Component
{
/**
* @event CartPurgeEvent The event that is triggered before the carts are purged.
*
* This example modifies the query to only purge carts with a total price of 0.
* You can also set the `isValid` property to `false` to prevent the carts from being purged.
*
* ```php
* use craft\commerce\events\CartPurgeEvent;
* use craft\commerce\services\Carts;
* use yii\base\Event;
*
* Event::on(
* Carts::class,
* Carts::EVENT_BEFORE_PURGE_INACTIVE_CARTS,
* function(CartPurgeEvent $event) {
* $event->inactiveCartsQuery = $event->inactiveCartsQuery->andWhere(['totalPrice' => 0]);
* }
* );
* ```
*/
public const EVENT_BEFORE_PURGE_INACTIVE_CARTS = 'beforePurgeInactiveCarts';

/**
* @var array The configuration of the cart cookie.
* @since 4.0.0
Expand Down Expand Up @@ -423,6 +445,10 @@ public function purgeIncompleteCarts(): int
'inactiveCartsQuery' => $cartIdsQuery,
]);

if ($this->hasEventHandlers(self::EVENT_BEFORE_PURGE_INACTIVE_CARTS)) {
$this->trigger(self::EVENT_BEFORE_PURGE_INACTIVE_CARTS, $event);
}

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

0 comments on commit 7cc1a2f

Please sign in to comment.