-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcommerce_custom_product.module
605 lines (532 loc) · 23.9 KB
/
commerce_custom_product.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
<?php
/**
* @file
* Adds features to support the creation of customizable products.
*/
/**
* Implements hook_menu().
*/
function commerce_custom_product_menu() {
$items = array();
$items['admin/commerce/config/line-items/add-product-line-item-type'] = array(
'title' => 'Add a product line item type',
'description' => 'Create a new customizable product line item type.',
'page callback' => 'drupal_get_form',
'page arguments' => array('commerce_custom_product_line_item_type_form', array('type' => '', 'name' => '')),
'access arguments' => array('administer line item types'),
'type' => MENU_LOCAL_ACTION,
'file' => 'includes/commerce_custom_product.admin.inc',
);
// Add delete links for custom product line item types that can be deleted.
foreach (commerce_custom_product_commerce_line_item_type_info() as $type => $line_item_type) {
// Convert underscores to hyphens for the menu item argument.
$type_arg = strtr($type, '_', '-');
$items['admin/commerce/config/line-items/' . $type_arg . '/edit'] = array(
'title' => 'Edit',
'description' => 'Edit the custom product line item type.',
'page callback' => 'commerce_custom_product_line_item_type_edit',
'page arguments' => array($type),
'access arguments' => array('administer line item types'),
'type' => MENU_LOCAL_TASK,
'context' => MENU_CONTEXT_INLINE,
'weight' => 5,
'file' => 'includes/commerce_custom_product.admin.inc',
);
$items['admin/commerce/config/line-items/' . $type_arg . '/delete'] = array(
'title' => 'Delete',
'description' => 'Delete the custom product line item type.',
'page callback' => 'drupal_get_form',
'page arguments' => array('commerce_custom_product_line_item_type_delete_form', $type),
'access callback' => 'commerce_custom_product_line_item_type_delete_access',
'access arguments' => array($type),
'type' => MENU_LOCAL_TASK,
'context' => MENU_CONTEXT_INLINE,
'weight' => 10,
'file' => 'includes/commerce_custom_product.admin.inc',
);
}
return $items;
}
/**
* Determines access to a delete form for the given line item type.
*/
function commerce_custom_product_line_item_type_delete_access($type) {
// Load the line item type.
$line_item_type = commerce_line_item_type_load($type);
// Look for any line items of this type.
$query = new EntityFieldQuery();
$query
->entityCondition('entity_type', 'commerce_line_item', '=')
->entityCondition('bundle', $type, '=')
->count();
// If we found at least one, return FALSE to prevent deletion of this line
// item type.
if ($query->execute() > 0) {
return FALSE;
}
return user_access('administer line item types');
}
/**
* Implements hook_help().
*/
function commerce_custom_product_help($path, $arg) {
if ($path == 'admin/commerce/config/line-items') {
return '<p>' . t('Add as many product line item types as you need to support the types of customizable products on your site. You must then change the Add to Cart form display formatter settings of a product reference field to use the new line item type. Any fields attached to the line item type marked to appear on the Add to Cart form will then appear when that form is rendered.') . '</p><p>' . t('Once a line item has been saved for the custom line item type (such as a product being added to the cart), the line item type can no longer be deleted unless you first delete all related line items.') . '</p>';
}
}
/**
* Implements hook_commerce_line_item_type_info().
*/
function commerce_custom_product_commerce_line_item_type_info() {
$line_item_types = array();
// Look for product line item types currently defined in the database.
$db_types = commerce_custom_product_line_item_types();
if (!empty($db_types)) {
foreach ($db_types as $type => $line_item_type) {
$line_item_types[$type] = array(
'name' => check_plain($line_item_type['name']),
'description' => t('A customizable product line item type.'),
'product' => FALSE,
'add_form_submit_value' => t('Add product'),
'base' => 'commerce_product_line_item',
);
}
}
return $line_item_types;
}
/**
* Returns an array of all available customizable product line item types.
*/
function commerce_custom_product_line_item_types() {
return db_query('SELECT * FROM {commerce_product_line_item_type}')->fetchAllAssoc('type', PDO::FETCH_ASSOC);
}
/**
* Saves a customizable product line item type.
*
* @param $line_item_type
* The full line item type info array to save.
* @param $configure
* Boolean indicating whether or not line item type configuration should be
* performed in the event of a new line item type being saved.
* @param $skip_rebuild
* Boolean indicating whether or not this save should result in the menu being
* rebuilt; defaults to FALSE. This is useful when you intend to perform many
* saves at once, as menu rebuilding is very costly to performance.
*
* @return
* The return value of the call to drupal_write_record() to save the line item
* type, either FALSE on failure or SAVED_NEW or SAVED_UPDATED indicating the
* type of query performed to save the line item type.
*/
function commerce_custom_product_line_item_type_save($line_item_type, $configure = TRUE, $skip_rebuild = FALSE) {
$op = drupal_write_record('commerce_product_line_item_type', $line_item_type, commerce_line_item_type_load($line_item_type['type']) ? 'type' : array());
commerce_line_item_types_reset();
if ($op == SAVED_NEW) {
// Notify the field API that a new bundle has been created.
field_attach_create_bundle('commerce_line_item', $line_item_type['type']);
// Load the full line item type array.
$line_item_type = commerce_line_item_type_load($line_item_type['type']);
// Configure the new line item type with default fields.
if ($configure) {
commerce_line_item_configure_line_item_type($line_item_type);
}
// Notify other modules that a new line item type has been created.
module_invoke_all('commerce_custom_product_line_item_type_insert', $line_item_type, $skip_rebuild);
}
elseif ($op == SAVED_UPDATED) {
// Notify other modules that an existing line item type has been updated.
module_invoke_all('commerce_custom_product_line_item_type_update', $line_item_type, $skip_rebuild);
}
if (!$skip_rebuild) {
variable_set('menu_rebuild_needed', TRUE);
}
return $op;
}
/**
* Deletes a customizable product line item type.
*
* @param $type
* The machine-name of the line item type to delete.
*/
function commerce_custom_product_line_item_type_delete($type) {
// Load the full line item type.
$line_item_type = commerce_line_item_type_load($type);
db_delete('commerce_product_line_item_type')
->condition('type', $type)
->execute();
commerce_line_item_types_reset();
// Notify other modules that this bundle / line item type has been deleted.
field_attach_delete_bundle('commerce_line_item', $line_item_type['type']);
module_invoke_all('commerce_custom_product_line_item_type_delete', $line_item_type);
variable_set('menu_rebuild_needed', TRUE);
}
/**
* Implements hook_field_info().
*/
function commerce_custom_product_field_info() {
return array(
'commerce_custom_product_line_item_type_reference' => array(
'label' => t('Line item type reference'),
'description' => t('This field stores the ID of a related line item type.'),
'settings' => array(),
'instance_settings' => array(),
'default_widget' => 'commerce_custom_product_line_item_type_reference_select',
'default_formatter' => NULL,
),
);
}
/**
* Implements hook_field_validate().
*/
function commerce_custom_product_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
foreach ($items as $delta => $item) {
if (!empty($item['target_id'])) {
$type = commerce_line_item_type_load($item['target_id']);
if (!$type || $type['product']) {
$errors[$field['field_name']][$langcode][$delta][] = array(
'error' => 'commerce_custom_product_line_item_type_reference_invalid',
'message' => t('%name: Invalid line item type referenced.', array('%name' => $instance['label'])),
);
}
}
}
}
/**
* Implements hook_field_widget_info().
*/
function commerce_custom_product_field_widget_info() {
$widgets = array();
$widgets['commerce_custom_product_line_item_type_reference_select'] = array(
'label' => t('Select list'),
'description' => t('Select referencable line item type from a drop-down.'),
'field types' => array('commerce_custom_product_line_item_type_reference'),
'settings' => array(),
'behaviors' => array(
'multiple values' => FIELD_BEHAVIOR_DEFAULT,
),
);
return $widgets;
}
/**
* Implements hook_field_widget_form().
*/
function commerce_custom_product_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
// Define the autocomplete textfield for products.
if ($instance['widget']['type'] == 'commerce_custom_product_line_item_type_reference_select') {
$line_items_types = array_filter(
commerce_line_item_types(),
function ($item) { return !$item['product']; }
);
$options = array(NULL => '- ' . t('None') . ' -');
foreach ($line_items_types as $line_items_type) {
$options[$line_items_type['type']] = $line_items_type['name'];
}
return $element + array(
'target_id' => array(
'#type' => 'select',
'#default_value' => isset($items[$delta]['target_id']) ? $items[$delta]['target_id'] : NULL,
'#options' => $options,
),
'use_multi' => array(
'#type' => 'checkbox',
'#title' => t('One line item per product item'),
'#default_value' => isset($items[$delta]['use_multi']) ? $items[$delta]['use_multi'] : 0,
),
);
}
}
/**
* Implements hook_field_is_empty().
*/
function commerce_custom_product_field_is_empty($item, $field) {
return empty($item['target_id']);
}
/**
* Implements hook_form_BASE_FORM_ID_alter();
*
* Instead of displaying fields from one line item type it reads data from a field
* and displays one or more line items.
*/
function commerce_custom_product_form_commerce_cart_add_to_cart_form_alter(&$form, &$form_state) {
$entity = entity_load($form_state['context']['entity_type'], array($form_state['context']['entity_id']));
$entity = reset($entity);
$view_mode = $form_state['context']['display'];
$entity_info = entity_get_info($form_state['context']['entity_type']);
$bundle_key = $entity_info['entity keys']['bundle'];
// If the display information contains settings use them, otherwise it's the
// name of a view mode.
$extra_line_items_field = NULL;
if (is_array($view_mode)) {
if (isset($view_mode['settings']['extra_line_items_field'])) {
$extra_line_items_field = $view_mode['settings']['extra_line_items_field'];
}
}
else {
// Check field configuration and see which field is to be used as the source
// for configuration.
foreach (field_info_instances($form_state['context']['entity_type'], $entity->{$bundle_key}) as $instance) {
$field = field_info_field($instance['field_name']);
if ($field['type'] == 'commerce_product_reference') {
if (empty($instance['display'][$view_mode])) {
$view_mode = 'default';
}
if ($instance['display'][$view_mode]['type'] == 'commerce_cart_add_to_cart_form') {
$extra_line_items_field = $instance['display'][$view_mode]['settings']['extra_line_items_field'];
}
}
}
}
// If configuration field was found and there are values on it add stuff to the
// form.
if ($extra_line_items_field && $types = field_get_items($form_state['context']['entity_type'], $entity, $extra_line_items_field)) {
if (!empty($form_state['input']['quantity'])) {
$form['quantity']['#default_value'] = $form_state['input']['quantity'];
}
$form['quantity']['#ajax'] = array(
'callback' => 'commerce_custom_product_quantity_js',
'wrapper' => drupal_html_class($form['#form_id']),
'method' => 'replace',
);
$product_ids = $form_state['line_item']->data['context']['product_ids'];
$products = commerce_product_load_multiple($product_ids);
$form['extra_line_items_fields'] = array(
'#type' => 'container',
'#parents' => array('extra_line_items_fields'),
'#weight' => 11,
'#tree' => TRUE,
);
$form_state['extra_line_items_fields'] = array();
$id = 0;
foreach ($types as $type) {
$line_item_count = 0;
do {
$form['extra_line_items_fields'][$id] = array(
'#type' => 'container',
);
if (empty($form_state['extra_line_items'][$id])) {
$line_item = commerce_product_line_item_new(commerce_product_reference_default_product($products), 1, 0, array(), $type['target_id']);
$line_item->data['context']['product_ids'] = array_keys($products);
$line_item->data['context']['add_to_cart_combine'] = !empty($settings['combine']);
$line_item->data['context']['show_single_product_attributes'] = !empty($settings['show_single_product_attributes']);
$form_state['extra_line_items'][] = $line_item;
}
else {
$line_item = $form_state['extra_line_items'][$id];
}
field_attach_form('commerce_line_item', $line_item, $form['extra_line_items_fields'][$id], $form_state);
// Loop over the fields we just added and remove any that haven't been
// marked for inclusion on this form.
foreach (element_children($form['extra_line_items_fields'][$id]) as $field_name) {
$form['extra_line_items_fields'][$id]['#parents'] = array('extra_line_items_fields', $id);
$info = field_info_instance('commerce_line_item', $field_name, $line_item->type);
$form['extra_line_items_fields'][$id][$field_name]['#commerce_cart_settings'] = commerce_cart_field_instance_access_settings($info);
if (empty($form['extra_line_items_fields'][$id][$field_name]['#commerce_cart_settings']['field_access'])) {
$form['extra_line_items_fields'][$id][$field_name]['#access'] = FALSE;
}
}
$id++;
$line_item_count++;
// TODO - We might need to read quantity from a different source.
} while($type['use_multi'] && $line_item_count < $form['quantity']['#default_value']);
}
$form['#submit'][0] = 'commerce_custom_product_add_to_cart_form_submit';
}
}
/**
* Form submit handler: add the selected product to the cart.
*
* @see commerce_cart_add_to_cart_form_submit()
*/
function commerce_custom_product_add_to_cart_form_submit($form, &$form_state) {
// Use default submit handler to add product line item.
commerce_cart_add_to_cart_form_submit($form, $form_state);
//Add any additional line items.
if (!empty($form_state['extra_line_items'])) {
$product_id = $form_state['values']['product_id'];
$product = commerce_product_load($product_id);
$order = commerce_cart_order_load($form_state['values']['uid']);
$order_wrapper = entity_metadata_wrapper('commerce_order', $order);
$line_item_types = commerce_line_item_types();
foreach ($form_state['extra_line_items'] as $id => $line_item) {
// If the line item passed to the function is new...
if (empty($line_item->line_item_id)) {
// Use different approach to create product vs non-product line items.
if (!empty($line_item_types[$line_item->type]['product'])) {
commerce_product_line_item_new($product, $form_state['values']['quantity'], 0, $form_state['line_item']->data, $form_state['line_item']->type);
}
else {
// Create the new product line item of the same type.
$line_item = entity_create('commerce_line_item', array(
'type' => $line_item->type,
'order_id' => $order->order_id,
'data' => $line_item->data,
));
$line_item_wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);
$line_item_wrapper->commerce_product = $product_id;
}
// Remove line item field values the user didn't have access to modify.
foreach ($form_state['values']['extra_line_items_fields'][$id] as $field_name => $value) {
// Note that we're checking the Commerce Cart settings that we inserted
// into this form element array back when we built the form. This means a
// module wanting to alter a line item field widget to be available must
// update both its form element's #access value and the field_access value
// of the #commerce_cart_settings array.
if (empty($form['extra_line_items_fields'][$id][$field_name]['#commerce_cart_settings']['field_access'])) {
unset($form_state['values']['extra_line_items_fields'][$id][$field_name]);
}
}
// Unset the line item field values array if it is now empty.
if (empty($form_state['values']['extra_line_items_fields'][$id])) {
unset($form_state['values']['extra_line_items_fields'][$id]);
}
// Add field data to the line item.
field_attach_submit('commerce_line_item', $line_item, $form['extra_line_items_fields'][$id], $form_state);
commerce_line_item_save($line_item);
$order_wrapper->commerce_line_items[] = $line_item;
}
}
commerce_order_save($order);
}
}
/**
* JS callback for quantity updates on "Add to cart" form.
*/
function commerce_custom_product_quantity_js($form, $form_state) {
return $form;
}
/**
* Implements hook_field_formatter_info_alter().
*/
function commerce_custom_product_field_formatter_info_alter(&$info) {
$info['commerce_cart_add_to_cart_form']['settings']['extra_line_items_field'] = NULL;
}
/**
* Returns an array with a list of line item type / product reference fields.
*
* @param string $entity_type
* The entity type to check.
* @param string|NULL $bundle
* The bundle to check.
*
* @return array
* List of line item type / product reference fields. The array is keyed by:
* - line_item_type_ref_fields: array of relevant line item type reference
* fields.
* - product_ref_fields: array of relevant product reference fields.
*/
function commerce_custom_product_get_field_options($entity_type, $bundle = NULL) {
$cache = &drupal_static(__FUNCTION__, array());
$cid = $entity_type . ':' . $bundle;
if (isset($cache[$cid])) {
return $cache[$cid];
}
$product_ref_fields = $line_item_type_ref_fields = array();
$instances = field_info_instances($entity_type, $bundle);
if (!$bundle) {
$all_instances = array();
foreach ($instances as $bundle_instances) {
$all_instances += $bundle_instances;
}
$instances = $all_instances;
}
foreach ($instances as $instance) {
$field = field_info_field($instance['field_name']);
if ($field['type'] == 'commerce_custom_product_line_item_type_reference') {
$line_item_type_ref_fields[$instance['field_name']] = $instance['label'];
}
elseif ($field['type'] == 'commerce_product_reference') {
$product_ref_fields[$instance['field_name']] = $instance['field_name'];
}
}
$cache[$cid] = array(
'line_item_type_ref_fields' => $line_item_type_ref_fields,
'product_ref_fields' => $product_ref_fields,
);
return $cache[$cid];
}
/**
* Implements hook_form_FORM_ID_alter() for field_ui_display_overview_form.
*
* Alters field display overview.
*/
function commerce_custom_product_form_field_ui_display_overview_form_alter(&$form, $form_state) {
// Check if we have both product reference and line item type reference
// fields.
$instances = field_info_instances($form['#entity_type'], $form['#bundle']);
$field_options = commerce_custom_product_get_field_options($form['#entity_type'], $form['#bundle']);
if (!empty($field_options['line_item_type_ref_fields']) && !empty($field_options['product_ref_fields'])) {
foreach ($field_options['product_ref_fields'] as $field_name) {
$extra_line_items_field = $instances[$field_name]['display'][$form['#view_mode']]['settings']['extra_line_items_field'];
if (!empty($form_state['values']['fields'][$field_name]['settings_edit_form']['settings']['extra_line_items_field'])) {
$extra_line_items_field = $form_state['values']['fields'][$field_name]['settings_edit_form']['settings']['extra_line_items_field'];
}
// Update field formatter settings summary with info about extra line
// items field.
if (!empty($form['fields'][$field_name]['settings_summary']['#markup'])) {
$items = explode('<br />', $form['fields'][$field_name]['settings_summary']['#markup']);
$last = array_pop($items);
array_push($items, str_replace('</div>', '', $last));
array_push($items, t('Extra line items field: @name', array('@name' => $extra_line_items_field ? $instances[$extra_line_items_field]['label'] : t('None defined'))) . '</div>');
$form['fields'][$field_name]['settings_summary']['#markup'] = implode('<br />', $items);
}
// Add form element for extra line items field configuration.
if (!empty($form['fields'][$field_name]['format']['settings_edit_form']['settings'])) {
$field_settings = $instances[$field_name]['display'][$form['#view_mode']]['settings'];
if (!empty($form_state['values']['fields'][$field_name]['settings_edit_form']['settings']['extra_line_items_field'])) {
$field_settings = $form_state['values']['fields'][$field_name]['settings_edit_form']['settings'];
}
$form['fields'][$field_name]['format']['settings_edit_form']['settings'] += commerce_custom_product_field_formatter_settings_form_element($field_settings, $form['#entity_type'], $form['#bundle']);
}
}
}
}
/**
* Implements hook_field_formatter_settings_form_alter().
*
* Ctools content type support for this field.
*/
function commerce_custom_product_field_formatter_settings_form_alter(&$settings_form, &$context) {
$context_name = key($context['form']['context']['#options']);
if (!empty($context['form']['context']['#default_value'])) {
$context_name = $context['form']['context']['#default_value'];
}
$pane_context = (isset($context['form_state']['contexts'][$context_name])) ? $context['form_state']['contexts'][$context_name] : NULL;
$entity_type = end($pane_context->type);
$field_settings = $context['instance']['display'][$context['view_mode']]['settings'];
$settings_form += commerce_custom_product_field_formatter_settings_form_element($field_settings, $entity_type);
}
/**
* Returns extra line item types field settings element.
*
* @param array $field_settings
* The current field settings.
* @param string $entity_type
* The entity type to check.
* @param string|NULL $bundle
* The bundle to check.
*
* @return array
* The configuration element(s).
*/
function commerce_custom_product_field_formatter_settings_form_element($field_settings, $entity_type, $bundle = NULL) {
$element = array();
// Check if we have both product reference and line item type reference
// fields.
$field_options = commerce_custom_product_get_field_options($entity_type, $bundle);
if (!empty($field_options['line_item_type_ref_fields']) && !empty($field_options['product_ref_fields'])) {
foreach ($field_options['product_ref_fields'] as $field_name) {
$extra_line_items_field = NULL;;
if (!empty($field_settings['extra_line_items_field'])) {
$extra_line_items_field = $field_settings['extra_line_items_field'];
}
$element['extra_line_items_field'] = array(
'#type' => 'select',
'#title' => t('Extra line item types field'),
'#description' => t('Choose a field that should be used as a source for configuration for any additional line items that should be added to the order with this product.'),
'#default_value' => $extra_line_items_field,
'#options' => array(0 => t('- None -')) + $field_options['line_item_type_ref_fields'],
);
}
}
return $element;
}