forked from awesomemotive/easy-digital-downloads
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cart-functions.php
executable file
·524 lines (434 loc) · 11.7 KB
/
cart-functions.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
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
<?php
/**
* Cart Functions
*
* @package Easy Digital Downloads
* @subpackage Cart Functions
* @copyright Copyright (c) 2012, Pippin Williamson
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
* @since 1.0
*/
/**
* Get Cart Contents
*
* Retrieve contents from the cart.
*
* @access public
* @since 1.0
* @return array | false
*/
function edd_get_cart_contents() {
return isset($_SESSION['edd_cart']) ? apply_filters( 'edd_cart_contents', $_SESSION['edd_cart'] ) : false;
}
/**
* Get Cart Quantity
*
* Gets the total quanity of items cart.
*
* @access public
* @since 1.0
* @return INT - number of this item in the cart
*/
function edd_get_cart_quantity() {
$cart = edd_get_cart_contents();
if($cart)
$quantity = count($cart);
else
$quantity = 0;
return $quantity;
}
/**
* Add To Cart
*
* Adds a download ID to the shopping cart.
* Uses edd_get_cart_contents().
*
* @access public
* @since 1.0
* @param $download_id - INT the ID number of the download to add to the cart
* @param $options - array an array of options, such as variable price
* @return string - cart key of the new item
*/
function edd_add_to_cart($download_id, $options = array()) {
$cart = edd_get_cart_contents();
if(!edd_item_in_cart($download_id)) {
do_action( 'edd_pre_add_to_cart', $download_id, $options );
if(is_array($cart)) {
$cart[] = array('id' => $download_id, 'options' => $options);
} else {
$cart = array(array('id' => $download_id, 'options' => $options));
}
$_SESSION['edd_cart'] = $cart;
do_action( 'edd_post_add_to_cart', $download_id, $options );
// clear all the checkout errors, if any
edd_clear_errors();
return count($cart) - 1;
}
}
/**
* Remove From Cart
*
* Removes a download from the shopping cart.
* Uses edd_get_cart_contents().
*
* @access public
* @since 1.0
* @param $cart_key INT the cart key to remove
* @return array - of updated cart items
*/
function edd_remove_from_cart($cart_key) {
$cart = edd_get_cart_contents();
do_action( 'edd_pre_remove_from_cart', $cart_key );
if(!is_array($cart)) {
return true; // empty cart
} else {
unset($cart[$cart_key]);
}
$_SESSION['edd_cart'] = $cart;
do_action( 'edd_post_remove_from_cart', $cart_key );
// clear all the checkout errors, if any
edd_clear_errors();
return $cart; // the updated cart items
}
/**
* Item in Cart
*
* Checks to see if an item is already in the cart.
* Uses edd_get_cart_contents().
*
* @access public
* @since 1.0
* @param $download_id - INT the ID number of the download to remove
* @return boolean
*/
function edd_item_in_cart($download_id) {
$cart_items = edd_get_cart_contents();
if(!is_array($cart_items)) {
return false; // empty cart
} else {
foreach($cart_items as $item) {
if($item['id'] == $download_id) {
return true;
}
}
}
}
/**
* Get Item Position in Cart
*
* Gets the position of an item in the cart.
* Uses edd_get_cart_contents().
*
* @access public
* @since 1.0.7.2
* @param $download_id - INT the ID number of the download to remove
* @return $position - INT position of the item in the cart
*/
function edd_get_item_position_in_cart($download_id) {
$cart_items = edd_get_cart_contents();
if(!is_array($cart_items)) {
return false; // empty cart
} else {
foreach($cart_items as $postion => $item) {
if($item['id'] == $download_id) {
return $postion;
}
}
}
}
/**
* Get Cart Item Quantity
*
* Gets the quanity for an item in the cart.
*
* @access public
* @since 1.0
* @param $item INT the download (cart item) ID number
* @return $position - INT position of the item in the cart
*/
function edd_get_cart_item_quantity($item) {
$cart = edd_get_cart_contents();
$item_counts = array_count_values($cart);
$quantity = $item_counts[$item];
return $quantity;
}
/**
* Get Cart Item Price
*
* Gets the price of the cart item.
*
* @access public
* @since 1.0
* @param $item INT the download ID number
* @param $options array optional parameters, used for defining variable prices
* @return string - price for this item
*/
function edd_get_cart_item_price($item_id, $options = array()) {
$variable_pricing = get_post_meta( $item_id, '_variable_pricing', true) ;
$price = get_post_meta( $item_id, 'edd_price', true );
if( $variable_pricing && !empty( $options ) ) {
// if variable prices are enabled, retrieve the options
$prices = get_post_meta( $item_id, 'edd_variable_prices', true );
if( $prices ) {
$price = $prices[$options['price_id']]['amount'];
}
}
return apply_filters( 'edd_cart_item_price', $price );
}
/**
* Formats a cart item price with decimal places and currency sign
*
* Displays a formatted price for an item in the cart.
*
* @access public
* @since 1.1.9
* @param float the price to format
* @return float
*/
function edd_format_cart_item_price( $price ) {
return number_format( (float) $price, 2 );
}
add_filter( 'edd_cart_item_price', 'edd_format_cart_item_price' );
/**
* Get Price Name
*
* Gets the name of the specified price option,
* for variable pricing only.
*
* @access public
* @since 1.0
* @param $item INT the download ID number
* @param $options array optional parameters, used for defining variable prices
* @return string - the name of the price option
*/
function edd_get_price_name($item_id, $options = array()) {
$variable_pricing = get_post_meta($item_id, '_variable_pricing', true);
if($variable_pricing && !empty($options)) {
// if variable prices are enabled, retrieve the options
$prices = get_post_meta($item_id, 'edd_variable_prices', true);
if($prices) {
$name = $prices[$options['price_id']]['name'];
}
return $name;
}
return false;
}
/**
* Get Cart Amount
*
* Gets the total price amount in the cart.
* uses edd_get_cart_contents().
*
* @access public
* @since 1.0
* @return string - the name of the price option
*/
function edd_get_cart_amount() {
$cart_items = edd_get_cart_contents();
$amount = 0;
if($cart_items) {
foreach($cart_items as $item) {
$item_price = edd_get_cart_item_price($item['id'], $item['options']);
$amount = $amount + $item_price;
}
if(isset($_POST['edd-discount']) && $_POST['edd-discount'] != '') {
// discount is validated before this function runs, so no need to check for it
$amount = edd_get_discounted_amount($_POST['edd-discount'], $amount);
}
return $amount;
}
return 0;
}
/**
* Format Amount
*
* Returns a nicely formatted amount.
*
* @access public
* @since 1.0
* @param $amount string the price amount to format
* @param $options array optional parameters, used for defining variable prices
* @return string - the newly formatted amount
*/
function edd_format_amount($amount) {
global $edd_options;
$thousands_sep = isset($edd_options['thousands_separator']) ? $edd_options['thousands_separator'] : ',';
$decimal_sep = isset($edd_options['decimal_separator']) ? $edd_options['decimal_separator'] : '.';
return number_format($amount, 2, $decimal_sep, $thousands_sep);
}
/**
* Get Purchase Summary
*
* Retrieves the purchase summary.
*
* @access public
* @since 1.0
* @return string
*/
function edd_get_purchase_summary($purchase_data, $email = true) {
$summary = '';
if($email) {
$summary .= $purchase_data['user_email'] . ' - ';
}
foreach($purchase_data['downloads'] as $download) {
$summary .= get_the_title($download['id']) . ', ';
}
$summary = substr($summary, 0, -2);
return $summary;
}
/**
* Get Cart Content Details
*
* Retrieves the cart contnet details.
*
* @access public
* @since 1.0
* @return array
*/
function edd_get_cart_content_details() {
$cart_items = edd_get_cart_contents();
$details = array();
if($cart_items) {
foreach($cart_items as $key => $item) {
$details[$key] = array(
'name' => get_the_title($item['id']),
'id' => $item['id'],
'item_number' => $item,
'price' => edd_get_cart_item_price($item['id'], $item['options']),
'quantity' => 1,
);
}
}
if(!empty($details)) {
return $details;
}
return false;
}
/**
* Add Collection to Cart
*
* Adds all downloads within a taxonomy term to the cart.
*
* @access public
* @since 1.0.6
* @param $taxonomy string - the name of the taxonomy
* @param $terms mixed - the slug or id of the term from which to add ites, or an array of terms
* @return array of IDs for each item added to the cart
*/
function edd_add_collection_to_cart($taxonomy, $terms) {
if(!is_string($taxonomy)) return false;
$field = is_int($terms) ? 'id' : 'slug';
$cart_item_ids = array();
$args = array(
'post_type' => 'download',
'posts_per_page' => -1,
$taxonomy => $terms
);
$items = get_posts($args);
if($items) {
foreach($items as $item) {
edd_add_to_cart($item->ID);
$cart_item_ids[] = $item->ID;
}
}
return $cart_item_ids;
}
/**
* Remove Item URL
*
* Returns the URL to remove an item.
*
* @access public
* @since 1.0
* @return string
*/
function edd_remove_item_url($cart_key, $post, $ajax = false) {
global $post;
$current_page = edd_get_current_page_url();
$remove_url = add_query_arg( array('cart_item' => $cart_key, 'edd_action' => 'remove' ), $current_page);
return apply_filters('edd_remove_item_url', $remove_url);
}
/**
* Show Added To Cart Messages
*
* Renders the added to cart messages.
*
* @access private
* @since 1.0
* @return void
*/
function edd_show_added_to_cart_messages($download_id) {
if( isset( $_POST['edd_action'] ) && $_POST['edd_action'] == 'add_to_cart' ) {
$alert = sprintf( __('You have successfully added %s to your shopping cart.', 'edd'), get_the_title( $download_id ) );
$alert .= ' <a href="' . edd_get_checkout_uri() . '" class="edd_alert_checkout_link">' . __('Checkout.', 'edd') . '</a>';
echo '<div class="edd_added_to_cart_alert">' . $alert . '</div>';
}
}
add_action('edd_after_download_content', 'edd_show_added_to_cart_messages');
/**
* Get Checkout URI
*
* Retrieves the URL of the checkout page.
*
* @access public
* @since 1.0.8
* @return mixed - the full URL to the checkout page, if present, NULL if it doesn't exist
*/
function edd_get_checkout_uri() {
global $edd_options;
return isset( $edd_options['purchase_page'] ) ? get_permalink( $edd_options['purchase_page'] ) : NULL;
}
/**
* Checks if on checkout page
*
* Determines if the current page is the checkout page
*
* @access public
* @since 1.1.2
* @return bool - true if on the page, false otherwise
*/
function edd_is_checkout() {
global $edd_options;
return isset( $edd_options['purchase_page'] ) ? is_page( $edd_options['purchase_page'] ) : false;
}
/**
* Empty Cart
*
* Empties the cart.
*
* @access public
* @since 1.0
* @return void
*/
function edd_empty_cart() {
$_SESSION['edd_cart'] = NULL;
}
/**
* Store Purchase Data in Sessions
*
* Used for storing info about purchase
*
* @access public
* @since 1.1.5
* @return void
*/
function edd_set_purchase_session( $purchase_data ) {
$_SESSION['edd_purchase_info'] = $purchase_data;
}
/**
* Retrieve Purchase Data from Session
*
* Used for retrieving info about purchase
* after completing a purchase
*
* @access public
* @since 1.1.5
* @return array / false
*/
function edd_get_purchase_session() {
return isset( $_SESSION['edd_purchase_info'] ) ? $_SESSION['edd_purchase_info'] : false;
}
// make sure a session is started
if(!session_id()){
add_action( 'init', 'session_start', -1 );
}