@@ -21,7 +21,7 @@ public function __construct()
21
21
}
22
22
23
23
//add items to cart
24
- public function add ($ item = array ())
24
+ public function add ($ item = array (), $ discount = 0 )
25
25
{
26
26
// Was any cart data passed?...
27
27
if (!is_array ($ item ) || count ($ item ) === 0 ) {
@@ -50,6 +50,9 @@ public function add($item = array())
50
50
}
51
51
$ item ['rowid ' ] = $ rowid ;
52
52
$ this ->CartContents [$ rowid ] = $ item ;
53
+ if ($ discount > 0 ) {
54
+ $ this ->addDiscount ($ rowid , $ discount );
55
+ }
53
56
$ this ->save_cart ();
54
57
} else {
55
58
die ('Error , The cart array must contain a product ID, quantity, price, and name. ' );
@@ -104,7 +107,11 @@ protected function save_cart()
104
107
105
108
$ this ->CartContents ['cart_total ' ] += ($ val ['price ' ] * $ val ['quantity ' ]);
106
109
$ this ->CartContents ['total_items ' ] += $ val ['quantity ' ];
107
- $ this ->CartContents [$ key ]['subtotal ' ] = ($ this ->CartContents [$ key ]['price ' ] * $ this ->CartContents [$ key ]['quantity ' ]);
110
+ $ subtotal = $ this ->CartContents [$ key ]['subtotal ' ] = ($ this ->CartContents [$ key ]['price ' ] * $ this ->CartContents [$ key ]['quantity ' ]);
111
+ if (isset ($ this ->CartContents [$ key ]['discount ' ])) {
112
+ $ this ->CartContents [$ key ]['subtotal ' ] = $ subtotal - ($ subtotal * ($ this ->CartContents [$ key ]['discount ' ] / 100 ));
113
+ $ this ->CartContents [$ key ]['subtotalWithoutDiscount ' ] = $ subtotal ;
114
+ }
108
115
}
109
116
if (count ($ this ->CartContents ) <= 2 ) {
110
117
unset($ _SESSION ['Cartie ' ]);
@@ -166,4 +173,11 @@ public function clear()
166
173
$ this ->CartContents = array ('cart_total ' => 0 , 'total_items ' => 0 );
167
174
unset($ _SESSION ['Cartie ' ]);
168
175
}
176
+
177
+ protected function addDiscount ($ rowid , $ discount )
178
+ {
179
+ if (isset ($ this ->CartContents [$ rowid ])) {
180
+ $ this ->CartContents [$ rowid ]['discount ' ] = $ discount ;
181
+ }
182
+ }
169
183
}
0 commit comments