You are here

function commerce_pricelist_post_update_2 in Commerce Pricelist 8.2

Allows price list start and end dates to have a time component.

File

./commerce_pricelist.post_update.php, line 43
Post update functions for Pricelist.

Code

function commerce_pricelist_post_update_2(array &$sandbox = NULL) {
  $storage = \Drupal::entityTypeManager()
    ->getStorage('commerce_pricelist');
  if (!isset($sandbox['current_count'])) {
    $query = $storage
      ->getQuery();
    $sandbox['total_count'] = $query
      ->count()
      ->execute();
    $sandbox['current_count'] = 0;
    if (empty($sandbox['total_count'])) {
      $sandbox['#finished'] = 1;
      return;
    }
  }
  $query = $storage
    ->getQuery();
  $query
    ->range($sandbox['current_count'], 25);
  $result = $query
    ->execute();
  if (empty($result)) {
    $sandbox['#finished'] = 1;
    return;
  }

  /** @var \Drupal\commerce_pricelist\Entity\PriceList[] $price_lists */
  $price_lists = $storage
    ->loadMultiple($result);
  foreach ($price_lists as $price_list) {

    // Re-set each date to ensure it is stored in the updated format.
    // Increase the end date by a day to match old inclusive loading
    // (where an end date was valid until 23:59:59 of that day).
    $start_date = $price_list
      ->getStartDate();
    $end_date = $price_list
      ->getEndDate();
    if ($end_date) {
      $end_date = $end_date
        ->modify('+1 day');
    }
    $price_list
      ->setStartDate($start_date);
    $price_list
      ->setEndDate($end_date);
    $price_list
      ->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'];
  }
}