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.

Command icon 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:

Comments

jpoesen created an issue. See original summary.

jpoesen’s picture

Issue summary: View changes
jpoesen’s picture

Issue summary: View changes

jpoesen’s picture

Status: Active » Needs review

Merge request added.

jpoesen’s picture

Issue summary: View changes

  • jsacksick committed 0a73951 on 8.x-2.x authored by jpoesen
    Issue #3226397 by jpoesen: Add null guard clause in OrderSubscriber::...
jsacksick’s picture

Status: Needs review » Fixed

Committed.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.