craftcms / craftcms/commerce

[5.x]: Deleting a completed order with a custom line item throws InvalidConfigException

Open Beginner friendly
#4,371 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug commerce5 Craft Commerce
Dominant language
PHP
Stars
240
Forks
173
Avg merge
5d 11h
Merged PRs (30d)
8

Description

What happened?
Description

Completed orders that contain a custom line item cannot be deleted, neither from the CP nor via Elements::deleteElement(). Order::afterDelete() loops over the line items captured in beforeDelete() and calls $lineItem->getPurchasable() on each to refresh the stock cache. getPurchasable() throws for LineItemType::Custom, so the delete transaction rolls back.

Steps to reproduce
  1. Complete an order that contains a custom line item.
  2. Delete the order from the Orders index.
Expected behavior

The order is deleted. Custom line items have no purchasable and no inventory, so they should be skipped.

Actual behavior

yii\base\InvalidConfigException: Cannot get a purchasable for a custom line item at src/models/LineItem.php:838, called from src/elements/Order.php:3816 (afterDelete).

Suggested fix:

skip custom line items in the loop, e.g. if ($lineItem->type === LineItemType::Custom) { continue; }. Same class of bug as #4337 and #3733.

Craft CMS version

5.11.1

Craft Commerce version

5.7.4

PHP version

No response

Operating system and version

No response

Database type and version

No response

Image driver and version

No response

Installed plugins and versions

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in src/elements/Order.php at Order::afterDelete(), then inspect src/models/LineItem.php around getPurchasable(). Reproduce deletion of a completed order containing a custom line item from the Orders index or via Elements::deleteElement(). Done means the order deletes successfully without InvalidConfigException and custom line items are skipped during stock-cache refresh.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
backend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
88/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.