Steps to reproduce:

1. Install Drupal 8.5.3 && Drupal Commerce 8.x-2.6
2. Goto /admin/commerce/orders/add
3. Specify all required details
4. Add the Adjustment:
Type - Promotion
Label - any label
Amount - any amount
5. Save changes and try to place the order. You will get:
TypeError: Argument 2 passed to Drupal\commerce_promotion\PromotionUsage::register() must implement interface Drupal\commerce_promotion\Entity\PromotionInterface, null given, called in /path/modules/contrib/commerce/modules/promotion/src/EventSubscriber/OrderEventSubscriber.php on line 85 in Drupal\commerce_promotion\PromotionUsage->register() (line 33 of /path/modules/contrib/commerce/modules/promotion/src/PromotionUsage.php)

Comments

max-kuzomko created an issue. See original summary.

bojanz’s picture

OrderEventSubscriber::registerUsage():

foreach ($adjustments as $adjustment) {
      if ($adjustment->getType() != 'promotion') {
        continue;
      }

      $promotion_id = $adjustment->getSourceId();
      if ($promotion_id && !in_array($promotion_id, $coupon_promotion_ids)) {
        $promotion = $this->promotionStorage->load($promotion_id);
        $this->usage->register($order, $promotion);
      }
    }

We need to check that $promotion is an object (and not FALSE) before passing it to $this->usage->register().

max-kuzomko’s picture

Assigned: Unassigned » max-kuzomko
max-kuzomko’s picture

Status: Active » Needs review
StatusFileSize
new21.04 KB

Thanks, @bojanz for really quick response!

Attaching the patch.

max-kuzomko’s picture

StatusFileSize
new875 bytes

Wrong patch

The last submitted patch, 4: commerce_checkbox-same-as-billing_2852207_166.patch, failed testing. View results

max-kuzomko’s picture

Assigned: max-kuzomko » Unassigned
mglaman’s picture

Looks good, to me. Ideally we'd have a test showing that this crash won't occur again.

bojanz credited archnode.

bojanz’s picture

Crediting archnode, who posted a duplicate patch in another issue (#2992825: Don't require a promotion adjustment source_id to match an entity).

  • bojanz committed 89aca7c on 8.x-2.x authored by max-kuzomko
    Issue #2980255 by max-kuzomko, bojanz, archnode: Custom promotion...
bojanz’s picture

Status: Needs review » Fixed

Committed. Thanks, everyone.

Status: Fixed » Closed (fixed)

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