Problem/Motivation

  1. Create a Promotion
  2. Create a coupon to this promotion
  3. Delete this coupon
  4. Save the promotion

An error is displayed:

The website encountered an unexpected error. Please try again later.
Error: Call to a member function getPromotionId() on null in Drupal\commerce_promotion\Entity\Promotion->postSave() (line 520 of modules/contrib/commerce/modules/promotion/src/Entity/Promotion.php).

Drupal\commerce_promotion\Entity\Promotion->postSave(Object, 1) (Line: 469)
Drupal\Core\Entity\EntityStorageBase->doPostSave(Object, 1) (Line: 320)
Drupal\Core\Entity\ContentEntityStorageBase->doPostSave(Object, 1) (Line: 395)
Drupal\Core\Entity\EntityStorageBase->save(Object) (Line: 796)
Drupal\Core\Entity\Sql\SqlContentEntityStorage->save(Object) (Line: 377)
Drupal\Core\Entity\Entity->save() (Line: 116)
Drupal\commerce_promotion\Form\PromotionForm->save(Array, Object)
call_user_func_array(Array, Array) (Line: 111)
Drupal\Core\Form\FormSubmitter->executeSubmitHandlers(Array, Object) (Line: 51)
Drupal\Core\Form\FormSubmitter->doSubmitForm(Array, Object) (Line: 585)
Drupal\Core\Form\FormBuilder->processForm('commerce_promotion_edit_form', Array, Object) (Line: 314)
Drupal\Core\Form\FormBuilder->buildForm('commerce_promotion_edit_form', Object) (Line: 74)
Drupal\Core\Controller\FormController->getContentResult(Object, Object)
call_user_func_array(Array, Array) (Line: 123)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 576)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 124)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) (Line: 97)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
call_user_func_array(Object, Array) (Line: 153)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 68)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 57)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 99)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 78)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 38)
Drupal\webprofiler\StackMiddleware\WebprofilerMiddleware->handle(Object, 1, 1) (Line: 50)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 657)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

The reference from Promotion to coupon still exists. The coupon remove promotion usage but don't remove reference to this promotion.

  /**
   * {@inheritdoc}
   */
  public function deleteByCoupon(array $coupons) {
    $this->connection->delete('commerce_promotion_usage')
      ->condition('coupon_id', EntityHelper::extractIds($coupons), 'IN')
      ->execute();
  }

Proposed resolution

Remove reference to this coupon in promotion entity at $coupon->postDelete()

CommentFileSizeAuthor
#6 2922147-6.patch2.22 KBmglaman
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

GoZ created an issue. See original summary.

GoZ’s picture

Issue summary: View changes
GoZ’s picture

Issue summary: View changes
GoZ’s picture

Title: Delete a coupon dos not delete coupon reference from Promotion » Delete a coupon does not delete coupon reference from Promotion
GoZ’s picture

Status: Active » Needs review
mglaman’s picture

Patch from the PR

  • mglaman committed b0f013f on 8.x-2.x authored by GoZ
    Issue #2922147 by mglaman, GoZ: Delete a coupon does not delete coupon...
mglaman’s picture

Status: Needs review » Fixed

Committed with tweak to access entity type manager via \Drupal::entityTypeManager().

Status: Fixed » Closed (fixed)

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