Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
- Create a Promotion
- Create a coupon to this promotion
- Delete this coupon
- 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()
Comment | File | Size | Author |
---|---|---|---|
#6 | 2922147-6.patch | 2.22 KB | mglaman |
|
Comments
Comment #2
GoZ CreditAttribution: GoZ at Barbe-Rousse commentedComment #3
GoZ CreditAttribution: GoZ at Barbe-Rousse commentedComment #4
GoZ CreditAttribution: GoZ at Barbe-Rousse commentedComment #5
GoZ CreditAttribution: GoZ at Barbe-Rousse commentedPR in https://github.com/drupalcommerce/commerce/pull/826
Comment #6
mglamanPatch from the PR
Comment #8
mglamanCommitted with tweak to access entity type manager via
\Drupal::entityTypeManager()
.