Description
Canceling an order results in a fatal error if the purchasedEntity one of the licensableOrderItems no longer exists.
Flow
Cancel order -> OrderSubscriber::onCancel() -> OrderSubscriber::getLicensableOrderItems() -> fatal error: cannot call method 'hasfield' on null
Snippet
Location: src/EventSubscriber/OrderSubscriber::getLicensableOrderItems()
$purchased_entity = $order_item->getPurchasedEntity();
// This order item isn't "licensable" if the purchased entity it
// references isn't properly configured.
if (!$purchased_entity->hasField('license_type') || $purchased_entity->get('license_type')->isEmpty()) {
continue;
}
If $purchased_entity is null, $purchased_entity->hasField('license_type') throws a fatal error.
Context
If I understand correctly, in my case this is happening because:
- we migrated a decade's worth of old commerce orders (old site did not use commerce_license)
- we chose not to migrate a bunch of obsolete products we no longer sell so there is no reference to any existing entity that was purchased
Solution
Add guard clause to check for null before using $purchased_entity.
Merge request following.
Issue fork commerce_license-3226397
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
- 3226397-add-null-guard
changes, plain diff MR !3
Comments
Comment #2
jpoesen commentedComment #3
jpoesen commentedComment #5
jpoesen commentedMerge request added.
Comment #6
jpoesen commentedComment #8
jsacksick commentedCommitted.