-
Notifications
You must be signed in to change notification settings - Fork 0
/
price_in_time.module
327 lines (289 loc) · 10.4 KB
/
price_in_time.module
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
<?php
/**
* @file
* Primary module hooks for Price in Time module.
*/
# Drupal Core.
use Drupal\Core\Form\FormStateInterface;
# Commerce Product.
use Drupal\commerce_product\Entity\ProductVariation;
/**
* Implements hook_preprocess_page().
*
* @param $variables
*
* Some style need to be added to the configuration page of the
* time slots, so we can inject our custom library to the specific
* route (based on route name).
*
*/
function price_in_time_preprocess_page(&$variables) {
if (\Drupal::routeMatch()->getRouteName() === 'price_in_time.product_type_time_slots') {
$variables['#attached']['library'][] = 'price_in_time/price_in_time';
}
}
/**
* Implements hook_form_alter().
*
* @param $form
* @param FormStateInterface $form_state
* @param $form_id
*
* Edit the form page of the product/variation to insert the custom
* prices on the relative slot of time. This will be showed only if
* the node is not new and only if it is a product/variation.
*
*/
function price_in_time_form_alter(&$form, FormStateInterface $form_state, $form_id) {
if (\Drupal::service('router.admin_context')->isAdminRoute() &&
method_exists($form_state->getFormObject(), 'getEntity') &&
_price_in_time_check_bundle_enabled($form_state->getFormObject()->getEntity()->bundle())) {
#--------------------------------------
# define drupal front URL
#--------------------------------------
global $base_url;
if (($entity = $form_state->getFormObject()->getEntity()) && !$entity->isNew()) {
if (method_exists($entity, 'getVariations') &&
count($entity->getVariations()) > 1) {
# return if there are some other variations.
return;
}
#--------------------------------------
# define drupal 'fieldset' element
#--------------------------------------
$form['price_in_time__slots'] = [
'#type' => 'fieldset',
'#title' => t('Insert the price for different time slots'),
'#description' => t('
You can edit this configuration on the module page:
<a href="'.$base_url.'/admin/commerce/config/time-slots">
Price in Time configuration page
</a>'),
'#prefix' => '<div id="price-in-time--slots">',
'#suffix' => '</div>',
'#weight' => -100, # we want this at the top.
];
foreach (_price_in_time_get_times_slots($entity->bundle()) as $key => $slot) {
#--------------------------------------
# define drupal 'markup' element
#--------------------------------------
$form['price_in_time__slots'][$key]['times_info'] = [
'#markup' => '
<br> <!-- break the line every slot -->
<div class="price_in_time__slots_form_element">
FROM '.$slot['start'].' to '.$slot['end'].' (24H)
</div>',
];
#--------------------------------------
# define drupal 'price' element
#--------------------------------------
$form['price_in_time__slots'][$key]['price'] = [
'#type' => 'commerce_price',
'#title' => t('Owerride the price for this slot:'),
'#default_value' =>
_price_in_time_get_slot_price_default_value(
$slot['uuid'],
_price_in_time_get_sku_key_from_product_or_variation($entity)
),
];
#--------------------------------------
# define drupal 'hidden' element
#--------------------------------------
$form['price_in_time__slots'][$key]['uuid'] = [
'#type' => 'hidden',
'#value' => $slot['uuid'],
];
}
foreach (array_keys($form['actions']) as $action) {
if ($action != 'preview' && isset($form['actions'][$action]['#type']) && $form['actions'][$action]['#type'] === 'submit') {
$form['actions'][$action]['#submit'][] = 'price_in_time_form_submit';
}
}
$form['#tree'] = TRUE;
}
else {
#--------------------------------------
# define drupal 'fieldset' element
#--------------------------------------
$form['price_in_time__slots'] = [
'#type' => 'fieldset',
'#title' => t('Information:'),
'#description' => t('
You have enabled this product variation on <b>Price in Time</b> module, after the save you
should view the form for the prices.<br>
You can edit this configuration on the module page:
<a href="'.$base_url.'/admin/commerce/config/time-slots">
Price in Time configuration page
</a>'),
'#prefix' => '<div id="price-in-time--slots">',
'#suffix' => '</div>',
'#weight' => -100, # we want this at the top.
];
}
}
}
/**
* Implements hook_form_submit().
*
* @param $form
* @param FormStateInterface $form_state
*
* This will be called after the submit of the form (on the
* node update operations).
*
*/
function price_in_time_form_submit($form, FormStateInterface $form_state) {
if (method_exists($form_state->getFormObject(), 'getEntity')) {
$entity = $form_state->getFormObject()->getEntity();
$prices_values = $form_state->getValue('price_in_time__slots');
foreach ($prices_values as $key => $price) {
$product_variation_sku = _price_in_time_get_sku_key_from_product_or_variation($entity);
if (!empty($price['price']['number']) && $product_variation_sku) {
$connection = \Drupal::database();
$transaction = $connection->startTransaction();
try {
$product_variation = $entity->bundle();
$slot_uuid = $price['uuid'];
$number = $price['price']['number'];
$currency_code = $price['price']['currency_code'];
$created = \Drupal::time()->getRequestTime();
if (!_price_in_time_check_row_existance($connection, $slot_uuid, $product_variation_sku)) {
$connection
->insert('commerce_product_variation_slots_data')
->fields([
'product_variation_sku' => $product_variation_sku,
'product_variation' => $product_variation,
'slot_uuid' => $slot_uuid,
'number' => $number,
'currency_code' => $currency_code,
'created' => $created,
])->execute();
} else {
$connection->update('commerce_product_variation_slots_data')
->fields([
'number' => $number,
'currency_code' => $currency_code,
])
->condition('slot_uuid', $slot_uuid)
->condition('product_variation_sku', $product_variation_sku)
->execute();
}
}
catch (Exception $exception) {
$transaction->rollBack();
# Here we go to an error, log this into the watchdog.
# Sure this will be usefull...
\Drupal::logger('[PRICE-IN-TIME]')
->error('Insert error, check the message: ' . $exception->getMessage());
}
# Commit the transaction by unsetting the $transaction variable.
unset($transaction);
}
# Use the default price if the user not set the price
# on the relative form (for the slot). Delete the element
# from the database table.
else {
\Drupal::database()
->delete('commerce_product_variation_slots_data')
->condition('slot_uuid', $price['uuid'])
->condition('product_variation_sku', _price_in_time_get_sku_key_from_product_or_variation($entity))
->condition('product_variation', $entity->bundle())
->execute();
}
}
}
}
/**
* Implements _hook_check_row_existance().
*
* @param $connection
* @param string $slot_uuid
* @param string $product_variation_sku
* @return bool
*
* Return a boolean to check if the row exists in the custom table named
* commerce_product_variation_slots_data, this because we need to update
* the record if exists or insert new one if not.
*
*/
function _price_in_time_check_row_existance($connection, $slot_uuid, $product_variation_sku) {
return $connection
->select('commerce_product_variation_slots_data', 'commerce_product_slots')
->condition('commerce_product_slots.slot_uuid', $slot_uuid)
->condition('commerce_product_slots.product_variation_sku', $product_variation_sku)
->countQuery()
->execute()
->fetchField() > 0 ?: 0;
}
/**
* Implements _hook_get_sku_key_from_product_or_variation().
*
* @param $entity
* @return string
*
* Return the SKU of the single variation, in any case; in the commerce
* product case and in the multiple variations case.
*
*/
function _price_in_time_get_sku_key_from_product_or_variation($entity) {
return ($entity->getEntityTypeId() === 'commerce_product' && count($entity->getVariations()) === 1) ?
$entity->getVariations()[0]->getSku() : $entity->getSku();
}
/**
* Implements _hook_check_bundle_enabled().
*
* @param string $bundle
* @return bool
*
* Return a boolean so we can know if the specific variation
* is enabled or not (based on the configuration).
*
*/
function _price_in_time_check_bundle_enabled($bundle) {
return @\Drupal::config('price_in_time.settings')->get($bundle)['enabled'];
}
/**
* Implements _hook_get_times_slots().
*
* @param string $bundle
* @return array
*
* Return all the times related to the single variation bundle, so
* an array with this structure:
*
* key => [
* start_time,
* end_time,
* free_shipping,
* ]
*
*/
function _price_in_time_get_times_slots($bundle) {
return @\Drupal::config('price_in_time.settings')->get($bundle)['times'];
}
/**
* Implements _hook_get_slot_price_default_value().
*
* @param string $uuid
* @return array
*
* Return the price (number and currency code) related to the
* single product in the specific slot uuid. Return empty price
* if it was not set prevs.
*
*/
function _price_in_time_get_slot_price_default_value($uuid, $sku) {
return
is_array(
$default_price_value = \Drupal::database()
->select('commerce_product_variation_slots_data', 'commerce_product_slots')
->condition('commerce_product_slots.slot_uuid', $uuid)
->condition('commerce_product_slots.product_variation_sku', $sku)
->fields('commerce_product_slots', ['number', 'currency_code'])
->execute()
->fetchAssoc()
) ? $default_price_value : [
'number' => '', # set an empty value for the price.
'currency_code' => '', # get the default value.
];
}