1
|
|
|
<?php if (!defined('EVENT_ESPRESSO_VERSION')) exit('No direct script access allowed'); |
2
|
|
|
do_action( 'AHEE_log', __FILE__, __FUNCTION__, '' );/** |
3
|
|
|
* |
4
|
|
|
* Event Espresso |
5
|
|
|
* |
6
|
|
|
* Event Registration and Management Plugin for WordPress |
7
|
|
|
* |
8
|
|
|
* @ package Event Espresso |
9
|
|
|
* @ author Seth Shoultes |
10
|
|
|
* @ copyright (c) 2008-2011 Event Espresso All Rights Reserved. |
11
|
|
|
* @ license http://eventespresso.com/support/terms-conditions/ * see Plugin Licensing * |
12
|
|
|
* @ link http://www.eventespresso.com |
13
|
|
|
* |
14
|
|
|
* ------------------------------------------------------------------------ |
15
|
|
|
* |
16
|
|
|
* EE_Cart class |
17
|
|
|
* Used to keep track of which tickets the user has specified they want to purchase. |
18
|
|
|
* This data is used for generating the Transaction and Registrations, and the |
19
|
|
|
* Line Items on cart are themselves saved for creating a persistent snapshot of |
20
|
|
|
* what was purchased and for how much. |
21
|
|
|
* |
22
|
|
|
* |
23
|
|
|
* @ version 2.0 |
24
|
|
|
* @subpackage includes/core/EE_Cart.core.php |
25
|
|
|
* @ author Mike Nelson, Brent Christensen |
26
|
|
|
* |
27
|
|
|
* ------------------------------------------------------------------------ |
28
|
|
|
*/ |
29
|
|
|
class EE_Cart { |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* instance of the EE_Cart object |
33
|
|
|
* @access private |
34
|
|
|
* @var EE_Cart $_instance |
35
|
|
|
*/ |
36
|
|
|
private static $_instance = NULL; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* instance of the EE_Session object |
40
|
|
|
* @access protected |
41
|
|
|
* @var EE_Session $_session |
42
|
|
|
*/ |
43
|
|
|
protected $_session = NULL; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* The total Line item which comprises all the children line-item subtotals, |
47
|
|
|
* which in turn each have their line items. |
48
|
|
|
* Typically, the line item structure will look like: |
49
|
|
|
* grand total |
50
|
|
|
* -tickets-sub-total |
51
|
|
|
* --ticket1 |
52
|
|
|
* --ticket2 |
53
|
|
|
* --... |
54
|
|
|
* -taxes-sub-total |
55
|
|
|
* --tax1 |
56
|
|
|
* --tax2 |
57
|
|
|
* @var EE_Line_Item |
58
|
|
|
*/ |
59
|
|
|
private $_grand_total = NULL; |
60
|
|
|
|
61
|
|
|
|
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @singleton method used to instantiate class object |
65
|
|
|
* @access public |
66
|
|
|
* @param EE_Line_Item $grand_total |
67
|
|
|
* @param EE_Session $session |
68
|
|
|
* @return \EE_Cart |
69
|
|
|
*/ |
70
|
|
|
public static function instance( EE_Line_Item $grand_total = null, EE_Session $session = null ) { |
71
|
|
|
if ( ! empty( $grand_total ) ){ |
72
|
|
|
self::$_instance = new self( $grand_total, $session ); |
73
|
|
|
} |
74
|
|
|
// or maybe retrieve an existing one ? |
75
|
|
|
if ( ! self::$_instance instanceof EE_Cart ) { |
76
|
|
|
// try getting the cart out of the session |
77
|
|
|
$saved_cart = $session instanceof EE_Session ? $session->cart() : null; |
78
|
|
|
self::$_instance = $saved_cart instanceof EE_Cart ? $saved_cart : new self( $grand_total, $session ); |
79
|
|
|
unset( $saved_cart ); |
80
|
|
|
} |
81
|
|
|
// verify that cart is ok and grand total line item exists |
82
|
|
|
if ( ! self::$_instance instanceof EE_Cart || ! self::$_instance->_grand_total instanceof EE_Line_Item ) { |
83
|
|
|
self::$_instance = new self( $grand_total, $session ); |
84
|
|
|
} |
85
|
|
|
self::$_instance->get_grand_total(); |
86
|
|
|
// once everything is all said and done, save the cart to the EE_Session |
87
|
|
|
add_action( 'shutdown', array( self::$_instance, 'save_cart' ), 90 ); |
88
|
|
|
return self::$_instance; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
|
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* private constructor to prevent direct creation |
95
|
|
|
* @Constructor |
96
|
|
|
* @access private |
97
|
|
|
* @param EE_Line_Item $grand_total |
98
|
|
|
* @param EE_Session $session |
99
|
|
|
* @return \EE_Cart |
|
|
|
|
100
|
|
|
*/ |
101
|
|
|
private function __construct( EE_Line_Item $grand_total = null, EE_Session $session = null ) { |
102
|
|
|
do_action( 'AHEE_log', __FILE__, __FUNCTION__, '' ); |
103
|
|
|
$this->set_session( $session ); |
104
|
|
|
if ( $grand_total instanceof EE_Line_Item ) { |
105
|
|
|
$this->set_grand_total_line_item( $grand_total ); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
|
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Resets the cart completely (whereas empty_cart |
113
|
|
|
* @param EE_Line_Item $grand_total |
114
|
|
|
* @param EE_Session $session |
115
|
|
|
* @return EE_Cart |
116
|
|
|
*/ |
117
|
|
|
public static function reset( EE_Line_Item $grand_total = null, EE_Session $session = null ) { |
118
|
|
|
remove_action( 'shutdown', array( self::$_instance, 'save_cart' ), 90 ); |
119
|
|
|
if ( $session instanceof EE_Session ) { |
120
|
|
|
$session->reset_cart(); |
121
|
|
|
} |
122
|
|
|
self::$_instance = null; |
123
|
|
|
return self::instance( $grand_total, $session ); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
|
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param EE_Session $session |
130
|
|
|
*/ |
131
|
|
|
public function set_session( EE_Session $session = null ) { |
132
|
|
|
$this->_session = $session instanceof EE_Session ? $session : EE_Registry::instance()->load_core( 'Session' ); |
|
|
|
|
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
|
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Sets the cart to match the line item. Especially handy for loading an old cart where you |
139
|
|
|
* know the grand total line item on it |
140
|
|
|
* @param EE_Line_Item $line_item |
141
|
|
|
*/ |
142
|
|
|
public function set_grand_total_line_item( EE_Line_Item $line_item ) { |
143
|
|
|
$this->_grand_total = $line_item; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
|
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* get_cart_from_reg_url_link |
150
|
|
|
* @access public |
151
|
|
|
* @param EE_Transaction $transaction |
152
|
|
|
* @param EE_Session $session |
153
|
|
|
* @return \EE_Cart |
154
|
|
|
*/ |
155
|
|
|
public static function get_cart_from_txn( EE_Transaction $transaction, EE_Session $session = null ) { |
156
|
|
|
$grand_total = $transaction->total_line_item(); |
157
|
|
|
$grand_total->get_items(); |
158
|
|
|
$grand_total->tax_descendants(); |
159
|
|
|
return EE_Cart::instance( $grand_total, $session ); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
|
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Creates the total line item, and ensures it has its 'tickets' and 'taxes' sub-items |
166
|
|
|
* @return EE_Line_Item |
167
|
|
|
*/ |
168
|
|
|
private function _create_grand_total(){ |
169
|
|
|
$this->_grand_total = EEH_Line_Item::create_total_line_item(); |
170
|
|
|
return $this->_grand_total; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
|
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Gets all the line items of object type Ticket |
177
|
|
|
* @access public |
178
|
|
|
* @return \EE_Line_Item[] |
179
|
|
|
*/ |
180
|
|
|
public function get_tickets() { |
181
|
|
|
return EEH_Line_Item::get_ticket_line_items( $this->_grand_total ); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
|
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* returns the total quantity of tickets in the cart |
188
|
|
|
* |
189
|
|
|
* @access public |
190
|
|
|
* @return int |
191
|
|
|
*/ |
192
|
|
|
public function all_ticket_quantity_count() { |
193
|
|
|
$tickets = $this->get_tickets(); |
194
|
|
|
if ( empty( $tickets )) { |
195
|
|
|
return 0; |
196
|
|
|
} |
197
|
|
|
$count = 0; |
198
|
|
|
foreach ( $tickets as $ticket ) { |
199
|
|
|
$count = $count + $ticket->get('LIN_quantity'); |
200
|
|
|
} |
201
|
|
|
return $count; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
|
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Gets all the tax line items |
208
|
|
|
* @return \EE_Line_Item[] |
209
|
|
|
*/ |
210
|
|
|
public function get_taxes(){ |
211
|
|
|
return EEH_Line_Item::get_taxes_subtotal( $this->_grand_total )->children(); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
|
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Gets the total line item (which is a parent of all other line items) on this cart |
218
|
|
|
* @return EE_Line_Item |
219
|
|
|
*/ |
220
|
|
|
public function get_grand_total(){ |
221
|
|
|
return $this->_grand_total instanceof EE_Line_Item ? $this->_grand_total : $this->_create_grand_total(); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
|
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* @process items for adding to cart |
228
|
|
|
* @access public |
229
|
|
|
* @param EE_Ticket $ticket |
230
|
|
|
* @param int $qty |
231
|
|
|
* @return TRUE on success, FALSE on fail |
232
|
|
|
*/ |
233
|
|
|
public function add_ticket_to_cart( EE_Ticket $ticket, $qty = 1 ) { |
234
|
|
|
EEH_Line_Item::add_ticket_purchase( $this->get_grand_total(), $ticket, $qty ); |
235
|
|
|
return $this->save_cart() ? TRUE : FALSE; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
|
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* get_cart_total_before_tax |
242
|
|
|
* @access public |
243
|
|
|
* @return float |
244
|
|
|
*/ |
245
|
|
|
public function get_cart_total_before_tax() { |
246
|
|
|
return $this->get_grand_total()->recalculate_pre_tax_total(); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
|
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* gets the total amount of tax paid for items in this cart |
253
|
|
|
* @access public |
254
|
|
|
* @return float |
255
|
|
|
*/ |
256
|
|
|
public function get_applied_taxes() { |
257
|
|
|
return EEH_Line_Item::ensure_taxes_applied( $this->_grand_total ); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
|
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* Gets the total amount to be paid for the items in the cart, including taxes and other modifiers |
264
|
|
|
* @access public |
265
|
|
|
* @return float |
266
|
|
|
*/ |
267
|
|
|
public function get_cart_grand_total() { |
268
|
|
|
EEH_Line_Item::ensure_taxes_applied( $this->_grand_total ); |
269
|
|
|
return $this->get_grand_total()->total(); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
|
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* Gets the total amount to be paid for the items in the cart, including taxes and other modifiers |
276
|
|
|
* @access public |
277
|
|
|
* @return float |
278
|
|
|
*/ |
279
|
|
|
public function recalculate_all_cart_totals() { |
280
|
|
|
$pre_tax_total = $this->get_cart_total_before_tax(); |
281
|
|
|
$taxes_total = EEH_Line_Item::ensure_taxes_applied( $this->_grand_total ); |
282
|
|
|
$this->_grand_total->set_total( $pre_tax_total + $taxes_total ); |
283
|
|
|
$this->_grand_total->save_this_and_descendants_to_txn(); |
284
|
|
|
return $this->get_grand_total()->total(); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
|
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* deletes an item from the cart |
291
|
|
|
* @access public |
292
|
|
|
* @param array|bool|string $line_item_codes |
293
|
|
|
* @return int on success, FALSE on fail |
294
|
|
|
*/ |
295
|
|
|
public function delete_items( $line_item_codes = FALSE ) { |
296
|
|
|
do_action( 'AHEE_log', __FILE__, __FUNCTION__, '' ); |
297
|
|
|
return EEH_Line_Item::delete_items($this->get_grand_total(), $line_item_codes ); |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
|
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* @remove ALL items from cart and zero ALL totals |
304
|
|
|
* @access public |
305
|
|
|
* @return bool |
306
|
|
|
*/ |
307
|
|
|
public function empty_cart() { |
308
|
|
|
do_action( 'AHEE_log', __FILE__, __FUNCTION__, '' ); |
309
|
|
|
$this->_grand_total = $this->_create_grand_total(); |
310
|
|
|
return $this->save_cart( TRUE ); |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
|
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* @remove ALL items from cart and delete total as well |
317
|
|
|
* @access public |
318
|
|
|
* @return bool |
319
|
|
|
*/ |
320
|
|
|
public function delete_cart() { |
321
|
|
|
$deleted = EEH_Line_Item::delete_all_child_items( $this->_grand_total ); |
322
|
|
|
if ( $deleted ) { |
323
|
|
|
$deleted += $this->_grand_total->delete(); |
324
|
|
|
$this->_grand_total = null; |
325
|
|
|
} |
326
|
|
|
return $deleted; |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
|
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* @save cart to session |
333
|
|
|
* @access public |
334
|
|
|
* @param bool $apply_taxes |
335
|
|
|
* @return TRUE on success, FALSE on fail |
336
|
|
|
*/ |
337
|
|
|
public function save_cart( $apply_taxes = TRUE ) { |
338
|
|
|
if ( $apply_taxes && $this->_grand_total instanceof EE_Line_Item ) { |
339
|
|
|
EEH_Line_Item::ensure_taxes_applied( $this->_grand_total ); |
340
|
|
|
//make sure we don't cache the transaction because it can get stale |
341
|
|
|
if( $this->_grand_total->get_one_from_cache( 'Transaction' ) instanceof EE_Transaction && |
342
|
|
|
$this->_grand_total->get_one_from_cache( 'Transaction' )->ID()) { |
343
|
|
|
$this->_grand_total->clear_cache( 'Transaction', null, true ); |
344
|
|
|
} |
345
|
|
|
} |
346
|
|
|
if ( $this->_session instanceof EE_Session ) { |
347
|
|
|
return $this->_session->set_cart( $this ); |
348
|
|
|
} else { |
349
|
|
|
return false; |
350
|
|
|
} |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
|
354
|
|
|
|
355
|
|
|
|
356
|
|
|
|
357
|
|
|
} |
358
|
|
|
/* End of file EE_Cart.core.php */ |
359
|
|
|
/* Location: /includes/core/EE_Cart.core.php */ |
360
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.