You are here

function commerce_promotion_post_update_10 in Commerce Core 8.2

Re-save order item promotions to populate the display_included field.

File

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

Code

function commerce_promotion_post_update_10(&$sandbox = NULL) {
  $offer_ids = [
    'order_item_fixed_amount_off',
    'order_item_percentage_off',
  ];
  $promotion_storage = \Drupal::entityTypeManager()
    ->getStorage('commerce_promotion');
  if (!isset($sandbox['current_count'])) {
    $query = $promotion_storage
      ->getQuery();
    $query
      ->condition('offer.target_plugin_id', $offer_ids, 'IN');
    $sandbox['total_count'] = $query
      ->count()
      ->execute();
    $sandbox['current_count'] = 0;
    if (empty($sandbox['total_count'])) {
      $sandbox['#finished'] = 1;
      return;
    }
  }
  $query = $promotion_storage
    ->getQuery();
  $query
    ->condition('offer.target_plugin_id', $offer_ids, 'IN');
  $query
    ->range($sandbox['current_count'], 25);
  $result = $query
    ->execute();
  if (empty($result)) {
    $sandbox['#finished'] = 1;
    return;
  }

  /** @var \Drupal\commerce_promotion\Entity\PromotionInterface[] $promotions */
  $promotions = $promotion_storage
    ->loadMultiple($result);
  foreach ($promotions as $promotion) {

    // Work on the raw plugin item to avoid defaults being merged in.
    $offer_item = $promotion
      ->get('offer')
      ->first();
    $configuration = $offer_item->target_plugin_configuration;
    if (!isset($configuration['display_inclusive'])) {
      $configuration['display_inclusive'] = FALSE;
      $offer_item->target_plugin_configuration = $configuration;
      $promotion
        ->save();
    }
  }
  $sandbox['current_count'] += 25;
  if ($sandbox['current_count'] >= $sandbox['total_count']) {
    $sandbox['#finished'] = 1;
  }
  else {
    $sandbox['#finished'] = ($sandbox['total_count'] - $sandbox['current_count']) / $sandbox['total_count'];
  }
}