1
|
|
|
<?php |
2
|
|
|
class WPSC_Controller_Cart extends WPSC_Controller { |
3
|
|
|
public function __construct() { |
4
|
|
|
parent::__construct(); |
5
|
|
|
require_once( WPSC_TE_V2_CLASSES_PATH . '/cart-item-table.php' ); |
6
|
|
|
require_once( WPSC_TE_V2_CLASSES_PATH . '/cart-item-table-form.php' ); |
7
|
|
|
$this->view = 'cart'; |
8
|
|
|
$this->title = wpsc_get_cart_title(); |
9
|
|
|
$this->init_cart_item_table(); |
10
|
|
|
} |
11
|
|
|
|
12
|
|
|
private function init_cart_item_table() { |
13
|
|
|
$cart_item_table = WPSC_Cart_Item_Table_Form::get_instance(); |
14
|
|
|
$cart_item_table->show_tax = false; |
15
|
|
|
$cart_item_table->show_shipping = false; |
16
|
|
|
$cart_item_table->show_total = false; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public function add( $product_id ) { |
20
|
|
|
global $wpsc_cart; |
21
|
|
|
|
22
|
|
|
if ( ! wp_verify_nonce( $_REQUEST['_wp_nonce'], "wpsc-add-to-cart-{$product_id}" ) ) { |
23
|
|
|
wp_die( __( 'Request expired. Please try adding the item to your cart again.', 'wp-e-commerce' ) ); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
extract( $_REQUEST, EXTR_SKIP ); |
|
|
|
|
27
|
|
|
|
28
|
|
|
$defaults = array( |
29
|
|
|
'variation_values' => array(), |
30
|
|
|
'quantity' => 1, |
31
|
|
|
'provided_price' => null, |
32
|
|
|
'comment' => null, |
33
|
|
|
'time_requested' => null, |
34
|
|
|
'custom_message' => '', |
35
|
|
|
'file_data' => null, |
36
|
|
|
'is_customisable' => false, |
37
|
|
|
'meta' => null, |
38
|
|
|
); |
39
|
|
|
|
40
|
|
|
$provided_parameters = array(); |
41
|
|
|
$product_id = apply_filters( 'wpsc_add_to_cart_product_id', (int) $product_id ); |
42
|
|
|
|
43
|
|
|
if ( ! empty( $wpsc_product_variations ) ) { |
44
|
|
|
|
45
|
|
|
foreach ( $wpsc_product_variations as $key => $variation ) { |
46
|
|
|
$provided_parameters['variation_values'][ (int) $key ] = (int) $variation; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$variation_product_id = wpsc_get_child_object_in_terms( $product_id, $provided_parameters['variation_values'], 'wpsc-variation' ); |
50
|
|
|
|
51
|
|
|
if ( $variation_product_id > 0 ) { |
52
|
|
|
$product_id = $variation_product_id; |
53
|
|
|
} else { |
54
|
|
|
$this->message_collection->add( __( 'This variation combination is no longer available. Please choose a different combination.', 'wp-e-commerce' ), 'error', 'main', 'flash' ); |
55
|
|
|
wp_safe_redirect( wpsc_get_cart_url() ); |
56
|
|
|
exit; |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if ( ! empty( $quantity ) ) { |
61
|
|
|
$provided_parameters['quantity'] = (int) $quantity; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if ( ! empty( $is_customisable ) ) { |
65
|
|
|
$provided_parameters['is_customisable'] = true; |
66
|
|
|
|
67
|
|
|
if ( isset( $custom_text ) ) { |
68
|
|
|
$provided_parameters['custom_message'] = $custom_text; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
if ( isset( $_FILES['custom_file'] ) ) { |
72
|
|
|
$provided_parameters['file_data'] = $_FILES['custom_file']; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
if ( isset( $donation_price ) && (float) $donation_price > 0 ) { |
77
|
|
|
$provided_parameters['provided_price'] = (float) $donation_price; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$parameters = array_merge( $defaults, $provided_parameters ); |
81
|
|
|
|
82
|
|
|
if ( $parameters['quantity'] <= 0 ) { |
83
|
|
|
$this->message_collection->add( __( 'Sorry, but the quantity you just entered is not valid. Please try again.', 'wp-e-commerce' ), 'error', 'main', 'flash' ); |
84
|
|
|
return; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$product = apply_filters( 'wpsc_add_to_cart_product_object', get_post( $product_id, OBJECT, 'display' ) ); |
88
|
|
|
|
89
|
|
|
$stock = get_post_meta( $product_id, '_wpsc_stock', true ); |
90
|
|
|
|
91
|
|
|
$remaining_quantity = $wpsc_cart->get_remaining_quantity( $product_id, $parameters['variation_values'] ); |
92
|
|
|
|
93
|
|
|
if ( $stock !== '' && $remaining_quantity !== true ) { |
94
|
|
|
if ( $remaining_quantity <= 0 ) { |
95
|
|
|
$message = apply_filters( 'wpsc_add_to_cart_out_of_stock_message', __( 'Sorry, the product "%s" is out of stock.', 'wp-e-commerce' ) ); |
96
|
|
|
$this->message_collection->add( sprintf( $message, $product->post_title ), 'error', 'main', 'flash' ); |
97
|
|
|
wp_safe_redirect( wpsc_get_cart_url() ); |
98
|
|
|
exit; |
99
|
|
|
} elseif ( $remaining_quantity < $parameters['quantity'] ) { |
100
|
|
|
$message = __( 'Sorry, but the quantity you just specified is larger than the available stock. There are only %d of the item in stock.', 'wp-e-commerce' ); |
101
|
|
|
$this->message_collection->add( sprintf( $message, $remaining_quantity ), 'error', 'main', 'flash' ); |
102
|
|
|
wp_safe_redirect( wpsc_get_cart_url() ); |
103
|
|
|
exit; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
if ( wpsc_product_has_variations( $product_id ) && is_null( $parameters['variation_values'] ) ) { |
108
|
|
|
$message = apply_filters( 'wpsc_add_to_cart_variation_missing_message', sprintf( __( 'This product has several options to choose from.<br /><br /><a href="%s" style="display:inline; float:none; margin: 0; padding: 0;">Visit the product page</a> to select options.', 'wp-e-commerce' ), esc_url( get_permalink( $product_id ) ) ), $product_id ); |
109
|
|
|
$this->message_collection->add( sprintf( $message, $product->post_title ), 'error', 'main', 'flash' ); |
110
|
|
|
wp_safe_redirect( wpsc_get_cart_url() ); |
111
|
|
|
exit; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
if ( $wpsc_cart->set_item( $product_id, $parameters ) ) { |
115
|
|
|
$message = sprintf( __( 'You just added %s to your cart.', 'wp-e-commerce' ), $product->post_title ); |
116
|
|
|
$this->message_collection->add( $message, 'success', 'main', 'flash' ); |
117
|
|
|
wp_safe_redirect( wpsc_get_cart_url() ); |
118
|
|
|
exit; |
119
|
|
|
} else { |
120
|
|
|
$this->message_collection->add( __( 'An unknown error just occurred. Please contact the shop administrator.', 'wp-e-commerce' ), 'error', 'main', 'flash' ); |
121
|
|
|
wp_safe_redirect( wpsc_get_cart_url() ); |
122
|
|
|
exit; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function _callback_update_quantity() { |
128
|
|
|
global $wpsc_cart; |
129
|
|
|
|
130
|
|
|
if ( ! wp_verify_nonce( $_REQUEST['_wp_nonce'], 'wpsc-cart-update' ) ) { |
131
|
|
|
wp_die( __( 'Request expired. Please try updating the items in your cart again.', 'wp-e-commerce' ) ); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$changed = 0; |
135
|
|
|
$has_errors = false; |
136
|
|
|
|
137
|
|
|
extract( $_REQUEST, EXTR_SKIP ); |
|
|
|
|
138
|
|
|
|
139
|
|
|
foreach ( $wpsc_cart->cart_items as $key => &$item ) { |
140
|
|
|
if ( isset( $quantity[ $key ] ) && $quantity[ $key ] != $item->quantity ) { |
141
|
|
|
|
142
|
|
|
$product = get_post( $item->product_id ); |
143
|
|
|
|
144
|
|
|
if ( ! is_numeric( $quantity[ $key ] ) ) { |
145
|
|
|
$message = sprintf( __( 'Invalid quantity for %s.', 'wp-e-commerce' ), $product->post_title ); |
146
|
|
|
$this->message_collection->add( $message, 'error' ); |
147
|
|
|
continue; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
if ( $quantity[ $key ] < wpsc_product_min_cart_quantity( $item->product_id ) ) { |
151
|
|
|
$message = __( 'Sorry, but the quantity you just specified is lower than the minimum allowed quantity of %s. You must purchase at least %s at a time.', 'wp-e-commerce' ); |
152
|
|
|
$this->message_collection->add( sprintf( $message, $product->post_title, number_format_i18n( wpsc_product_min_cart_quantity( $item->product_id ) ) ), 'error' ); |
153
|
|
|
$has_errors = true; |
154
|
|
|
continue; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
if ( $quantity[ $key ] > $item->quantity ) { |
158
|
|
|
$product = WPSC_Product::get_instance( $item->product_id ); |
159
|
|
|
|
160
|
|
|
if ( ! $product->has_stock ) { |
161
|
|
|
$message = __( "Sorry, all the remaining stock of %s has been claimed. You can only checkout with the current quantity in your cart.", 'wp-e-commerce' ); |
162
|
|
|
$this->message_collection->add( sprintf( $message, $product->post->post_title ), 'error' ); |
163
|
|
|
$has_errors = true; |
164
|
|
|
continue; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
if ( $product->has_limited_stock && $product->stock < $item->quantity ) { |
168
|
|
|
$message = __( 'Sorry, but the quantity you just specified is larger than the available stock of %s. Besides the current quantity of that product in your cart, you can only add %d more.', 'wp-e-commerce' ); |
169
|
|
|
$this->message_collection->add( sprintf( $message, $product->post->post_title, $product->stock ), 'error' ); |
170
|
|
|
$has_errors = true; |
171
|
|
|
continue; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
if ( $quantity[ $key ] > wpsc_product_max_cart_quantity( $item->product_id ) ) { |
175
|
|
|
$message = __( 'Sorry, but the quantity you just specified is larger than the maximum allowed quantity of %s. You may only purchase %s at a time.', 'wp-e-commerce' ); |
176
|
|
|
$this->message_collection->add( sprintf( $message, $product->post->post_title, number_format_i18n( wpsc_product_max_cart_quantity( $item->product_id ) ) ), 'error' ); |
177
|
|
|
$has_errors = true; |
178
|
|
|
continue; |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
$item->quantity = $quantity[ $key ]; |
183
|
|
|
$item->refresh_item(); |
184
|
|
|
$changed++; |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
$wpsc_cart->clear_cache(); |
189
|
|
|
|
190
|
|
|
if ( ! isset( $_POST['update_quantity'] ) && ! $has_errors ) { |
191
|
|
|
wp_redirect( wpsc_get_checkout_url() ); |
192
|
|
|
exit; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
if ( $changed ) { |
196
|
|
|
$message = _n( 'You just successfully updated the quantity for %d item.', 'You just successfully updated the quantity for %d items.', $changed, 'wp-e-commerce' ); |
197
|
|
|
$this->message_collection->add( sprintf( $message, $changed ), 'success' ); |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
public function index() { |
202
|
|
|
|
203
|
|
|
if ( isset( $_POST['apply_coupon'] ) && empty( $_POST['coupon_code'] ) ) { |
204
|
|
|
$this->_callback_remove_coupon(); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
if ( isset( $_POST['apply_coupon'] ) && isset( $_POST['coupon_code'] ) ) { |
208
|
|
|
$this->_callback_apply_coupon(); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
if ( isset( $_POST['action'] ) && $_POST['action'] == 'update_quantity' ) { |
212
|
|
|
$this->_callback_update_quantity(); |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
public function _callback_remove_coupon() { |
217
|
|
|
global $wpsc_cart; |
218
|
|
|
|
219
|
|
|
$wpsc_cart->coupons_amount = 0; |
220
|
|
|
$wpsc_cart->coupons_name = ''; |
221
|
|
|
wpsc_delete_customer_meta( 'coupon' ); |
222
|
|
|
|
223
|
|
|
$this->message_collection->add( __( 'Coupon removed.', 'wp-e-commerce' ), 'error', 'main', 'flash' ); |
224
|
|
|
|
225
|
|
|
wp_safe_redirect( wpsc_get_cart_url() ); |
226
|
|
|
exit; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
public function _callback_apply_coupon() { |
230
|
|
|
global $wpsc_coupons; |
231
|
|
|
|
232
|
|
|
wpsc_coupon_price( $_POST['coupon_code'] ); |
233
|
|
|
|
234
|
|
|
$coupon = wpsc_get_customer_meta( 'coupon' ); |
235
|
|
|
|
236
|
|
|
if ( ! empty( $coupon ) ) { |
237
|
|
|
$wpsc_coupons = new wpsc_coupons( $coupon ); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
if ( $wpsc_coupons->errormsg || empty( $_POST['coupon_code'] ) ) { |
241
|
|
|
$this->message_collection->add( __( 'Coupon not applied.', 'wp-e-commerce' ), 'error', 'main', 'flash' ); |
242
|
|
|
} else { |
243
|
|
|
$this->message_collection->add( __( 'Coupon applied.', 'wp-e-commerce' ), 'success', 'main', 'flash' ); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
wp_safe_redirect( wpsc_get_cart_url() ); |
247
|
|
|
exit; |
248
|
|
|
|
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
public function clear() { |
252
|
|
|
global $wpsc_cart; |
253
|
|
|
|
254
|
|
|
if ( ! wp_verify_nonce( $_REQUEST['_wp_nonce'], 'wpsc-clear-cart' ) ) { |
255
|
|
|
wp_die( __( 'Request expired. Please go back and try clearing the cart again.', 'wp-e-commerce' ) ); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
$wpsc_cart->empty_cart(); |
259
|
|
|
$this->message_collection->add( __( 'Shopping cart emptied.', 'wp-e-commerce' ) ); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
public function remove( $key ) { |
263
|
|
|
global $wpsc_cart; |
264
|
|
|
|
265
|
|
|
if ( ! wp_verify_nonce( $_REQUEST['_wp_nonce'], "wpsc-remove-cart-item-{$key}" ) ) { |
266
|
|
|
wp_die( __( 'Request expired. Please go back and try removing the cart item again.', 'wp-e-commerce' ) ); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
$wpsc_cart->remove_item( $key ); |
270
|
|
|
$this->message_collection->add( __( 'Item removed.', 'wp-e-commerce' ), 'success', 'main', 'flash' ); |
271
|
|
|
|
272
|
|
|
wp_safe_redirect( wpsc_get_cart_url() ); |
273
|
|
|
exit; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
} |
277
|
|
|
|