craftcms / cms

Build bespoke content experiences with Craft.
https://craftcms.com
Other
3.28k stars 635 forks source link

[4.x]: Purging of pending users that have orders fails GC with error #13490

Closed lukeholder closed 1 year ago

lukeholder commented 1 year ago

What happened?

Description

In Commerce we allow guests to register on order completion which creates the user as pending and send the activation email to them.

This means we have pending users with orders. Thus when running garbage collection, the purging of pending users will fail midway through on the first purgeable user with an order with the following error:

$ php craft gc
Delete all trashed items? (yes|no) [no]:yes
Running garbage collection ...
    > purging unsaved drafts that have gone stale ... done
    > purging pending users with stale activation codes ... Exception 'yii\base\UserException' with message '%Unable to delete user 4814: the user has a Craft Commerce order.%'

in /var/www/html/packages/commerce4/src/services/Orders.php:179

Stack trace:
#0 [internal function]: craft\commerce\services\Orders->beforeDeleteUserHandler(Object(craft\events\ModelEvent))
#1 /var/www/html/vendor-root/yiisoft/yii2/base/Event.php(312): call_user_func(Array, Object(craft\events\ModelEvent))
#2 /var/www/html/vendor-root/yiisoft/yii2/base/Component.php(642): yii\base\Event::trigger('craft\\elements\\...', 'beforeDelete', Object(craft\events\ModelEvent))
#3 /var/www/html/packages/cms4/src/base/Element.php(5023): yii\base\Component->trigger('beforeDelete', Object(craft\events\ModelEvent))
#4 /var/www/html/packages/cms4/src/elements/User.php(1794): craft\base\Element->beforeDelete()
...

Related:
https://github.com/craftcms/cms/issues/5095

See https://github.com/craftcms/commerce/issues/3239

Craft CMS version

4.x

PHP version

Any

Operating system and version

No response

Database type and version

No response

Image driver and version

No response

Installed plugins and versions

-

i-just commented 1 year ago

Thanks, Luke! PR raised.

i-just commented 1 year ago

This was released with Craft 4.4.17.