1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* Field handler to present an add to cart form for the product..
|
5
|
*/
|
6
|
class commerce_cart_handler_field_add_to_cart_form extends views_handler_field_entity {
|
7
|
|
8
|
function option_definition() {
|
9
|
$options = parent::option_definition();
|
10
|
|
11
|
$options['show_quantity'] = array('default' => FALSE);
|
12
|
$options['default_quantity'] = array('default' => 1);
|
13
|
$options['combine'] = array('default' => TRUE);
|
14
|
$options['display_path'] = array('default' => FALSE);
|
15
|
$options['line_item_type'] = array('product' => t('Product'));
|
16
|
|
17
|
return $options;
|
18
|
}
|
19
|
|
20
|
/**
|
21
|
* Provide the add to cart display options.
|
22
|
*/
|
23
|
function options_form(&$form, &$form_state) {
|
24
|
parent::options_form($form, $form_state);
|
25
|
|
26
|
$form['show_quantity'] = array(
|
27
|
'#type' => 'checkbox',
|
28
|
'#title' => t('Display a textfield quantity widget on the add to cart form.'),
|
29
|
'#default_value' => $this->options['show_quantity'],
|
30
|
);
|
31
|
|
32
|
$form['default_quantity'] = array(
|
33
|
'#type' => 'textfield',
|
34
|
'#title' => t('Default quantity'),
|
35
|
'#default_value' => $this->options['default_quantity'] <= 0 ? 1 : $this->options['default_quantity'],
|
36
|
'#element_validate' => array('commerce_cart_field_formatter_settings_form_quantity_validate'),
|
37
|
'#size' => 16,
|
38
|
);
|
39
|
|
40
|
$form['combine'] = array(
|
41
|
'#type' => 'checkbox',
|
42
|
'#title' => t('Attempt to combine like products on the same line item in the cart.'),
|
43
|
'#description' => t('The line item type, referenced product, and data from fields exposed on the Add to Cart form must all match to combine.'),
|
44
|
'#default_value' => $this->options['combine'],
|
45
|
);
|
46
|
|
47
|
// Add a conditionally visible line item type element.
|
48
|
$types = commerce_product_line_item_types();
|
49
|
|
50
|
if (count($types) > 1) {
|
51
|
$form['line_item_type'] = array(
|
52
|
'#type' => 'select',
|
53
|
'#title' => t('Add to Cart line item type'),
|
54
|
'#options' => array_intersect_key(commerce_line_item_type_get_name(), drupal_map_assoc($types)),
|
55
|
'#default_value' => !empty($this->options['line_item_type']) ? $this->options['line_item_type'] : 'product',
|
56
|
);
|
57
|
}
|
58
|
else {
|
59
|
$form['line_item_type'] = array(
|
60
|
'#type' => 'hidden',
|
61
|
'#value' => key($types),
|
62
|
);
|
63
|
}
|
64
|
|
65
|
if ($this->view->display[$this->view->current_display]->display_plugin == 'page') {
|
66
|
$title = t("Link products added to the cart from this page display to the View's path.");
|
67
|
}
|
68
|
else {
|
69
|
$title = t('Link products added to the cart from this display to the current path the customer is viewing where the View is rendered.');
|
70
|
}
|
71
|
|
72
|
$form['display_path'] = array(
|
73
|
'#type' => 'checkbox',
|
74
|
'#title' => $title,
|
75
|
'#default_value' => $this->options['display_path'],
|
76
|
);
|
77
|
}
|
78
|
|
79
|
function render($values) {
|
80
|
// Attempt to load the specified product.
|
81
|
$product = $this->get_value($values);
|
82
|
|
83
|
if (!empty($product)) {
|
84
|
// Extract a default quantity for the Add to Cart form line item.
|
85
|
$default_quantity = $this->options['default_quantity'] <= 0 ? 1 : $this->options['default_quantity'];
|
86
|
$product_ids = array($product->product_id);
|
87
|
|
88
|
// Build the line item we'll pass to the Add to Cart form.
|
89
|
$line_item = commerce_product_line_item_new($product, $default_quantity, 0, array(), $this->options['line_item_type']);
|
90
|
$line_item->data['context']['product_ids'] = $product_ids;
|
91
|
$line_item->data['context']['add_to_cart_combine'] = $this->options['combine'];
|
92
|
|
93
|
// Generate a form ID for this add to cart form.
|
94
|
$form_id = commerce_cart_add_to_cart_form_id($product_ids);
|
95
|
|
96
|
// Add the display path to the line item's context data array if specified.
|
97
|
if ($this->options['display_path']) {
|
98
|
if ($this->view->display[$this->view->current_display]->display_plugin == 'page') {
|
99
|
$line_item->data['context']['display_path'] = $this->view->display[$this->view->current_display]->display_options['path'];
|
100
|
}
|
101
|
else {
|
102
|
$line_item->data['context']['display_path'] = current_path();
|
103
|
}
|
104
|
}
|
105
|
|
106
|
// Store the View data in the context data array as well.
|
107
|
$line_item->data['context']['view'] = array(
|
108
|
'view_name' => $this->view->name,
|
109
|
'display_name' => $this->view->current_display,
|
110
|
'human_name' => $this->view->human_name,
|
111
|
'page' => $this->view->current_page,
|
112
|
);
|
113
|
|
114
|
// Build the Add to Cart form using the prepared values.
|
115
|
$form = drupal_get_form($form_id, $line_item, $this->options['show_quantity'], array());
|
116
|
|
117
|
return drupal_render($form);
|
118
|
}
|
119
|
}
|
120
|
}
|