-
Notifications
You must be signed in to change notification settings - Fork 252
Expand file tree
/
Copy pathcommerce_promotion.module
More file actions
115 lines (101 loc) · 3.3 KB
/
commerce_promotion.module
File metadata and controls
115 lines (101 loc) · 3.3 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
<?php
/**
* @file
* Provides a UI for managing promotions.
*/
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Render\Element;
/**
* Implements hook_theme().
*/
function commerce_promotion_theme() {
return [
'commerce_promotion' => [
'render element' => 'elements',
],
'commerce_promotion_form' => [
'render element' => 'form',
],
'commerce_coupon_redemption_form' => [
'render element' => 'form',
],
];
}
/**
* Implements hook_theme_suggestions_commerce_promotion().
*/
function commerce_promotion_theme_suggestions_commerce_promotion(array $variables) {
return _commerce_entity_theme_suggestions('commerce_promotion', $variables);
}
/**
* Prepares variables for promotion templates.
*
* Default template: commerce-promotion.html.twig.
*
* @param array $variables
* An associative array containing:
* - elements: An associative array containing rendered fields.
* - attributes: HTML attributes for the containing element.
*/
function template_preprocess_commerce_promotion(array &$variables) {
/** @var Drupal\commerce_promotion\Entity\PromotionInterface $promotion */
$promotion = $variables['elements']['#commerce_promotion'];
$variables['promotion_entity'] = $promotion;
$variables['promotion_url'] = $promotion->toUrl();
$variables['promotion'] = [];
foreach (Element::children($variables['elements']) as $key) {
$variables['promotion'][$key] = $variables['elements'][$key];
}
}
/**
* Implements hook_entity_base_field_info().
*/
function commerce_promotion_entity_base_field_info(EntityTypeInterface $entity_type) {
if ($entity_type->id() == 'commerce_order') {
$fields['coupons'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Coupons'))
->setDescription(t('Coupons which have been applied to order.'))
->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)
->setRequired(FALSE)
->setSetting('target_type', 'commerce_promotion_coupon')
->setSetting('handler', 'default')
->setTranslatable(FALSE)
->setDisplayOptions('form', [
'type' => 'entity_reference_autocomplete',
'weight' => 5,
'settings' => [
'match_operator' => 'CONTAINS',
'size' => '60',
'autocomplete_type' => 'tags',
'placeholder' => '',
],
]);
return $fields;
}
}
/**
* Implements hook_cron().
*/
function commerce_promotion_cron() {
/** @var \Drupal\commerce_promotion\PromotionStorageInterface $promotion_storage */
$promotion_storage = \Drupal::service('entity_type.manager')->getStorage('commerce_promotion');
// Disable any promotions that have passed their end date.
$promotions = $promotion_storage->loadExpired();
if (!empty($promotions)) {
/** @var \Drupal\commerce_promotion\Entity\PromotionInterface $promotion */
foreach ($promotions as $promotion) {
$promotion->setEnabled(FALSE);
$promotion->save();
}
}
// Disable any promotions that have met their max usage.
$promotions = $promotion_storage->loadMaxedUsage();
if (!empty($promotions)) {
/** @var \Drupal\commerce_promotion\Entity\PromotionInterface $promotion */
foreach ($promotions as $promotion) {
$promotion->setEnabled(FALSE);
$promotion->save();
}
}
}