You are here

function commerce_promotion_post_update_2 in Commerce Core 8.2

Add the 'promotion_id' field to coupons.

File

modules/promotion/commerce_promotion.post_update.php, line 25
Post update functions for Promotion.

Code

function commerce_promotion_post_update_2() {
  $storage_definition = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('Promotion'))
    ->setDescription(t('The parent promotion.'))
    ->setSetting('target_type', 'commerce_promotion')
    ->setReadOnly(TRUE)
    ->setDisplayConfigurable('view', TRUE);
  $update_manager = \Drupal::entityDefinitionUpdateManager();
  $update_manager
    ->installFieldStorageDefinition('promotion_id', 'commerce_promotion_coupon', 'commerce_promotion', $storage_definition);

  /** @var \Drupal\commerce_promotion\PromotionStorageInterface $promotion_storage */
  $promotion_storage = \Drupal::service('entity_type.manager')
    ->getStorage('commerce_promotion');
  $promotions = $promotion_storage
    ->loadMultiple();
  foreach ($promotions as $promotion) {

    // Promotion::preSave() will populate the new field.
    $promotion
      ->save();
  }
}