-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple-product-options-frontend.php
221 lines (130 loc) · 7 KB
/
simple-product-options-frontend.php
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
<?php
class wpec_simple_product_options_frontend {
function __construct() {
// Original version
add_action ( 'wpsc_product_form_fields', array ( &$this, 'display_product_options' ) );
// Actual hook added to WPEC
add_action ( 'wpsc_product_form_fields_begin', array ( &$this, 'display_product_options' ) );
add_filter ( 'wpsc_add_to_cart_product_id', array ( &$this, 'parse_product_options' ) );
// Show the personalisation information during checkout
add_action ( 'wpsc_after_checkout_cart_item_name', array ( &$this, 'checkout_personalisation_information' ) );
add_action ( 'wpsc_after_cart_widget_item_name', array ( &$this, 'cart_widget_personalisation_information' ) );
add_action( 'init', array( $this, 'init' ) );
}
public function init() {
global $wpec_show_personalisation;
remove_action( 'wpsc_after_checkout_cart_item_name', array( $wpec_show_personalisation, 'checkout_personalisation_information' ) );
remove_action( 'wpsc_after_cart_widget_item_name', array( $wpec_show_personalisation, 'cart_widget_personalisation_information' ) );
}
public function checkout_personalisation_information() {
$this->show_personalisation_information ( 'checkout' );
}
public function cart_widget_personalisation_information() {
$this->show_personalisation_information ( 'cart_widget' );
}
public function show_personalisation_information($context) {
global $wpsc_cart;
// Seperate out the options into individual items
$options = explode ( apply_filters ( 'wpec_spo_between_options_db', '; ' ), $wpsc_cart->cart_item->custom_message );
echo apply_filters ( 'wpec_spo_before_options', '<br/><span class="wpec_product_option_'.esc_attr ( $context ).'_text">', $context );
$cnt = 0 ;
foreach ( $options as $option ) {
if ( $cnt )
echo nl2br ( esc_html ( apply_filters ( 'wpec_spo_between_options', '; ', $context ) ) );
echo nl2br ( esc_html ( $option ) );
$cnt++;
}
echo apply_filters ( 'wpec_spo_after_options', '</span>', $context );
}
function parse_product_options( $product_id ) {
if ( isset ( $_POST['wpec-product-option'] ) && count ( $_POST['wpec-product-option'] ) ) {
// Flag the product as personalisable.
// Put it into POST for WPEC pre-4.0, REQUEST for post-4.0
$_POST['is_customisable'] = 'true';
$_REQUEST['is_customisable'] = 'true';
// Construct the options into a string
$cnt = 0;
$custom_text = isset ( $_POST['custom_text'] ) ? $_POST['custom_text']."\n" : '';
foreach ( $_POST['wpec-product-option'] as $parent_term => $term ) {
$parent = get_term_by ( 'id', $parent_term, 'wpec_product_option' );
$child = get_term_by ( 'id', $term, 'wpec_product_option' );
/* Filter wpec_spo_between_options_db allows you to change the separator used between options when
* storing the data in the database. DO NOT use this to change how the information is displayed in
* the cart or during checkout. Only use this if your options, or values contain the default
* separator ";"
*/
if ( $cnt )
$custom_text .= apply_filters ( 'wpec_spo_between_options_db', '; ' );
$custom_text .= $parent->name . ': ' . $child->name;
$cnt++;
}
// Put it into POST for WPEC pre-4.0, REQUEST for post-4.0
$_POST['custom_text'] = $custom_text;
$_REQUEST['custom_text'] = $custom_text;
}
return $product_id;
}
function sort_product_option_sets ( $a, $b ) {
$a_order = isset ( $a['option_set_info']->term_order ) ? $a['option_set_info']->term_order : 0;
$b_order = isset ( $b['option_set_info']->term_order ) ? $b['option_set_info']->term_order : 0;
if ( $a_order == $b_order)
return 0;
if ( $a_order > $b_order)
return 1;
return -1;
}
function sort_product_options( $a, $b ) {
$a_order = isset ( $a->term_order ) ? $a->term_order : 0;
$b_order = isset ( $b->term_order ) ? $b->term_order : 0;
if ( $a_order == $b_order)
return 0;
if ( $a_order > $b_order)
return 1;
return -1;
}
function display_product_options() {
// Retrieve the product options for this product
$product_id = wpsc_the_product_id();
$options = wp_get_object_terms ( $product_id, 'wpec_product_option', array ( 'orderby' => 'parent', 'order' => 'asc' ) );
$options = apply_filters ( 'wpec_spo_product_options', $options, $product_id );
if ( ! count ( $options ) )
return;
// Re-arrange to an array structure suitable for output
foreach ($options as $option) {
if ( $option->parent == 0 )
continue;
// Add to array
if ( isset ( $output_array[$option->parent] ) ) {
// Already have the parent info - just add the child
$output_array[$option->parent]['options'][] = $option;
} else {
// Grab the parent info AND add this as a child
$parent = get_term_by ( 'id', $option->parent, 'wpec_product_option' );
// No sensible way to structure multi-level options
if ( $parent->parent != 0 )
continue;
$output_array[$parent->term_id] = Array ( 'option_set_info' => $parent,
'options' => Array ( $option )
);
}
}
if ( ! isset ( $output_array) || ! count ( $output_array ) )
return;
usort ( $output_array, array ( &$this, 'sort_product_option_sets' ) );
$output_array = apply_filters ( 'wpec_spo_product_options_output_array', $output_array, $product_id);
foreach ( $output_array as $option_set ) {
if ( ! empty ( $option_set['options'] ) ) {
// Sort by term order
usort ( $option_set['options'], array ( &$this, 'sort_product_options' ) );
$option_set_info = &$option_set['option_set_info'];
echo "<span class=\"wpec-product-option-title\">".esc_html($option_set_info->name).": </span>";
echo "<select class=\"wpec-product-option-select\" name=\"wpec-product-option[".esc_attr($option_set_info->term_id)."]\">";
foreach ( $option_set['options'] as $option ) {
echo "<option value=\"".esc_attr($option->term_id)."\">".esc_html($option->name)."</option>";
}
echo "</select><br/>";
}
}
}
}
$wpec_simple_product_options_frontend = new wpec_simple_product_options_frontend();