· 4 years ago · Mar 23, 2021, 02:58 PM
1<?php
2/**
3 * CodeIgniter
4 *
5 * An open source application development framework for PHP
6 *
7 * This content is released under the MIT License (MIT)
8 *
9 * Copyright (c) 2014 - 2018, British Columbia Institute of Technology
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 * THE SOFTWARE.
28 *
29 * @package CodeIgniter
30 * @author EllisLab Dev Team
31 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
32 * @copyright Copyright (c) 2014 - 2018, British Columbia Institute of Technology (http://bcit.ca/)
33 * @license http://opensource.org/licenses/MIT MIT License
34 * @link https://codeigniter.com
35 * @since Version 1.0.0
36 * @filesource
37 */
38defined('BASEPATH') OR exit('No direct script access allowed');
39
40/**
41 * Shopping cart2 Class
42 *
43 * @package CodeIgniter
44 * @subpackage Libraries
45 * @category Shopping cart2
46 * @author EllisLab Dev Team
47 * @link https://codeigniter.com/user_guide/libraries/cart2.html
48 * @deprecated 3.0.0 This class is too specific for CI.
49 */
50class CI_cart2 {
51
52 /**
53 * These are the regular expression rules that we use to validate the product ID and product name
54 * alpha-numeric, dashes, underscores, or periods
55 *
56 * @var string
57 */
58 public $product_id_rules = '\.a-z0-9_-';
59
60 /**
61 * These are the regular expression rules that we use to validate the product ID and product name
62 * alpha-numeric, dashes, underscores, colons or periods
63 *
64 * @var string
65 */
66 public $product_name_rules = '\w \-\.\:';
67
68 /**
69 * only allow safe product names
70 *
71 * @var bool
72 */
73 public $product_name_safe = TRUE;
74
75 // --------------------------------------------------------------------------
76
77 /**
78 * Reference to CodeIgniter instance
79 *
80 * @var object
81 */
82 protected $CI;
83
84 /**
85 * Contents of the cart2
86 *
87 * @var array
88 */
89 protected $_cart2_contents = array();
90
91 /**
92 * Shopping Class Constructor
93 *
94 * The constructor loads the Session class, used to store the shopping cart2 contents.
95 *
96 * @param array
97 * @return void
98 */
99 public function __construct($params = array())
100 {
101 // Set the super object to a local variable for use later
102 $this->CI =& get_instance();
103
104 // Are any config settings being passed manually? If so, set them
105 $config = is_array($params) ? $params : array();
106
107 // Load the Sessions class
108 $this->CI->load->driver('session', $config);
109
110 // Grab the shopping cart2 array from the session table
111 $this->_cart2_contents = $this->CI->session->userdata('cart2_contents');
112 if ($this->_cart2_contents === NULL)
113 {
114 // No cart2 exists so we'll set some base values
115 $this->_cart2_contents = array('cart2_total' => 0, 'total_items' => 0);
116 }
117
118 log_message('info', 'cart2 Class Initialized');
119 }
120
121 // --------------------------------------------------------------------
122
123 /**
124 * Insert items into the cart2 and save it to the session table
125 *
126 * @param array
127 * @return bool
128 */
129 public function insert($items = array())
130 {
131 // Was any cart2 data passed? No? Bah...
132 if ( ! is_array($items) OR count($items) === 0)
133 {
134 log_message('error', 'The insert method must be passed an array containing data.');
135 return FALSE;
136 }
137
138 // You can either insert a single product using a one-dimensional array,
139 // or multiple products using a multi-dimensional one. The way we
140 // determine the array type is by looking for a required array key named "id"
141 // at the top level. If it's not found, we will assume it's a multi-dimensional array.
142
143 $save_cart2 = FALSE;
144 if (isset($items['id']))
145 {
146 if (($rowid = $this->_insert($items)))
147 {
148 $save_cart2 = TRUE;
149 }
150 }
151 else
152 {
153 foreach ($items as $val)
154 {
155 if (is_array($val) && isset($val['id']))
156 {
157 if ($this->_insert($val))
158 {
159 $save_cart2 = TRUE;
160 }
161 }
162 }
163 }
164
165 // Save the cart2 data if the insert was successful
166 if ($save_cart2 === TRUE)
167 {
168 $this->_save_cart2();
169 return isset($rowid) ? $rowid : TRUE;
170 }
171
172 return FALSE;
173 }
174
175 // --------------------------------------------------------------------
176
177 /**
178 * Insert
179 *
180 * @param array
181 * @return bool
182 */
183 protected function _insert($items = array())
184 {
185 // Was any cart2 data passed? No? Bah...
186 if ( ! is_array($items) OR count($items) === 0)
187 {
188 log_message('error', 'The insert method must be passed an array containing data.');
189 return FALSE;
190 }
191
192 // --------------------------------------------------------------------
193
194 // Does the $items array contain an id, quantity, price, and name? These are required
195 if ( ! isset($items['id'], $items['qty'], $items['price'], $items['name'], $items['status']))
196 {
197 log_message('error', 'The cart2 array must contain a product ID, quantity, price, and name.');
198 return FALSE;
199 }
200
201 // --------------------------------------------------------------------
202
203 // Prep the quantity. It can only be a number. Duh... also trim any leading zeros
204 $items['qty'] = (float) $items['qty'];
205
206 // If the quantity is zero or blank there's nothing for us to do
207 if ($items['qty'] == 0)
208 {
209 return FALSE;
210 }
211
212 // --------------------------------------------------------------------
213
214 // Validate the product ID. It can only be alpha-numeric, dashes, underscores or periods
215 // Not totally sure we should impose this rule, but it seems prudent to standardize IDs.
216 // Note: These can be user-specified by setting the $this->product_id_rules variable.
217 if ( ! preg_match('/^['.$this->product_id_rules.']+$/i', $items['id']))
218 {
219 log_message('error', 'Invalid product ID. The product ID can only contain alpha-numeric characters, dashes, and underscores');
220 return FALSE;
221 }
222
223 // --------------------------------------------------------------------
224
225 // Validate the product name. It can only be alpha-numeric, dashes, underscores, colons or periods.
226 // Note: These can be user-specified by setting the $this->product_name_rules variable.
227 if ($this->product_name_safe && ! preg_match('/^['.$this->product_name_rules.']+$/i'.(UTF8_ENABLED ? 'u' : ''), $items['name']))
228 {
229 log_message('error', 'An invalid name was submitted as the product name: '.$items['name'].' The name can only contain alpha-numeric characters, dashes, underscores, colons, and spaces');
230 return FALSE;
231 }
232
233 // --------------------------------------------------------------------
234
235 // Prep the price. Remove leading zeros and anything that isn't a number or decimal point.
236 $items['price'] = (float) $items['price'];
237 $items['status'] = (float) $items['status'];
238
239 // We now need to create a unique identifier for the item being inserted into the cart2.
240 // Every time something is added to the cart2 it is stored in the master cart2 array.
241 // Each row in the cart2 array, however, must have a unique index that identifies not only
242 // a particular product, but makes it possible to store identical products with different options.
243 // For example, what if someone buys two identical t-shirts (same product ID), but in
244 // different sizes? The product ID (and other attributes, like the name) will be identical for
245 // both sizes because it's the same shirt. The only difference will be the size.
246 // Internally, we need to treat identical submissions, but with different options, as a unique product.
247 // Our solution is to convert the options array to a string and MD5 it along with the product ID.
248 // This becomes the unique "row ID"
249 if (isset($items['options']) && count($items['options']) > 0)
250 {
251 $rowid = md5($items['id'].serialize($items['options']));
252 }
253 else
254 {
255 // No options were submitted so we simply MD5 the product ID.
256 // Technically, we don't need to MD5 the ID in this case, but it makes
257 // sense to standardize the format of array indexes for both conditions
258 $rowid = md5($items['id']);
259 }
260
261 // --------------------------------------------------------------------
262
263 // Now that we have our unique "row ID", we'll add our cart2 items to the master array
264 // grab quantity if it's already there and add it on
265 $old_quantity = isset($this->_cart2_contents[$rowid]['qty']) ? (int) $this->_cart2_contents[$rowid]['qty'] : 0;
266
267 // Re-create the entry, just to make sure our index contains only the data from this submission
268 $items['rowid'] = $rowid;
269 $items['qty'] += $old_quantity;
270 $this->_cart2_contents[$rowid] = $items;
271
272 return $rowid;
273 }
274
275 public function insert_pembelian($items = array())
276 {
277 // Was any cart2 data passed? No? Bah...
278 if ( ! is_array($items) OR count($items) === 0)
279 {
280 log_message('error', 'The insert method must be passed an array containing data.');
281 return FALSE;
282 }
283
284 // You can either insert a single product using a one-dimensional array,
285 // or multiple products using a multi-dimensional one. The way we
286 // determine the array type is by looking for a required array key named "id"
287 // at the top level. If it's not found, we will assume it's a multi-dimensional array.
288
289 $save_cart2_pembelian = FALSE;
290 if (isset($items['id']))
291 {
292 if (($rowid = $this->_insert_pembelian($items)))
293 {
294 $save_cart2_pembelian = TRUE;
295 }
296 }
297 else
298 {
299 foreach ($items as $val)
300 {
301 if (is_array($val) && isset($val['id']))
302 {
303 if ($this->_insert_pembelian($val))
304 {
305 $save_cart2_pembelian = TRUE;
306 }
307 }
308 }
309 }
310
311 // Save the cart2 data if the insert was successful
312 if ($save_cart2_pembelian === TRUE)
313 {
314 $this->_save_cart2_pembelian();
315 return isset($rowid) ? $rowid : TRUE;
316 }
317
318 return FALSE;
319 }
320
321 // --------------------------------------------------------------------
322
323 /**
324 * Insert
325 *
326 * @param array
327 * @return bool
328 */
329 protected function _insert_pembelian($items = array())
330 {
331 // Was any cart2 data passed? No? Bah...
332 if ( ! is_array($items) OR count($items) === 0)
333 {
334 log_message('error', 'The insert method must be passed an array containing data.');
335 return FALSE;
336 }
337
338 // --------------------------------------------------------------------
339
340 // Does the $items array contain an id, quantity, price, and name? These are required
341 if ( ! isset($items['id'], $items['qty'], $items['price'], $items['name'], $items['status']))
342 {
343 log_message('error', 'The cart2 array must contain a product ID, quantity, price, and name.');
344 return FALSE;
345 }
346
347 // --------------------------------------------------------------------
348
349 // Prep the quantity. It can only be a number. Duh... also trim any leading zeros
350 $items['qty'] = (float) $items['qty'];
351
352 // If the quantity is zero or blank there's nothing for us to do
353 if ($items['qty'] == 0)
354 {
355 return FALSE;
356 }
357
358 // --------------------------------------------------------------------
359
360 // Validate the product ID. It can only be alpha-numeric, dashes, underscores or periods
361 // Not totally sure we should impose this rule, but it seems prudent to standardize IDs.
362 // Note: These can be user-specified by setting the $this->product_id_rules variable.
363 if ( ! preg_match('/^['.$this->product_id_rules.']+$/i', $items['id']))
364 {
365 log_message('error', 'Invalid product ID. The product ID can only contain alpha-numeric characters, dashes, and underscores');
366 return FALSE;
367 }
368
369 // --------------------------------------------------------------------
370
371 // Validate the product name. It can only be alpha-numeric, dashes, underscores, colons or periods.
372 // Note: These can be user-specified by setting the $this->product_name_rules variable.
373 if ($this->product_name_safe && ! preg_match('/^['.$this->product_name_rules.']+$/i'.(UTF8_ENABLED ? 'u' : ''), $items['name']))
374 {
375 log_message('error', 'An invalid name was submitted as the product name: '.$items['name'].' The name can only contain alpha-numeric characters, dashes, underscores, colons, and spaces');
376 return FALSE;
377 }
378
379 // --------------------------------------------------------------------
380
381 // Prep the price. Remove leading zeros and anything that isn't a number or decimal point.
382 $items['price'] = (float) $items['price'];
383 $items['status'] = (float) $items['status'];
384
385 // We now need to create a unique identifier for the item being inserted into the cart2.
386 // Every time something is added to the cart2 it is stored in the master cart2 array.
387 // Each row in the cart2 array, however, must have a unique index that identifies not only
388 // a particular product, but makes it possible to store identical products with different options.
389 // For example, what if someone buys two identical t-shirts (same product ID), but in
390 // different sizes? The product ID (and other attributes, like the name) will be identical for
391 // both sizes because it's the same shirt. The only difference will be the size.
392 // Internally, we need to treat identical submissions, but with different options, as a unique product.
393 // Our solution is to convert the options array to a string and MD5 it along with the product ID.
394 // This becomes the unique "row ID"
395 if (isset($items['options']) && count($items['options']) > 0)
396 {
397 $rowid = md5($items['id'].serialize($items['options']));
398 }
399 else
400 {
401 // No options were submitted so we simply MD5 the product ID.
402 // Technically, we don't need to MD5 the ID in this case, but it makes
403 // sense to standardize the format of array indexes for both conditions
404 $rowid = md5($items['id']);
405 }
406
407 // --------------------------------------------------------------------
408
409 // Now that we have our unique "row ID", we'll add our cart2 items to the master array
410 // grab quantity if it's already there and add it on
411 $old_quantity = isset($this->_cart2_contents[$rowid]['qty']) ? (int) $this->_cart2_contents[$rowid]['qty'] : 0;
412
413 // Re-create the entry, just to make sure our index contains only the data from this submission
414 $items['rowid'] = $rowid;
415 $items['qty'] += $old_quantity;
416 $this->_cart2_contents[$rowid] = $items;
417
418 return $rowid;
419 }
420
421 // --------------------------------------------------------------------
422
423 /**
424 * Update the cart2
425 *
426 * This function permits the quantity of a given item to be changed.
427 * Typically it is called from the "view cart2" page if a user makes
428 * changes to the quantity before checkout. That array must contain the
429 * product ID and quantity for each item.
430 *
431 * @param array
432 * @return bool
433 */
434 public function update($items = array())
435 {
436 // Was any cart2 data passed?
437 if ( ! is_array($items) OR count($items) === 0)
438 {
439 return FALSE;
440 }
441
442 // You can either update a single product using a one-dimensional array,
443 // or multiple products using a multi-dimensional one. The way we
444 // determine the array type is by looking for a required array key named "rowid".
445 // If it's not found we assume it's a multi-dimensional array
446 $save_cart2 = FALSE;
447 if (isset($items['rowid']))
448 {
449 if ($this->_update($items) === TRUE)
450 {
451 $save_cart2 = TRUE;
452 }
453 }
454 else
455 {
456 foreach ($items as $val)
457 {
458 if (is_array($val) && isset($val['rowid']))
459 {
460 if ($this->_update($val) === TRUE)
461 {
462 $save_cart2 = TRUE;
463 }
464 }
465 }
466 }
467
468 // Save the cart2 data if the insert was successful
469 if ($save_cart2 === TRUE)
470 {
471 $this->_save_cart2();
472 return TRUE;
473 }
474
475 return FALSE;
476 }
477
478 // --------------------------------------------------------------------
479
480 /**
481 * Update the cart2
482 *
483 * This function permits changing item properties.
484 * Typically it is called from the "view cart2" page if a user makes
485 * changes to the quantity before checkout. That array must contain the
486 * rowid and quantity for each item.
487 *
488 * @param array
489 * @return bool
490 */
491 protected function _update($items = array())
492 {
493 // Without these array indexes there is nothing we can do
494 if ( ! isset($items['rowid'], $this->_cart2_contents[$items['rowid']]))
495 {
496 return FALSE;
497 }
498
499 // Prep the quantity
500 if (isset($items['qty']))
501 {
502 $items['qty'] = (float) $items['qty'];
503 // Is the quantity zero? If so we will remove the item from the cart2.
504 // If the quantity is greater than zero we are updating
505 if ($items['qty'] == 0)
506 {
507 unset($this->_cart2_contents[$items['rowid']]);
508 return TRUE;
509 }
510 }
511
512 // find updatable keys
513 $keys = array_intersect(array_keys($this->_cart2_contents[$items['rowid']]), array_keys($items));
514 // if a price was passed, make sure it contains valid data
515 if (isset($items['price']))
516 {
517 $items['price'] = (float) $items['price'];
518 }
519
520 // product id & name shouldn't be changed
521 foreach (array_diff($keys, array('id', 'name')) as $key)
522 {
523 $this->_cart2_contents[$items['rowid']][$key] = $items[$key];
524 }
525
526 return TRUE;
527 }
528
529
530 public function update_pembelian($items = array())
531 {
532 // Was any cart2 data passed?
533 if ( ! is_array($items) OR count($items) === 0)
534 {
535 return FALSE;
536 }
537
538 // You can either update a single product using a one-dimensional array,
539 // or multiple products using a multi-dimensional one. The way we
540 // determine the array type is by looking for a required array key named "rowid".
541 // If it's not found we assume it's a multi-dimensional array
542 $save_cart2_pembelian = FALSE;
543 if (isset($items['rowid']))
544 {
545 if ($this->_update_pembelian($items) === TRUE)
546 {
547 $save_cart2_pembelian = TRUE;
548 }
549 }
550 else
551 {
552 foreach ($items as $val)
553 {
554 if (is_array($val) && isset($val['rowid']))
555 {
556 if ($this->_update_pembelian($val) === TRUE)
557 {
558 $save_cart2_pembelian = TRUE;
559 }
560 }
561 }
562 }
563
564 // Save the cart2 data if the insert was successful
565 if ($save_cart2_pembelian === TRUE)
566 {
567 $this->_save_cart2_pembelian();
568 return TRUE;
569 }
570
571 return FALSE;
572 }
573
574 // --------------------------------------------------------------------
575
576 /**
577 * Update the cart2
578 *
579 * This function permits changing item properties.
580 * Typically it is called from the "view cart2" page if a user makes
581 * changes to the quantity before checkout. That array must contain the
582 * rowid and quantity for each item.
583 *
584 * @param array
585 * @return bool
586 */
587 protected function _update_pembelian($items = array())
588 {
589 // Without these array indexes there is nothing we can do
590 if ( ! isset($items['rowid'], $this->_cart2_contents[$items['rowid']]))
591 {
592 return FALSE;
593 }
594
595 // Prep the quantity
596 if (isset($items['qty']))
597 {
598 $items['qty'] = (float) $items['qty'];
599 // Is the quantity zero? If so we will remove the item from the cart2.
600 // If the quantity is greater than zero we are updating
601 if ($items['qty'] == 0)
602 {
603 unset($this->_cart2_contents[$items['rowid']]);
604 return TRUE;
605 }
606 }
607
608 // find updatable keys
609 $keys = array_intersect(array_keys($this->_cart2_contents[$items['rowid']]), array_keys($items));
610 // if a price was passed, make sure it contains valid data
611 if (isset($items['price']))
612 {
613 $items['price'] = (float) $items['price'];
614 }
615
616 // product id & name shouldn't be changed
617 foreach (array_diff($keys, array('id', 'name')) as $key)
618 {
619 $this->_cart2_contents[$items['rowid']][$key] = $items[$key];
620 }
621
622 return TRUE;
623 }
624
625 // --------------------------------------------------------------------
626
627 /**
628 * Save the cart2 array to the session DB
629 *
630 * @return bool
631 */
632 protected function _save_cart2()
633 {
634 // Let's add up the individual prices and set the cart2 sub-total
635 $this->_cart2_contents['total_items'] = $this->_cart2_contents['cart2_total'] = 0;
636 foreach ($this->_cart2_contents as $key => $val)
637 {
638 // We make sure the array contains the proper indexes
639 if ( ! is_array($val) OR ! isset($val['price'], $val['qty']))
640 {
641 continue;
642 }
643
644 $this->_cart2_contents['cart2_total'] += ($val['price'] * $val['qty']);
645 $this->_cart2_contents['total_items'] += $val['qty'];
646 $this->_cart2_contents[$key]['subtotal'] = ($this->_cart2_contents[$key]['price'] * $this->_cart2_contents[$key]['qty']);
647 }
648
649 // Is our cart2 empty? If so we delete it from the session
650 if (count($this->_cart2_contents) <= 2)
651 {
652 $this->CI->session->unset_userdata('cart2_contents');
653
654 // Nothing more to do... coffee time!
655 return FALSE;
656 }
657
658 // If we made it this far it means that our cart2 has data.
659 // Let's pass it to the Session class so it can be stored
660 $this->CI->session->set_userdata(array('cart2_contents' => $this->_cart2_contents));
661
662 // Woot!
663 return TRUE;
664 }
665
666
667 protected function _save_cart2_pembelian()
668 {
669 // Let's add up the individual prices and set the cart2 sub-total
670 $this->_cart2_contents['total_items'] = $this->_cart2_contents['cart2_total'] = 0;
671 foreach ($this->_cart2_contents as $key => $val)
672 {
673 // We make sure the array contains the proper indexes
674 if ( ! is_array($val) OR ! isset($val['price'], $val['qty']))
675 {
676 continue;
677 }
678
679 $this->_cart2_contents['cart2_total'] += ($val['price'] * $val['qty']);
680 $this->_cart2_contents['total_items'] += $val['qty'];
681 $this->_cart2_contents[$key]['subtotal'] = ($this->_cart2_contents[$key]['price'] * $this->_cart2_contents[$key]['qty']);
682 }
683
684 // Is our cart2 empty? If so we delete it from the session
685 if (count($this->_cart2_contents) <= 2)
686 {
687 $this->CI->session->unset_userdata('cart2_contents');
688
689 // Nothing more to do... coffee time!
690 return FALSE;
691 }
692
693 // If we made it this far it means that our cart2 has data.
694 // Let's pass it to the Session class so it can be stored
695 $this->CI->session->set_userdata(array('cart2_contents' => $this->_cart2_contents));
696
697 // Woot!
698 return TRUE;
699 }
700 // --------------------------------------------------------------------
701
702 /**
703 * cart2 Total
704 *
705 * @return int
706 */
707 public function total()
708 {
709 return $this->_cart2_contents['cart2_total'];
710 }
711
712 // --------------------------------------------------------------------
713
714 /**
715 * Remove Item
716 *
717 * Removes an item from the cart2
718 *
719 * @param int
720 * @return bool
721 */
722 public function remove($rowid)
723 {
724 // unset & save
725 unset($this->_cart2_contents[$rowid]);
726 $this->_save_cart2();
727 return TRUE;
728 }
729
730 // --------------------------------------------------------------------
731
732 /**
733 * Total Items
734 *
735 * Returns the total item count
736 *
737 * @return int
738 */
739 public function total_items()
740 {
741 return $this->_cart2_contents['total_items'];
742 }
743
744 // --------------------------------------------------------------------
745
746 /**
747 * cart2 Contents
748 *
749 * Returns the entire cart2 array
750 *
751 * @param bool
752 * @return array
753 */
754 public function contents($newest_first = FALSE)
755 {
756 // do we want the newest first?
757 $cart2 = ($newest_first) ? array_reverse($this->_cart2_contents) : $this->_cart2_contents;
758
759 // Remove these so they don't create a problem when showing the cart2 table
760 unset($cart2['total_items']);
761 unset($cart2['cart2_total']);
762
763 return $cart2;
764 }
765
766 // --------------------------------------------------------------------
767
768 /**
769 * Get cart2 item
770 *
771 * Returns the details of a specific item in the cart2
772 *
773 * @param string $row_id
774 * @return array
775 */
776 public function get_item($row_id)
777 {
778 return (in_array($row_id, array('total_items', 'cart2_total'), TRUE) OR ! isset($this->_cart2_contents[$row_id]))
779 ? FALSE
780 : $this->_cart2_contents[$row_id];
781 }
782
783 // --------------------------------------------------------------------
784
785 /**
786 * Has options
787 *
788 * Returns TRUE if the rowid passed to this function correlates to an item
789 * that has options associated with it.
790 *
791 * @param string $row_id = ''
792 * @return bool
793 */
794 public function has_options($row_id = '')
795 {
796 return (isset($this->_cart2_contents[$row_id]['options']) && count($this->_cart2_contents[$row_id]['options']) !== 0);
797 }
798
799 // --------------------------------------------------------------------
800
801 /**
802 * Product options
803 *
804 * Returns the an array of options, for a particular product row ID
805 *
806 * @param string $row_id = ''
807 * @return array
808 */
809 public function product_options($row_id = '')
810 {
811 return isset($this->_cart2_contents[$row_id]['options']) ? $this->_cart2_contents[$row_id]['options'] : array();
812 }
813
814 // --------------------------------------------------------------------
815
816 /**
817 * Format Number
818 *
819 * Returns the supplied number with commas and a decimal point.
820 *
821 * @param float
822 * @return string
823 */
824 public function format_number($n = '')
825 {
826 return ($n === '') ? '' : number_format( (float) $n, 2, '.', ',');
827 }
828
829 // --------------------------------------------------------------------
830
831 /**
832 * Destroy the cart2
833 *
834 * Empties the cart2 and kills the session
835 *
836 * @return void
837 */
838 public function destroy()
839 {
840 $this->_cart2_contents = array('cart2_total' => 0, 'total_items' => 0);
841 $this->CI->session->unset_userdata('cart2_contents');
842 }
843
844}