-
Notifications
You must be signed in to change notification settings - Fork 252
Expand file tree
/
Copy pathcommerce_promotion.post_update.php
More file actions
289 lines (259 loc) · 10.5 KB
/
commerce_promotion.post_update.php
File metadata and controls
289 lines (259 loc) · 10.5 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
<?php
/**
* @file
* Post update functions for Promotion.
*/
use Drupal\commerce_promotion\Entity\PromotionInterface;
use Drupal\Core\Field\BaseFieldDefinition;
/**
* Add the coupons field to orders.
*/
function commerce_promotion_post_update_1() {
$entity_definition_update = \Drupal::entityDefinitionUpdateManager();
$order_definition = $entity_definition_update->getEntityType('commerce_order');
$fields = commerce_promotion_entity_base_field_info($order_definition);
$entity_definition_update->installFieldStorageDefinition('coupons', 'commerce_order', 'commerce_promotion', $fields['coupons']);
}
/**
* Add the 'promotion_id' field to coupons.
*/
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();
}
}
/**
* Delete orphaned coupons.
*/
function commerce_promotion_post_update_3() {
/** @var \Drupal\commerce_promotion\CouponStorageInterface $coupon_storage */
$coupon_storage = \Drupal::service('entity_type.manager')->getStorage('commerce_promotion_coupon');
/** @var \Drupal\commerce_promotion\Entity\CouponInterface[] $coupons */
$coupons = $coupon_storage->loadMultiple();
$delete_coupons = [];
foreach ($coupons as $coupon) {
if (!$coupon->getPromotion()) {
$delete_coupons[] = $coupon;
}
}
$coupon_storage->delete($delete_coupons);
}
/**
* Add the compatibility field to promotions.
*/
function commerce_promotion_post_update_4() {
$storage_definition = BaseFieldDefinition::create('list_string')
->setLabel(t('Compatibility with other promotions'))
->setSetting('allowed_values_function', ['\Drupal\commerce_promotion\Entity\Promotion', 'getCompatibilityOptions'])
->setRequired(TRUE)
->setDefaultValue(PromotionInterface::COMPATIBLE_ANY)
->setDisplayOptions('form', [
'type' => 'options_select',
'weight' => 4,
]);
$entity_definition_update = \Drupal::entityDefinitionUpdateManager();
$entity_definition_update->installFieldStorageDefinition('compatibility', 'commerce_promotion', 'commerce_promotion', $storage_definition);
/** @var \Drupal\commerce_promotion\PromotionStorageInterface $promotion_storage */
$promotion_storage = \Drupal::service('entity_type.manager')->getStorage('commerce_promotion');
/** @var \Drupal\commerce_promotion\Entity\PromotionInterface[] $promotions */
$promotions = $promotion_storage->loadMultiple();
foreach ($promotions as $promotion) {
$promotion->setCompatibility(PromotionInterface::COMPATIBLE_ANY);
$promotion->save();
}
}
/**
* Update offers and conditions.
*/
function commerce_promotion_post_update_6(&$sandbox = NULL) {
$promotion_storage = \Drupal::entityTypeManager()->getStorage('commerce_promotion');
if (!isset($sandbox['current_count'])) {
$query = $promotion_storage->getQuery();
$sandbox['total_count'] = $query->count()->execute();
$sandbox['current_count'] = 0;
if (empty($sandbox['total_count'])) {
$sandbox['#finished'] = 1;
return;
}
}
$query = $promotion_storage->getQuery();
$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) {
$needs_save = FALSE;
$conditions = $promotion->get('conditions')->getValue();
foreach ($conditions as &$condition_item) {
if ($condition_item['target_plugin_id'] == 'commerce_promotion_order_total_price') {
$condition_item['target_plugin_id'] = 'order_total_price';
// Remove data added by the old conditions API.
unset($condition_item['target_plugin_configuration']['id']);
unset($condition_item['target_plugin_configuration']['negate']);
$needs_save = TRUE;
}
}
$offer = $promotion->get('offer')->first()->getValue();
if ($offer['target_plugin_id'] == 'commerce_promotion_order_percentage_off') {
$offer['target_plugin_id'] = 'order_percentage_off';
$needs_save = TRUE;
}
elseif ($offer['target_plugin_id'] = 'commerce_promotion_product_percentage_off') {
$offer['target_plugin_id'] = 'order_item_percentage_off';
// The product_id setting has been removed and needs to be migrated to a condition.
$product_id = $offer['target_plugin_configuration']['product_id'];
unset($offer['target_plugin_configuration']['product_id']);
$has_existing_condition = FALSE;
foreach ($conditions as &$condition_item) {
if ($condition_item['target_plugin_id'] == 'order_item_product') {
$condition_item['target_plugin_configuration']['products'][] = ['product_id' => $product_id];
$condition_item['target_plugin_configuration']['products'] = array_unique($condition_item['target_plugin_configuration']['products']);
$has_existing_condition = TRUE;
}
}
if (!$has_existing_condition) {
$conditions[] = [
'target_plugin_id' => 'order_item_product',
'target_plugin_configuration' => [
'products' => [
['product_id' => $product_id],
],
],
];
}
$needs_save = TRUE;
}
if ($needs_save) {
$promotion->set('offer', $offer);
$promotion->set('conditions', $conditions);
$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'];
}
}
/**
* Add the condition_operator field to promotions.
*/
function commerce_promotion_post_update_7() {
$storage_definition = BaseFieldDefinition::create('list_string')
->setLabel(t('Condition operator'))
->setDescription(t('The condition operator.'))
->setRequired(TRUE)
->setSetting('allowed_values', [
'AND' => t('All conditions must pass'),
'OR' => t('Only one condition must pass'),
])
->setDisplayOptions('form', [
'type' => 'options_buttons',
'weight' => 4,
])
->setDisplayConfigurable('form', TRUE)
->setDefaultValue('AND');
$entity_definition_update = \Drupal::entityDefinitionUpdateManager();
$entity_definition_update->installFieldStorageDefinition('condition_operator', 'commerce_promotion', 'commerce_promotion', $storage_definition);
}
/**
* Re-save promotions to populate the condition operator field.
*/
function commerce_promotion_post_update_8(&$sandbox = NULL) {
$promotion_storage = \Drupal::entityTypeManager()->getStorage('commerce_promotion');
if (!isset($sandbox['current_count'])) {
$query = $promotion_storage->getQuery();
$sandbox['total_count'] = $query->count()->execute();
$sandbox['current_count'] = 0;
if (empty($sandbox['total_count'])) {
$sandbox['#finished'] = 1;
return;
}
}
$query = $promotion_storage->getQuery();
$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) {
$promotion->setConditionOperator('AND');
$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'];
}
}
/**
* Change the description basefield to support formatted text.
*/
function commerce_promotion_post_update_9() {
// To update the field schema we need to have no field data in the storage,
// thus we retrieve it, delete it from storage, and write it back to the
// storage after updating the schema.
$database = \Drupal::database();
// Check update is needed.
if ($database->schema()->fieldExists('commerce_promotion_field_data', 'description')) {
// Retrieve existing field data.
$descriptions = $database->select('commerce_promotion_field_data', 'cp')
->fields('cp', ['promotion_id', 'description'])
->execute()
->fetchAllKeyed();
// Remove data from the storage.
$database->update('commerce_promotion_field_data')
->fields(['description' => NULL])
->execute();
// Update definitions and schema.
$entity_definition_manager = \Drupal::entityDefinitionUpdateManager();
/** @var Drupal\Core\Field\BaseFieldDefinition $storage_definition */
$storage_definition = $entity_definition_manager->getFieldStorageDefinition('description', 'commerce_promotion');
$entity_definition_manager->uninstallFieldStorageDefinition($storage_definition);
$storage_definition = BaseFieldDefinition::create('text_long')
->setLabel(t('Description'))
->setDescription(t('Additional information about the promotion to show to the customer'))
->setTranslatable(TRUE)
->setDefaultValue('')
->setDisplayOptions('form', [
'type' => 'text_textarea',
'weight' => 1,
'settings' => [
'rows' => 3,
],
])
->setDisplayConfigurable('view', TRUE)
->setDisplayConfigurable('form', TRUE);
$entity_definition_manager->installFieldStorageDefinition('description', 'commerce_promotion', 'commerce_promotion', $storage_definition);
// Restore entity data in the new schema.
foreach ($descriptions as $promotion_id => $description) {
$database->update('commerce_promotion_field_data')
->fields(['description__value' => $description])
->condition('promotion_id', $promotion_id)
->execute();
}
}
}