commerce
commerce copied to clipboard
[3.x]: deleted product in inactive carts causes exception when retrieving cart on template - No line item exists with the ID “571”
What happened?
Description
Hey, I hope you can help. I'm facing an issue where when products that have been deleted from the CMS are still attached to inactive carts. When a customer returns they experience an exception when their inactive cart is loaded up again.
Update 2022-08-26
I had been reviewing this a bit more and after clearing out my inactive carts it seems to have resolved most of the problems but clearly do not want this to happen again.
I have seen 5 exceptions in past 24 hours despite this on new carts where products again may have been removed.
I may have tracked down the issue! After looking for a particular line item id which it was complaining about 1122 more recently, after reviewing a DB dump for 1122,, I found it as a lineItemId in the orderadjustments table!
`commerce_orderadjustments` (`id`, `orderId`, `lineItemId`,
This particular id still had two corresponding rows for discount & tax.
Do I need to remove any related adjusters on a EVENT_AFTER_REMOVE_LINE_ITEM event? I feel like craft commerce should maybe handle this for me if a line item is removed surely just remove anything with a matching lineitemid?
I realise I had not provided a stack trace which might be helpful, after reviewing it myself, I am not sure how 1122 was added in Order.php, unless I missed it building them up from existing adjusters perhaps?
[2022-08-25T13:50:56.038179+00:00] app.ERROR: yii\base\Exception: No line item exists with the ID “1122” in /var/www/html/cms/vendor/craftcms/commerce/src/services/LineItems.php:236 Stack trace: #0 /var/www/html/cms/vendor/craftcms/commerce/src/elements/Order.php(3391): craft\commerce\services\LineItems->saveLineItem() #1 /var/www/html/cms/vendor/craftcms/commerce/src/elements/Order.php(2068): craft\commerce\elements\Order->_saveLineItems() #2 /var/www/html/cms/vendor/craftcms/cms/src/services/Elements.php(2716): craft\commerce\elements\Order->afterSave() #3 /var/www/html/cms/vendor/craftcms/cms/src/services/Elements.php(785): craft\services\Elements->_saveElementInternal() #4 /var/www/html/cms/vendor/craftcms/commerce/src/services/Carts.php(119): craft\services\Elements->saveElement() #5 /var/www/html/cms/vendor/craftcms/commerce/src/controllers/CartController.php(487): craft\commerce\services\Carts->getCart() #6 /var/www/html/cms/vendor/craftcms/commerce/src/controllers/CartController.php(88): craft\commerce\controllers\CartController->_getCart() #7 [internal function]: craft\commerce\controllers\CartController->actionUpdateCart() #8 /var/www/html/cms/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array() #9 /var/www/html/cms/vendor/yiisoft/yii2/base/Controller.php(178): yii\base\InlineAction->runWithParams() #10 /var/www/html/cms/vendor/yiisoft/yii2/base/Module.php(552): yii\base\Controller->runAction() #11 /var/www/html/cms/vendor/craftcms/cms/src/web/Application.php(293): yii\base\Module->runAction() #12 /var/www/html/cms/vendor/craftcms/cms/src/web/Application.php(602): craft\web\Application->runAction() #13 /var/www/html/cms/vendor/craftcms/cms/src/web/Application.php(272): craft\web\Application->_processActionRequest() #14 /var/www/html/cms/vendor/yiisoft/yii2/base/Application.php(384): craft\web\Application->handleRequest() #15 /var/www/html/cms/web/index.php(28): yii\base\Application->run() #16 {main} {"trace":[],"memory":14680864,"category":"yii\\base\\Exception","timestamp":1661435455.888059,"exception":"[object] (yii\\base\\Exception(code: 0): No line item exists with the ID “1122” at /var/www/html/cms/vendor/craftcms/commerce/src/services/LineItems.php:236)"} []
Steps to reproduce
- customer adds line item added to cart
- product is deleted
- customer returns
- customer lands on template such as a cart/checkout step hitting
{% set cart = craft.commerce.carts.cart %} - customer triggers
Exception – No line item exists with the ID “571”
Looks like calling craft.commerce.carts.cart lands in commerce/src/services/LineItems.php saveLineItem() which throws this exception.
Seeing as this Exception occurs on the very method which gets the Order, how can we handle this scenario when we have no access to get the order's lineItem which no longer has a Variant attached to it?
I have been able to manage this on EVENT_BEFORE_PROCESS_PAYMENT where I remove an item from the order when $lineItem->purchasable is missing.
Expected behavior
No exception when calling
{% if cart is not defined %}
{% set cart = craft.commerce.carts.cart %}
{% endif %}
Actual behavior
Exception triggered
Craft CMS version
"craftcms/cms": "3.7.46",
Craft Commerce version
"craftcms/commerce": "3.4.15",
PHP version
8.0
Operating system and version
php:8.0.12-fpm-alpine
Database type and version
mariadb:10.2.31
Image driver and version
No response
Installed plugins and versions
@joepagan I am unable to reproduce in both Commerce 3.4.x and 4.1.x
Steps I took using the example templates:
- Added a product to the cart
- Confirmed there is a discount adjustment on the line item
- Deleted the product in the CP
- Refreshed the cart (calling save cart)
- See the following:
Could you maybe clear your logs, reproduce the issue and send your DB, logs, composer.json, composer.lock to [email protected]
Thanks.
Can you open a new issue if this is still not working correctly, with steps to reproduce. Thanks.