forked from lisastreeter/commerce_fee
-
Notifications
You must be signed in to change notification settings - Fork 2
/
commerce_fee.post_update.php
56 lines (49 loc) · 1.52 KB
/
commerce_fee.post_update.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
/**
* @file
* Post update functions for Fee.
*/
/**
* Allows fee start and end dates to have a time component.
*/
function commerce_fee_post_update_1(array &$sandbox = NULL) {
$fee_storage = \Drupal::entityTypeManager()->getStorage('commerce_fee');
if (!isset($sandbox['current_count'])) {
$query = $fee_storage->getQuery();
$sandbox['total_count'] = $query->count()->execute();
$sandbox['current_count'] = 0;
if (empty($sandbox['total_count'])) {
$sandbox['#finished'] = 1;
return;
}
}
$query = $fee_storage->getQuery();
$query->range($sandbox['current_count'], 50);
$result = $query->execute();
if (empty($result)) {
$sandbox['#finished'] = 1;
return;
}
/** @var \Drupal\commerce_fee\Entity\Fee[] $fees */
$fees = $fee_storage->loadMultiple($result);
foreach ($fees as $fee) {
// 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 = $fee->getStartDate();
$end_date = $fee->getEndDate();
if ($end_date) {
$end_date = $end_date->modify('+1 day');
}
$fee->setStartDate($start_date);
$fee->setEndDate($end_date);
$fee->save();
}
$sandbox['current_count'] += 50;
if ($sandbox['current_count'] >= $sandbox['total_count']) {
$sandbox['#finished'] = 1;
}
else {
$sandbox['#finished'] = ($sandbox['total_count'] - $sandbox['current_count']) / $sandbox['total_count'];
}
}