· 4 years ago · Mar 22, 2021, 02:10 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 Cart Class
42 *
43 * @package CodeIgniter
44 * @subpackage Libraries
45 * @category Shopping Cart
46 * @author EllisLab Dev Team
47 * @link https://codeigniter.com/user_guide/libraries/cart.html
48 * @deprecated 3.0.0 This class is too specific for CI.
49 */
50class CI_Cart {
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 cart
86 *
87 * @var array
88 */
89 protected $_cart_contents = array();
90
91 /**
92 * Shopping Class Constructor
93 *
94 * The constructor loads the Session class, used to store the shopping cart 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 cart array from the session table
111 $this->_cart_contents = $this->CI->session->userdata('cart_contents');
112 if ($this->_cart_contents === NULL)
113 {
114 // No cart exists so we'll set some base values
115 $this->_cart_contents = array('cart_total' => 0, 'total_items' => 0);
116 }
117
118 log_message('info', 'Cart Class Initialized');
119 }
120
121 // --------------------------------------------------------------------
122
123 /**
124 * Insert items into the cart 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 cart 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_cart = FALSE;
144 if (isset($items['id']))
145 {
146 if (($rowid = $this->_insert($items)))
147 {
148 $save_cart = 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_cart = TRUE;
160 }
161 }
162 }
163 }
164
165 // Save the cart data if the insert was successful
166 if ($save_cart === TRUE)
167 {
168 $this->_save_cart();
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 cart 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['discount'], $items['status']))
196 {
197 log_message('error', 'The cart 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['discount'] = (float) $items['discount'];
238 $items['status'] = (float) $items['status'];
239
240 // We now need to create a unique identifier for the item being inserted into the cart.
241 // Every time something is added to the cart it is stored in the master cart array.
242 // Each row in the cart array, however, must have a unique index that identifies not only
243 // a particular product, but makes it possible to store identical products with different options.
244 // For example, what if someone buys two identical t-shirts (same product ID), but in
245 // different sizes? The product ID (and other attributes, like the name) will be identical for
246 // both sizes because it's the same shirt. The only difference will be the size.
247 // Internally, we need to treat identical submissions, but with different options, as a unique product.
248 // Our solution is to convert the options array to a string and MD5 it along with the product ID.
249 // This becomes the unique "row ID"
250 if (isset($items['options']) && count($items['options']) > 0)
251 {
252 $rowid = md5($items['id'].serialize($items['options']));
253 }
254 else
255 {
256 // No options were submitted so we simply MD5 the product ID.
257 // Technically, we don't need to MD5 the ID in this case, but it makes
258 // sense to standardize the format of array indexes for both conditions
259 $rowid = md5($items['id']);
260 }
261
262 // --------------------------------------------------------------------
263
264 // Now that we have our unique "row ID", we'll add our cart items to the master array
265 // grab quantity if it's already there and add it on
266 $old_quantity = isset($this->_cart_contents[$rowid]['qty']) ? (int) $this->_cart_contents[$rowid]['qty'] : 0;
267
268 // Re-create the entry, just to make sure our index contains only the data from this submission
269 $items['rowid'] = $rowid;
270 $items['qty'] += $old_quantity;
271 $this->_cart_contents[$rowid] = $items;
272
273 return $rowid;
274 }
275
276 public function insert_pembelian($items = array())
277 {
278 // Was any cart data passed? No? Bah...
279 if ( ! is_array($items) OR count($items) === 0)
280 {
281 log_message('error', 'The insert method must be passed an array containing data.');
282 return FALSE;
283 }
284
285 // You can either insert a single product using a one-dimensional array,
286 // or multiple products using a multi-dimensional one. The way we
287 // determine the array type is by looking for a required array key named "id"
288 // at the top level. If it's not found, we will assume it's a multi-dimensional array.
289
290 $save_cart_pembelian = FALSE;
291 if (isset($items['id']))
292 {
293 if (($rowid = $this->_insert_pembelian($items)))
294 {
295 $save_cart_pembelian = TRUE;
296 }
297 }
298 else
299 {
300 foreach ($items as $val)
301 {
302 if (is_array($val) && isset($val['id']))
303 {
304 if ($this->_insert_pembelian($val))
305 {
306 $save_cart_pembelian = TRUE;
307 }
308 }
309 }
310 }
311
312 // Save the cart data if the insert was successful
313 if ($save_cart_pembelian === TRUE)
314 {
315 $this->_save_cart_pembelian();
316 return isset($rowid) ? $rowid : TRUE;
317 }
318
319 return FALSE;
320 }
321
322 // --------------------------------------------------------------------
323
324 /**
325 * Insert
326 *
327 * @param array
328 * @return bool
329 */
330 protected function _insert_pembelian($items = array())
331 {
332 // Was any cart data passed? No? Bah...
333 if ( ! is_array($items) OR count($items) === 0)
334 {
335 log_message('error', 'The insert method must be passed an array containing data.');
336 return FALSE;
337 }
338
339 // --------------------------------------------------------------------
340
341 // Does the $items array contain an id, quantity, price, and name? These are required
342 if ( ! isset($items['id'], $items['qty'], $items['price'], $items['name'], $items['status']))
343 {
344 log_message('error', 'The cart array must contain a product ID, quantity, price, and name.');
345 return FALSE;
346 }
347
348 // --------------------------------------------------------------------
349
350 // Prep the quantity. It can only be a number. Duh... also trim any leading zeros
351 $items['qty'] = (float) $items['qty'];
352
353 // If the quantity is zero or blank there's nothing for us to do
354 if ($items['qty'] == 0)
355 {
356 return FALSE;
357 }
358
359 // --------------------------------------------------------------------
360
361 // Validate the product ID. It can only be alpha-numeric, dashes, underscores or periods
362 // Not totally sure we should impose this rule, but it seems prudent to standardize IDs.
363 // Note: These can be user-specified by setting the $this->product_id_rules variable.
364 if ( ! preg_match('/^['.$this->product_id_rules.']+$/i', $items['id']))
365 {
366 log_message('error', 'Invalid product ID. The product ID can only contain alpha-numeric characters, dashes, and underscores');
367 return FALSE;
368 }
369
370 // --------------------------------------------------------------------
371
372 // Validate the product name. It can only be alpha-numeric, dashes, underscores, colons or periods.
373 // Note: These can be user-specified by setting the $this->product_name_rules variable.
374 if ($this->product_name_safe && ! preg_match('/^['.$this->product_name_rules.']+$/i'.(UTF8_ENABLED ? 'u' : ''), $items['name']))
375 {
376 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');
377 return FALSE;
378 }
379
380 // --------------------------------------------------------------------
381
382 // Prep the price. Remove leading zeros and anything that isn't a number or decimal point.
383 $items['price'] = (float) $items['price'];
384 $items['status'] = (float) $items['status'];
385
386 // We now need to create a unique identifier for the item being inserted into the cart.
387 // Every time something is added to the cart it is stored in the master cart array.
388 // Each row in the cart array, however, must have a unique index that identifies not only
389 // a particular product, but makes it possible to store identical products with different options.
390 // For example, what if someone buys two identical t-shirts (same product ID), but in
391 // different sizes? The product ID (and other attributes, like the name) will be identical for
392 // both sizes because it's the same shirt. The only difference will be the size.
393 // Internally, we need to treat identical submissions, but with different options, as a unique product.
394 // Our solution is to convert the options array to a string and MD5 it along with the product ID.
395 // This becomes the unique "row ID"
396 if (isset($items['options']) && count($items['options']) > 0)
397 {
398 $rowid = md5($items['id'].serialize($items['options']));
399 }
400 else
401 {
402 // No options were submitted so we simply MD5 the product ID.
403 // Technically, we don't need to MD5 the ID in this case, but it makes
404 // sense to standardize the format of array indexes for both conditions
405 $rowid = md5($items['id']);
406 }
407
408 // --------------------------------------------------------------------
409
410 // Now that we have our unique "row ID", we'll add our cart items to the master array
411 // grab quantity if it's already there and add it on
412 $old_quantity = isset($this->_cart_contents[$rowid]['qty']) ? (int) $this->_cart_contents[$rowid]['qty'] : 0;
413
414 // Re-create the entry, just to make sure our index contains only the data from this submission
415 $items['rowid'] = $rowid;
416 $items['qty'] += $old_quantity;
417 $this->_cart_contents[$rowid] = $items;
418
419 return $rowid;
420 }
421
422 // --------------------------------------------------------------------
423
424 /**
425 * Update the cart
426 *
427 * This function permits the quantity of a given item to be changed.
428 * Typically it is called from the "view cart" page if a user makes
429 * changes to the quantity before checkout. That array must contain the
430 * product ID and quantity for each item.
431 *
432 * @param array
433 * @return bool
434 */
435 public function update($items = array())
436 {
437 // Was any cart data passed?
438 if ( ! is_array($items) OR count($items) === 0)
439 {
440 return FALSE;
441 }
442
443 // You can either update a single product using a one-dimensional array,
444 // or multiple products using a multi-dimensional one. The way we
445 // determine the array type is by looking for a required array key named "rowid".
446 // If it's not found we assume it's a multi-dimensional array
447 $save_cart = FALSE;
448 if (isset($items['rowid']))
449 {
450 if ($this->_update($items) === TRUE)
451 {
452 $save_cart = TRUE;
453 }
454 }
455 else
456 {
457 foreach ($items as $val)
458 {
459 if (is_array($val) && isset($val['rowid']))
460 {
461 if ($this->_update($val) === TRUE)
462 {
463 $save_cart = TRUE;
464 }
465 }
466 }
467 }
468
469 // Save the cart data if the insert was successful
470 if ($save_cart === TRUE)
471 {
472 $this->_save_cart();
473 return TRUE;
474 }
475
476 return FALSE;
477 }
478
479 // --------------------------------------------------------------------
480
481 /**
482 * Update the cart
483 *
484 * This function permits changing item properties.
485 * Typically it is called from the "view cart" page if a user makes
486 * changes to the quantity before checkout. That array must contain the
487 * rowid and quantity for each item.
488 *
489 * @param array
490 * @return bool
491 */
492 protected function _update($items = array())
493 {
494 // Without these array indexes there is nothing we can do
495 if ( ! isset($items['rowid'], $this->_cart_contents[$items['rowid']]))
496 {
497 return FALSE;
498 }
499
500 // Prep the quantity
501 if (isset($items['qty']))
502 {
503 $items['qty'] = (float) $items['qty'];
504 // Is the quantity zero? If so we will remove the item from the cart.
505 // If the quantity is greater than zero we are updating
506 if ($items['qty'] == 0)
507 {
508 unset($this->_cart_contents[$items['rowid']]);
509 return TRUE;
510 }
511 }
512
513 // find updatable keys
514 $keys = array_intersect(array_keys($this->_cart_contents[$items['rowid']]), array_keys($items));
515 // if a price was passed, make sure it contains valid data
516 if (isset($items['price']))
517 {
518 $items['price'] = (float) $items['price'];
519 }
520
521 // product id & name shouldn't be changed
522 foreach (array_diff($keys, array('id', 'name')) as $key)
523 {
524 $this->_cart_contents[$items['rowid']][$key] = $items[$key];
525 }
526
527 return TRUE;
528 }
529
530
531 public function update_pembelian($items = array())
532 {
533 // Was any cart data passed?
534 if ( ! is_array($items) OR count($items) === 0)
535 {
536 return FALSE;
537 }
538
539 // You can either update a single product using a one-dimensional array,
540 // or multiple products using a multi-dimensional one. The way we
541 // determine the array type is by looking for a required array key named "rowid".
542 // If it's not found we assume it's a multi-dimensional array
543 $save_cart_pembelian = FALSE;
544 if (isset($items['rowid']))
545 {
546 if ($this->_update_pembelian($items) === TRUE)
547 {
548 $save_cart_pembelian = TRUE;
549 }
550 }
551 else
552 {
553 foreach ($items as $val)
554 {
555 if (is_array($val) && isset($val['rowid']))
556 {
557 if ($this->_update_pembelian($val) === TRUE)
558 {
559 $save_cart_pembelian = TRUE;
560 }
561 }
562 }
563 }
564
565 // Save the cart data if the insert was successful
566 if ($save_cart_pembelian === TRUE)
567 {
568 $this->_save_cart_pembelian();
569 return TRUE;
570 }
571
572 return FALSE;
573 }
574
575 // --------------------------------------------------------------------
576
577 /**
578 * Update the cart
579 *
580 * This function permits changing item properties.
581 * Typically it is called from the "view cart" page if a user makes
582 * changes to the quantity before checkout. That array must contain the
583 * rowid and quantity for each item.
584 *
585 * @param array
586 * @return bool
587 */
588 protected function _update_pembelian($items = array())
589 {
590 // Without these array indexes there is nothing we can do
591 if ( ! isset($items['rowid'], $this->_cart_contents[$items['rowid']]))
592 {
593 return FALSE;
594 }
595
596 // Prep the quantity
597 if (isset($items['qty']))
598 {
599 $items['qty'] = (float) $items['qty'];
600 // Is the quantity zero? If so we will remove the item from the cart.
601 // If the quantity is greater than zero we are updating
602 if ($items['qty'] == 0)
603 {
604 unset($this->_cart_contents[$items['rowid']]);
605 return TRUE;
606 }
607 }
608
609 // find updatable keys
610 $keys = array_intersect(array_keys($this->_cart_contents[$items['rowid']]), array_keys($items));
611 // if a price was passed, make sure it contains valid data
612 if (isset($items['price']))
613 {
614 $items['price'] = (float) $items['price'];
615 }
616
617 // product id & name shouldn't be changed
618 foreach (array_diff($keys, array('id', 'name')) as $key)
619 {
620 $this->_cart_contents[$items['rowid']][$key] = $items[$key];
621 }
622
623 return TRUE;
624 }
625
626 // --------------------------------------------------------------------
627
628 /**
629 * Save the cart array to the session DB
630 *
631 * @return bool
632 */
633 protected function _save_cart()
634 {
635 // Let's add up the individual prices and set the cart sub-total
636 $this->_cart_contents['total_items'] = $this->_cart_contents['cart_total'] = 0;
637 foreach ($this->_cart_contents as $key => $val)
638 {
639 // We make sure the array contains the proper indexes
640 if ( ! is_array($val) OR ! isset($val['price'], $val['qty'], $val['discount']))
641 {
642 continue;
643 }
644
645 $this->_cart_contents['cart_total'] += ($val['price'] * ((100 - $val['discount'])/100) * $val['qty']);
646 $this->_cart_contents['total_items'] += $val['qty'];
647 $this->_cart_contents[$key]['subtotal'] = ($this->_cart_contents[$key]['price'] * ((100 - $this->_cart_contents[$key]['discount'])/100) * $this->_cart_contents[$key]['qty']);
648 }
649
650 // Is our cart empty? If so we delete it from the session
651 if (count($this->_cart_contents) <= 2)
652 {
653 $this->CI->session->unset_userdata('cart_contents');
654
655 // Nothing more to do... coffee time!
656 return FALSE;
657 }
658
659 // If we made it this far it means that our cart has data.
660 // Let's pass it to the Session class so it can be stored
661 $this->CI->session->set_userdata(array('cart_contents' => $this->_cart_contents));
662
663 // Woot!
664 return TRUE;
665 }
666
667
668 protected function _save_cart_pembelian()
669 {
670 // Let's add up the individual prices and set the cart sub-total
671 $this->_cart_contents['total_items'] = $this->_cart_contents['cart_total'] = 0;
672 foreach ($this->_cart_contents as $key => $val)
673 {
674 // We make sure the array contains the proper indexes
675 if ( ! is_array($val) OR ! isset($val['price'], $val['qty']))
676 {
677 continue;
678 }
679
680 $this->_cart_contents['cart_total'] += ($val['price'] * $val['qty']);
681 $this->_cart_contents['total_items'] += $val['qty'];
682 $this->_cart_contents[$key]['subtotal'] = ($this->_cart_contents[$key]['price'] * $this->_cart_contents[$key]['qty']);
683 }
684
685 // Is our cart empty? If so we delete it from the session
686 if (count($this->_cart_contents) <= 2)
687 {
688 $this->CI->session->unset_userdata('cart_contents');
689
690 // Nothing more to do... coffee time!
691 return FALSE;
692 }
693
694 // If we made it this far it means that our cart has data.
695 // Let's pass it to the Session class so it can be stored
696 $this->CI->session->set_userdata(array('cart_contents' => $this->_cart_contents));
697
698 // Woot!
699 return TRUE;
700 }
701 // --------------------------------------------------------------------
702
703 /**
704 * Cart Total
705 *
706 * @return int
707 */
708 public function total()
709 {
710 return $this->_cart_contents['cart_total'];
711 }
712
713 // --------------------------------------------------------------------
714
715 /**
716 * Remove Item
717 *
718 * Removes an item from the cart
719 *
720 * @param int
721 * @return bool
722 */
723 public function remove($rowid)
724 {
725 // unset & save
726 unset($this->_cart_contents[$rowid]);
727 $this->_save_cart();
728 return TRUE;
729 }
730
731 // --------------------------------------------------------------------
732
733 /**
734 * Total Items
735 *
736 * Returns the total item count
737 *
738 * @return int
739 */
740 public function total_items()
741 {
742 return $this->_cart_contents['total_items'];
743 }
744
745 // --------------------------------------------------------------------
746
747 /**
748 * Cart Contents
749 *
750 * Returns the entire cart array
751 *
752 * @param bool
753 * @return array
754 */
755 public function contents($newest_first = FALSE)
756 {
757 // do we want the newest first?
758 $cart = ($newest_first) ? array_reverse($this->_cart_contents) : $this->_cart_contents;
759
760 // Remove these so they don't create a problem when showing the cart table
761 unset($cart['total_items']);
762 unset($cart['cart_total']);
763
764 return $cart;
765 }
766
767 // --------------------------------------------------------------------
768
769 /**
770 * Get cart item
771 *
772 * Returns the details of a specific item in the cart
773 *
774 * @param string $row_id
775 * @return array
776 */
777 public function get_item($row_id)
778 {
779 return (in_array($row_id, array('total_items', 'cart_total'), TRUE) OR ! isset($this->_cart_contents[$row_id]))
780 ? FALSE
781 : $this->_cart_contents[$row_id];
782 }
783
784 // --------------------------------------------------------------------
785
786 /**
787 * Has options
788 *
789 * Returns TRUE if the rowid passed to this function correlates to an item
790 * that has options associated with it.
791 *
792 * @param string $row_id = ''
793 * @return bool
794 */
795 public function has_options($row_id = '')
796 {
797 return (isset($this->_cart_contents[$row_id]['options']) && count($this->_cart_contents[$row_id]['options']) !== 0);
798 }
799
800 // --------------------------------------------------------------------
801
802 /**
803 * Product options
804 *
805 * Returns the an array of options, for a particular product row ID
806 *
807 * @param string $row_id = ''
808 * @return array
809 */
810 public function product_options($row_id = '')
811 {
812 return isset($this->_cart_contents[$row_id]['options']) ? $this->_cart_contents[$row_id]['options'] : array();
813 }
814
815 // --------------------------------------------------------------------
816
817 /**
818 * Format Number
819 *
820 * Returns the supplied number with commas and a decimal point.
821 *
822 * @param float
823 * @return string
824 */
825 public function format_number($n = '')
826 {
827 return ($n === '') ? '' : number_format( (float) $n, 2, '.', ',');
828 }
829
830 // --------------------------------------------------------------------
831
832 /**
833 * Destroy the cart
834 *
835 * Empties the cart and kills the session
836 *
837 * @return void
838 */
839 public function destroy()
840 {
841 $this->_cart_contents = array('cart_total' => 0, 'total_items' => 0);
842 $this->CI->session->unset_userdata('cart_contents');
843 }
844
845}