forked from ajitdev/commerce_product_kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
commerce_product_kit.module
277 lines (262 loc) · 9.71 KB
/
commerce_product_kit.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
<?php
/**
* @file
* Contains the definition of the field formatter.
*/
/**
* Implements hook_permission().
*/
function commerce_product_kit_permission() {
return array(
'administer commerce product kit' => array(
'title' => t('Administer commerce product kit'),
'description' => t('Perform administration task for commerce product kit module.'),
),
);
}
/**
* Implements hook_field_formatter_info().
*/
function commerce_product_kit_field_formatter_info() {
return array(
'commerce_product_kit' => array(
'label' => t('Product kit'),
'field types' => array('entityreference', 'commerce_product_reference'),
'settings' => array(
'commerce_product_kit_label' => 'product kit',
),
),
);
}
/**
* Implements hook_field_formatter_settings_form().
*/
function commerce_product_kit_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
if ($display['type'] == 'commerce_product_kit') {
$element['commerce_product_kit_label'] = array(
'#title' => t('Product kit label'),
'#description' => t('What do you call a product kit? E.g. "Set", "Wardrobe", etc.'),
'#type' => 'textfield',
'#required' => 'required',
'#size' => 20,
'#default_value' => $settings['commerce_product_kit_label'],
);
}
return $element;
}
/**
* Implements hook_field_formatter_settings_summary().
*/
function commerce_product_kit_field_formatter_settings_summary($field, $instance, $view_mode) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
if ($display['type'] == 'commerce_product_kit') {
$summary = t('Product kit label : @label', array('@label' => $settings['commerce_product_kit_label']));
}
return $summary;
}
/**
* Implements hook_field_formatter_view().
*/
function commerce_product_kit_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
if ($display['type'] == 'commerce_product_kit') {
$settings = $display['settings'];
$product_ids = array();
foreach ($items as $item) {
if (isset($item['product_id'])) {
$product_ids[] = $item['product_id'];
}
elseif (module_exists('entityreference') && isset($item['target_id'])) {
$product_ids[] = $item['target_id'];
}
}
$product_ids = array_count_values($product_ids);
$commerce_product_kit_add_cart_form = drupal_get_form('commerce_product_kit_add_cart_form', $product_ids, $settings['commerce_product_kit_label']);
return array(
'#theme' => 'commerce_product_kit',
'#commerce_product_kit_form' => $commerce_product_kit_add_cart_form,
);
}
}
/**
* Form which displays the single button to add the products in the
* kit to the cart.
*/
function commerce_product_kit_add_cart_form($form, &$form_state, $product_ids, $formatter_label) {
$form = array();
$form['product_ids'] = array(
'#type' => 'value',
'#value' => $product_ids,
);
$form['add_kit_to_cart'] = array(
'#type' => 'submit',
'#value' => t("Add @formatter_label to cart", array('@formatter_label' => $formatter_label)),
);
return $form;
}
/**
* Submit function to add products to the cart.
*/
function commerce_product_kit_add_cart_form_submit($form, &$form_state) {
global $user;
$clicked_button_text = $form_state['clicked_button']['#value'];
// Get the label of the product kit from the submit button text.
preg_match('~Add(.*?)to\ cart~', $clicked_button_text, $matches);
$product_ids = $form_state['values']['product_ids'];
$number_of_products_in_kit = count($product_ids);
if (!empty($product_ids)) {
$products_not_added_to_cart = array();
$number_of_products_added_to_cart = 0;
// Load each product and add to cart.
foreach ($product_ids as $product_id => $quantity) {
$product = commerce_product_load($product_id);
if (module_exists('commerce_stock')) {
$wrapper = entity_metadata_wrapper('commerce_product', $product);
if (isset($wrapper->commerce_stock)) {
$product_stock = (int) $wrapper->commerce_stock->value();
$product_title = (string) $wrapper->title->value();
if ($product_stock < $quantity) {
$products_not_added_to_cart[] = $product_title;
continue;
}
}
}
$line_item = commerce_product_line_item_new($product, $quantity);
$number_of_products_added_to_cart++;
// For calculation of selling price. This helps in invoking the
// pricing or shipping rules.
drupal_alter('commerce_product_calculate_sell_price_line_item', $line_item);
rules_invoke_event('commerce_product_calculate_sell_price', $line_item);
$line_item = commerce_cart_product_add($user->uid, $line_item);
}
// Find how many products were not added to the cart.
$number_of_products_not_added_to_cart = count($products_not_added_to_cart);
rules_invoke_event('commerce_product_kit_added', $number_of_products_added_to_cart, $number_of_products_not_added_to_cart, $number_of_products_in_kit);
if ($number_of_products_added_to_cart == $number_of_products_in_kit) {
$message = t('All products from the @product_kit have been added to the cart.', array('@product_kit' => $matches[1]));
drupal_set_message($message);
}
elseif ($number_of_products_not_added_to_cart == $number_of_products_in_kit) {
$message = t('No products were added to the cart due to stock shortage.');
drupal_set_message($message);
}
else {
$products_not_added = implode(', ', $products_not_added_to_cart);
$message = format_plural($number_of_products_not_added_to_cart, 'The product @product_not_added was not added to the cart because it is out of stock.', 'The products @product_not_added were not added to the cart due to stock shortage.', array('@product_not_added' => $products_not_added));
drupal_set_message($message);
}
}
}
/**
* Implements hook_theme().
*/
function commerce_product_kit_theme($existing, $type, $theme, $path) {
return array(
'commerce_product_kit' => array(
'template' => 'templates/commerce-product-kit',
'variables' => array(
'commerce_product_kit_form' => array(),
),
),
);
}
/**
* Implements rules_event_info().
*/
function commerce_product_kit_rules_event_info() {
return array(
'commerce_product_kit_added' => array(
'label' => t('Product kit added to cart'),
'module' => 'commerce_product_kit',
'group' => 'commerce product kit' ,
'variables' => array(
'number_of_products_added_to_cart' => array(
'type' => 'integer',
'label' => t('Number of products added to cart'),
),
'number_of_products_not_added_to_cart' => array(
'type' => 'integer',
'label' => t('Number of products not added to cart'),
),
'number_of_products_in_kit' => array(
'type' => 'integer',
'label' => t('Total number of products in the product kit'),
),
),
),
);
}
/**
* Implements rules_condition_info().
*/
function commerce_product_kit_rules_condition_info() {
$conditions = array(
'commerce_product_kit_all_added' => array(
'label' => t('All products added to cart'),
'group' => t('Commerce Product Kit') ,
'parameter' => array(
'number_of_products_added_to_cart' => array(
'label' => t('Number of products added to cart'),
'type' => 'integer',
),
'number_of_products_in_kit' => array(
'label' => t('Total number of products in the product kit'),
'type' => 'integer',
),
),
),
'commerce_product_kit_none_added' => array(
'label' => t('No products added to cart'),
'group' => t('Commerce Product Kit') ,
'parameter' => array(
'number_of_products_not_added_to_cart' => array(
'label' => t('Number of products not added to cart'),
'type' => 'integer',
),
'number_of_products_in_kit' => array(
'label' => t('Total number of products in the product kit'),
'type' => 'integer',
),
),
),
'commerce_product_kit_some_added' => array(
'label' => t('Some products added to cart'),
'group' => t('Commerce Product Kit'),
'parameter' => array(
'number_of_products_added_to_cart' => array(
'label' => t('Number of products added to cart'),
'type' => 'integer',
),
'number_of_products_not_added_to_cart' => array(
'label' => t('Number of products not added to cart'),
'type' => 'integer',
),
'number_of_products_in_kit' => array(
'label' => t('Total number of products in the product kit'),
'type' => 'integer',
),
),
),
);
return $conditions;
}
/**
* Checks condition all the products are added to cart.
*/
function commerce_product_kit_all_added($number_of_products_added_to_cart, $number_of_products_in_kit) {
return ($number_of_products_added_to_cart == $number_of_products_in_kit);
}
/**
* Checks condition none products are added to cart.
*/
function commerce_product_kit_none_added($number_of_products_not_added_to_cart, $number_of_products_in_kit) {
return ($number_of_products_not_added_to_cart == $number_of_products_in_kit);
}
/**
* Checks condition some of the products are added to cart.
*/
function commerce_product_kit_some_added($number_of_products_added_to_cart, $number_of_products_not_added_to_cart, $number_of_products_in_kit) {
return (!(($number_of_products_added_to_cart == $number_of_products_in_kit) || ($number_of_products_not_added_to_cart == $number_of_products_in_kit)));
}