1 <?php
2 3 4 5 6 7 8 9 10
11
12
13 if ( ! defined( 'ABSPATH' ) ) exit;
14
15 16 17 18 19 20
21 function edd_get_cart_contents() {
22 $cart = EDD()->session->get( 'edd_cart' );
23 return ! empty( $cart ) ? apply_filters( 'edd_cart_contents', $cart ) : false;
24 }
25
26 27 28 29 30 31
32 function edd_get_cart_quantity() {
33 $cart = edd_get_cart_contents();
34 if ( $cart )
35 $quantity = count( $cart );
36 else
37 $quantity = 0;
38 return $quantity;
39 }
40
41 42 43 44 45 46 47 48 49 50 51 52
53 function edd_add_to_cart( $download_id, $options = array() ) {
54 $cart = edd_get_cart_contents();
55 if ( ! edd_item_in_cart( $download_id, $options ) ) {
56 if( 'download' != get_post_type( $download_id ) )
57 return;
58
59 do_action( 'edd_pre_add_to_cart', $download_id, $options );
60
61 if ( edd_has_variable_prices( $download_id ) && ! isset( $options['price_id'] ) ) {
62
63 $options['price_id'] = 0;
64 }
65
66 $to_add = array();
67
68 if ( isset( $options['price_id'] ) && is_array( $options['price_id'] ) ) {
69
70 foreach ( $options['price_id'] as $price ) {
71 $price_options = array( 'price_id' => $price );
72 $to_add[] = apply_filters( 'edd_add_to_cart_item', array( 'id' => $download_id, 'options' => $price_options ) );
73 }
74 } else {
75
76 $to_add[] = apply_filters( 'edd_add_to_cart_item', array( 'id' => $download_id, 'options' => $options ) );
77 }
78
79 if ( is_array( $cart ) ) {
80 $cart = array_merge( $cart, $to_add );
81 } else {
82 $cart = $to_add;
83 }
84
85 EDD()->session->set( 'edd_cart', $cart );
86
87 do_action( 'edd_post_add_to_cart', $download_id, $options );
88
89
90 edd_clear_errors();
91
92 return count( $cart ) - 1;
93 }
94 }
95
96 97 98 99 100 101 102
103 function edd_remove_from_cart( $cart_key ) {
104 $cart = edd_get_cart_contents();
105
106 do_action( 'edd_pre_remove_from_cart', $cart_key );
107
108 if ( ! is_array( $cart ) ) {
109 return true;
110 } else {
111 unset( $cart[ $cart_key ] );
112 }
113
114 EDD()->session->set( 'edd_cart', $cart );
115
116 do_action( 'edd_post_remove_from_cart', $cart_key );
117
118
119 edd_clear_errors();
120
121 return $cart;
122 }
123
124 125 126 127 128 129 130
131 function edd_item_in_cart( $download_id = 0, $options = array() ) {
132 $cart_items = edd_get_cart_contents();
133
134 $ret = false;
135
136 if ( is_array( $cart_items ) ) {
137 foreach ( $cart_items as $item ) {
138 if ( $item['id'] == $download_id ) {
139 if ( isset( $options['price_id'] ) && isset( $item['options']['price_id'] ) ) {
140 if ( $options['price_id'] == $item['options']['price_id'] ) {
141 $ret = true;
142 break;
143 }
144 } else {
145 $ret = true;
146 break;
147 }
148 }
149 }
150 }
151
152 return (bool) apply_filters( 'edd_item_in_cart', $ret, $download_id, $options );
153 }
154
155 156 157 158 159 160 161
162 function edd_get_item_position_in_cart( $download_id ) {
163 $cart_items = edd_get_cart_contents();
164 if ( ! is_array( $cart_items ) ) {
165 return false;
166 } else {
167 foreach ( $cart_items as $position => $item ) {
168 if ( $item['id'] == $download_id ) {
169 return $position;
170 }
171 }
172 }
173 }
174
175 176 177 178 179 180 181
182 function edd_get_cart_item_quantity( $item ) {
183 $cart = edd_get_cart_contents();
184 $item_counts = array_count_values( $cart );
185 $quantity = $item_counts[ $item ];
186 return $quantity;
187 }
188
189 190 191 192 193 194 195 196
197 function edd_cart_item_price( $item_id = 0, $options = array() ) {
198 global $edd_options;
199
200 $price = edd_get_cart_item_price( $item_id, $options );
201 $label = '';
202
203 if ( edd_is_cart_taxed() ) {
204
205 if ( ! edd_prices_show_tax_on_checkout() && edd_prices_include_tax() ) {
206 $label .= ' ' . __( '(ex. tax)', 'edd' );
207 }
208
209 if ( edd_prices_show_tax_on_checkout() && ! edd_prices_include_tax() ) {
210 $label .= ' ' . __( '(incl. tax)', 'edd' );
211 }
212
213 }
214
215 $price = edd_currency_filter( edd_format_amount( $price ) );
216
217 return esc_html( $price . $label );
218 }
219
220 221 222 223 224 225 226 227 228 229
230 function edd_get_cart_item_price( $item_id, $options = array(), $tax = true ) {
231 global $edd_options;
232
233 $price = edd_get_download_price( $item_id );
234
235
236 $variable_pricing = get_post_meta( $item_id, '_variable_pricing', true) ;
237
238 if ( $variable_pricing && ! empty( $options ) ) {
239
240 $prices = get_post_meta( $item_id, 'edd_variable_prices', true );
241 if ( $prices ) {
242 $price = isset( $prices[ $options['price_id'] ] ) ? $prices[ $options['price_id'] ]['amount'] : $price;
243 }
244 }
245
246
247 if ( $tax &&
248 (
249 ( edd_prices_include_tax() && ! edd_is_cart_taxed() && edd_use_taxes() ) ||
250 ( edd_is_cart_taxed() && edd_prices_show_tax_on_checkout() || ( ! edd_prices_show_tax_on_checkout() && edd_prices_include_tax() ) )
251 )
252 ) {
253 $price = edd_calculate_tax( $price );
254 }
255
256 return apply_filters( 'edd_cart_item_price', $price, $item_id, $options );
257 }
258
259 260 261 262 263 264 265 266 267 268 269
270 function edd_get_price_name( $item_id, $options = array() ) {
271 $return = false;
272 $variable_pricing = get_post_meta($item_id, '_variable_pricing', true);
273 if( $variable_pricing && !empty( $options ) ) {
274
275 $prices = get_post_meta( $item_id, 'edd_variable_prices', true );
276 $name = false;
277 if( $prices ) {
278 if( isset( $prices[ $options['price_id'] ] ) )
279 $name = $prices[ $options['price_id'] ]['name'];
280 }
281 $return = $name;
282 }
283 return apply_filters( 'edd_get_price_name', $return, $item_id, $options );
284 }
285
286 287 288 289 290 291 292 293 294
295 function edd_cart_subtotal() {
296 global $edd_options;
297
298 $tax = ( ( ! edd_prices_show_tax_on_checkout() && edd_prices_include_tax() ) || ( ! edd_prices_include_tax() && edd_prices_show_tax_on_checkout() ) );
299 $price = esc_html( edd_currency_filter( edd_format_amount( edd_get_cart_subtotal() ) ) );
300
301 if ( edd_is_cart_taxed() ) {
302 if ( ! edd_prices_show_tax_on_checkout() && edd_prices_include_tax() ) {
303 $price .= '<br/><span style="font-weight:normal;text-transform:none;">' . __( '(ex. tax)', 'edd' ) . '</span>';
304 }
305
306 if ( edd_prices_show_tax_on_checkout() && $edd_options['prices_include_tax'] == 'no' ) {
307 $price .= '<br/><span style="font-weight:normal;text-transform:none;">' . __( '(incl. tax)', 'edd' ) . '</span>';
308 }
309 }
310
311 return $price;
312 }
313
314 315 316 317 318 319 320 321 322 323 324
325 function edd_get_cart_subtotal( $tax = true ) {
326 global $edd_options;
327
328 $cart_items = edd_get_cart_contents();
329 $amount = 0;
330
331 if ( $cart_items ) {
332 foreach ( $cart_items as $item ) {
333 $amount += edd_get_cart_item_price( $item['id'], $item['options'], $tax );
334
335 }
336 }
337
338 return apply_filters( 'edd_get_cart_subtotal', $amount );
339 }
340
341 342 343 344 345 346 347 348 349
350 function edd_cart_has_fees() {
351 return EDD()->fees->has_fees();
352 }
353
354 355 356 357 358 359 360 361 362
363 function edd_get_cart_fees() {
364 return EDD()->fees->get_fees();
365 }
366
367 368 369 370 371 372 373 374 375
376 function edd_get_cart_fee_total() {
377 return EDD()->fees->total();
378 }
379
380 381 382 383 384 385 386 387
388 function edd_get_cart_amount( $add_taxes = true, $local_override = false ) {
389 $amount = edd_get_cart_subtotal( false );
390
391 if ( ! empty( $_POST['edd-discount'] ) || edd_get_cart_discounts() !== false ) {
392
393 $discounts = edd_get_cart_discounts();
394
395
396 $posted_discount = isset( $_POST['edd-discount'] ) ? trim( $_POST['edd-discount'] ) : '';
397
398 if ( $posted_discount && ! in_array( $posted_discount, $discounts ) ) {
399
400 $amount = edd_get_discounted_amount( $posted_discount, $amount );
401 }
402
403 if( ! empty( $discounts ) ) {
404
405 $amount -= edd_get_cart_discounted_amount();
406 }
407 }
408
409 if ( edd_use_taxes() && $add_taxes ) {
410 if ( edd_local_taxes_only() && ( isset( $_POST['edd_tax_opt_in'] ) || $local_override ) ) {
411
412 $tax = edd_get_cart_tax();
413 $amount += $tax;
414 } elseif ( ! edd_local_taxes_only() ) {
415
416 $tax = edd_get_cart_tax();
417 $amount += $tax;
418 }
419 }
420
421 return apply_filters( 'edd_get_cart_amount', $amount, $add_taxes, $local_override );
422 }
423
424 425 426 427 428 429 430 431 432 433
434 function edd_get_cart_total( $discounts = false ) {
435 global $edd_options;
436
437 $subtotal = edd_get_cart_subtotal( edd_prices_include_tax() );
438 $fees = edd_get_cart_fee_total();
439 $cart_tax = edd_is_cart_taxed() ? edd_get_cart_tax( $discounts ) : 0;
440 $discount = edd_get_cart_discounted_amount( $discounts );
441
442 $total = $subtotal + $fees + $cart_tax - $discount;
443
444 return (float) apply_filters( 'edd_get_cart_total', $total );
445 }
446
447 448 449 450 451 452 453 454 455 456 457
458 function edd_cart_total( $echo = true ) {
459 global $edd_options;
460
461 $total = apply_filters( 'edd_cart_total', edd_currency_filter( edd_format_amount( edd_get_cart_total() ) ) );
462
463 if ( edd_is_cart_taxed() ) {
464 if ( edd_prices_show_tax_on_checkout() ) {
465 $total .= '<br/><span>'. sprintf( __('(includes %s tax)', 'edd'), edd_cart_tax() ) . '</span>';
466 }
467 }
468
469 if ( ! $echo ) {
470 return $total;
471 }
472
473 echo $total;
474 }
475
476 477 478 479 480 481 482 483 484
485 function edd_get_purchase_summary( $purchase_data, $email = true ) {
486 $summary = '';
487
488 if ( $email ) {
489 $summary .= $purchase_data['user_email'] . ' - ';
490 }
491
492 foreach ( $purchase_data['downloads'] as $download ) {
493 $summary .= get_the_title( $download['id'] ) . ', ';
494 }
495
496 $summary = substr( $summary, 0, -2 );
497
498 return $summary;
499 }
500
501 502 503 504 505 506 507
508 function edd_get_cart_tax( $discounts = false ) {
509 $subtotal = edd_get_cart_subtotal( false );
510 $subtotal += edd_get_cart_fee_total();
511 $cart_tax = 0;
512
513 if ( edd_is_cart_taxed() ) {
514
515 if ( edd_taxes_after_discounts() ) {
516 $subtotal -= edd_get_cart_discounted_amount( $discounts );
517 }
518
519 $cart_tax = edd_calculate_tax( $subtotal, false );
520
521 }
522
523 return apply_filters( 'edd_get_cart_tax', $cart_tax, $subtotal );
524 }
525
526 527 528 529 530 531 532
533 function edd_cart_tax( $echo = false ) {
534 $cart_tax = 0;
535
536 if ( edd_is_cart_taxed() ) {
537 $cart_tax = edd_get_cart_tax();
538 $cart_tax = edd_currency_filter( edd_format_amount( $cart_tax ) );
539 }
540
541 $tax = apply_filters( 'edd_cart_tax', $cart_tax );
542
543 if ( ! $echo ) {
544 return $tax;
545 }
546
547 echo $tax;
548 }
549
550 551 552 553 554 555
556 function edd_get_cart_content_details() {
557 $cart_items = edd_get_cart_contents();
558 if ( empty( $cart_items ) ) return false;
559
560 $details = array();
561 $is_taxed = edd_is_cart_taxed();
562
563 foreach( $cart_items as $key => $item ) {
564
565 $price = edd_get_cart_item_price( $item['id'], $item['options'] );
566 $non_taxed_price = edd_get_cart_item_price( $item['id'], $item['options'], false );
567
568 $details[ $key ] = array(
569 'name' => get_the_title( $item['id'] ),
570 'id' => $item['id'],
571 'item_number' => $item,
572 'price' => $price,
573 'quantity' => 1,
574 'tax' => $is_taxed ? edd_calculate_tax( $non_taxed_price, false ) : 0,
575 );
576 }
577
578 return $details;
579 }
580
581 582 583 584 585 586 587 588 589 590
591 function edd_add_collection_to_cart( $taxonomy, $terms ) {
592 if ( ! is_string( $taxonomy ) ) return false;
593
594 $field = is_int( $terms ) ? 'id' : 'slug';
595
596 $cart_item_ids = array();
597
598 $args = array(
599 'post_type' => 'download',
600 'posts_per_page' => -1,
601 $taxonomy => $terms
602 );
603
604 $items = get_posts( $args );
605 if ( $items ) {
606 foreach ( $items as $item ) {
607 edd_add_to_cart( $item->ID );
608 $cart_item_ids[] = $item->ID;
609 }
610 }
611 return $cart_item_ids;
612 }
613
614 615 616 617 618 619 620 621 622 623
624 function edd_remove_item_url( $cart_key, $post, $ajax = false ) {
625 global $post;
626
627 if( is_page() ) {
628 $current_page = add_query_arg( 'page_id', $post->ID, home_url('/') );
629 } else if( is_singular() ) {
630 $current_page = add_query_arg( 'p', $post->ID, home_url('/') );
631 } else {
632 $current_page = edd_get_current_page_url();
633 }
634 $remove_url = add_query_arg( array('cart_item' => $cart_key, 'edd_action' => 'remove' ), $current_page );
635
636 return apply_filters( 'edd_remove_item_url', $remove_url );
637 }
638
639 640 641 642 643 644 645
646 function edd_show_added_to_cart_messages( $download_id ) {
647 if ( isset( $_POST['edd_action'] ) && $_POST['edd_action'] == 'add_to_cart' ) {
648 $alert = '<div class="edd_added_to_cart_alert">'
649 . sprintf( __('You have successfully added %s to your shopping cart.', 'edd'), get_the_title( $download_id ) )
650 . ' <a href="' . edd_get_checkout_uri() . '" class="edd_alert_checkout_link">' . __('Checkout.', 'edd') . '</a>'
651 . '</div>';
652
653 echo apply_filters( 'edd_show_added_to_cart_messages', $alert );
654 }
655 }
656 add_action('edd_after_download_content', 'edd_show_added_to_cart_messages');
657
658 659 660 661 662 663 664 665
666 function edd_get_checkout_uri( $args = array() ) {
667 global $edd_options;
668
669 $uri = isset( $edd_options['purchase_page'] ) ? get_permalink( $edd_options['purchase_page'] ) : NULL;
670
671 if ( ! empty( $args ) ) {
672
673 if ( is_string( $args ) )
674 $args = str_replace( '?', '', $args );
675
676 $args = wp_parse_args( $args );
677
678 $uri = add_query_arg( $args, $uri );
679 }
680
681 $scheme = defined( 'FORCE_SSL_ADMIN' ) && FORCE_SSL_ADMIN ? 'https' : 'admin';
682
683 $ajax_url = admin_url( 'admin-ajax.php', $scheme );
684
685 if ( ! preg_match( '/^https/', $uri ) && preg_match( '/^https/', $ajax_url ) ) {
686 $uri = preg_replace( '/^http/', 'https', $uri );
687 }
688
689 if ( isset( $edd_options['no_cache_checkout'] ) && edd_is_caching_plugin_active() )
690 $uri = add_query_arg( 'nocache', 'true', $uri );
691
692 return apply_filters( 'edd_get_checkout_uri', $uri );
693 }
694
695 696 697 698 699 700 701 702
703 function edd_get_failed_transaction_uri( $extras = false ) {
704 global $edd_options;
705
706 $uri = isset( $edd_options['failure_page'] ) ? trailingslashit( get_permalink( $edd_options['failure_page'] ) ) : home_url();
707 if ( $extras )
708 $uri .= $extras;
709
710 return apply_filters( 'edd_get_failed_transaction_uri', $uri );
711 }
712
713 714 715 716 717 718
719 function edd_is_checkout() {
720 global $edd_options;
721 $is_checkout = isset( $edd_options['purchase_page'] ) ? is_page( $edd_options['purchase_page'] ) : false;
722 return apply_filters( 'edd_is_checkout', $is_checkout );
723 }
724
725 726 727 728 729 730 731
732 function edd_empty_cart() {
733
734 EDD()->session->set('edd_cart', NULL );
735
736
737 edd_unset_all_cart_discounts();
738 }
739
740 741 742 743 744 745 746 747 748
749 function edd_set_purchase_session( $purchase_data ) {
750 EDD()->session->set('edd_purchase', $purchase_data );
751 }
752
753 754 755 756 757 758 759 760 761 762
763 function edd_get_purchase_session() {
764 return EDD()->session->get('edd_purchase');
765 }
766