hpdl
|
477
|
1
|
<?php
|
|
2
|
/*
|
hpdl
|
1739
|
3
|
$Id: shopping_cart.php 1739 2007-12-20 00:52:16Z hpdl $
|
hpdl
|
477
|
4
|
|
|
5
|
osCommerce, Open Source E-Commerce Solutions
|
|
6
|
http://www.oscommerce.com
|
|
7
|
|
|
8
|
Copyright (c) 2003 osCommerce
|
|
9
|
|
|
10
|
Released under the GNU General Public License
|
|
11
|
*/
|
|
12
|
|
|
13
|
class shoppingCart {
|
|
14
|
var $contents, $total, $weight, $cartID, $content_type;
|
|
15
|
|
|
16
|
function shoppingCart() {
|
|
17
|
$this->reset();
|
|
18
|
}
|
|
19
|
|
|
20
|
function restore_contents() {
|
|
21
|
global $customer_id;
|
|
22
|
|
|
23
|
if (!tep_session_is_registered('customer_id')) return false;
|
|
24
|
|
|
25
|
// insert current cart contents in database
|
|
26
|
if (is_array($this->contents)) {
|
|
27
|
reset($this->contents);
|
|
28
|
while (list($products_id, ) = each($this->contents)) {
|
|
29
|
$qty = $this->contents[$products_id]['qty'];
|
|
30
|
$product_query = tep_db_query("select products_id from " . TABLE_CUSTOMERS_BASKET . " where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($products_id) . "'");
|
|
31
|
if (!tep_db_num_rows($product_query)) {
|
hpdl
|
1592
|
32
|
tep_db_query("insert into " . TABLE_CUSTOMERS_BASKET . " (customers_id, products_id, customers_basket_quantity, customers_basket_date_added) values ('" . (int)$customer_id . "', '" . tep_db_input($products_id) . "', '" . tep_db_input($qty) . "', '" . date('Ymd') . "')");
|
hpdl
|
477
|
33
|
if (isset($this->contents[$products_id]['attributes'])) {
|
|
34
|
reset($this->contents[$products_id]['attributes']);
|
|
35
|
while (list($option, $value) = each($this->contents[$products_id]['attributes'])) {
|
|
36
|
tep_db_query("insert into " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . " (customers_id, products_id, products_options_id, products_options_value_id) values ('" . (int)$customer_id . "', '" . tep_db_input($products_id) . "', '" . (int)$option . "', '" . (int)$value . "')");
|
|
37
|
}
|
|
38
|
}
|
|
39
|
} else {
|
hpdl
|
1592
|
40
|
tep_db_query("update " . TABLE_CUSTOMERS_BASKET . " set customers_basket_quantity = '" . tep_db_input($qty) . "' where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($products_id) . "'");
|
hpdl
|
477
|
41
|
}
|
|
42
|
}
|
|
43
|
}
|
|
44
|
|
|
45
|
// reset per-session cart contents, but not the database contents
|
|
46
|
$this->reset(false);
|
|
47
|
|
|
48
|
$products_query = tep_db_query("select products_id, customers_basket_quantity from " . TABLE_CUSTOMERS_BASKET . " where customers_id = '" . (int)$customer_id . "'");
|
|
49
|
while ($products = tep_db_fetch_array($products_query)) {
|
|
50
|
$this->contents[$products['products_id']] = array('qty' => $products['customers_basket_quantity']);
|
|
51
|
// attributes
|
|
52
|
$attributes_query = tep_db_query("select products_options_id, products_options_value_id from " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . " where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($products['products_id']) . "'");
|
|
53
|
while ($attributes = tep_db_fetch_array($attributes_query)) {
|
|
54
|
$this->contents[$products['products_id']]['attributes'][$attributes['products_options_id']] = $attributes['products_options_value_id'];
|
|
55
|
}
|
|
56
|
}
|
|
57
|
|
|
58
|
$this->cleanup();
|
|
59
|
}
|
|
60
|
|
|
61
|
function reset($reset_database = false) {
|
|
62
|
global $customer_id;
|
|
63
|
|
|
64
|
$this->contents = array();
|
|
65
|
$this->total = 0;
|
|
66
|
$this->weight = 0;
|
|
67
|
$this->content_type = false;
|
|
68
|
|
|
69
|
if (tep_session_is_registered('customer_id') && ($reset_database == true)) {
|
|
70
|
tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET . " where customers_id = '" . (int)$customer_id . "'");
|
|
71
|
tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . " where customers_id = '" . (int)$customer_id . "'");
|
|
72
|
}
|
|
73
|
|
|
74
|
unset($this->cartID);
|
|
75
|
if (tep_session_is_registered('cartID')) tep_session_unregister('cartID');
|
|
76
|
}
|
|
77
|
|
|
78
|
function add_cart($products_id, $qty = '1', $attributes = '', $notify = true) {
|
|
79
|
global $new_products_id_in_cart, $customer_id;
|
|
80
|
|
|
81
|
$products_id_string = tep_get_uprid($products_id, $attributes);
|
|
82
|
$products_id = tep_get_prid($products_id_string);
|
|
83
|
|
hpdl
|
1596
|
84
|
if (defined('MAX_QTY_IN_CART') && (MAX_QTY_IN_CART > 0) && ((int)$qty > MAX_QTY_IN_CART)) {
|
|
85
|
$qty = MAX_QTY_IN_CART;
|
|
86
|
}
|
|
87
|
|
hpdl
|
703
|
88
|
$attributes_pass_check = true;
|
|
89
|
|
|
90
|
if (is_array($attributes)) {
|
|
91
|
reset($attributes);
|
|
92
|
while (list($option, $value) = each($attributes)) {
|
|
93
|
if (!is_numeric($option) || !is_numeric($value)) {
|
|
94
|
$attributes_pass_check = false;
|
|
95
|
break;
|
|
96
|
}
|
|
97
|
}
|
|
98
|
}
|
|
99
|
|
|
100
|
if (is_numeric($products_id) && is_numeric($qty) && ($attributes_pass_check == true)) {
|
hpdl
|
477
|
101
|
$check_product_query = tep_db_query("select products_status from " . TABLE_PRODUCTS . " where products_id = '" . (int)$products_id . "'");
|
|
102
|
$check_product = tep_db_fetch_array($check_product_query);
|
|
103
|
|
|
104
|
if (($check_product !== false) && ($check_product['products_status'] == '1')) {
|
|
105
|
if ($notify == true) {
|
|
106
|
$new_products_id_in_cart = $products_id;
|
|
107
|
tep_session_register('new_products_id_in_cart');
|
|
108
|
}
|
|
109
|
|
|
110
|
if ($this->in_cart($products_id_string)) {
|
|
111
|
$this->update_quantity($products_id_string, $qty, $attributes);
|
|
112
|
} else {
|
hpdl
|
1595
|
113
|
$this->contents[$products_id_string] = array('qty' => (int)$qty);
|
hpdl
|
477
|
114
|
// insert into database
|
|
115
|
if (tep_session_is_registered('customer_id')) tep_db_query("insert into " . TABLE_CUSTOMERS_BASKET . " (customers_id, products_id, customers_basket_quantity, customers_basket_date_added) values ('" . (int)$customer_id . "', '" . tep_db_input($products_id_string) . "', '" . (int)$qty . "', '" . date('Ymd') . "')");
|
|
116
|
|
|
117
|
if (is_array($attributes)) {
|
|
118
|
reset($attributes);
|
|
119
|
while (list($option, $value) = each($attributes)) {
|
|
120
|
$this->contents[$products_id_string]['attributes'][$option] = $value;
|
|
121
|
// insert into database
|
|
122
|
if (tep_session_is_registered('customer_id')) tep_db_query("insert into " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . " (customers_id, products_id, products_options_id, products_options_value_id) values ('" . (int)$customer_id . "', '" . tep_db_input($products_id_string) . "', '" . (int)$option . "', '" . (int)$value . "')");
|
|
123
|
}
|
|
124
|
}
|
|
125
|
}
|
|
126
|
|
|
127
|
$this->cleanup();
|
|
128
|
|
|
129
|
// assign a temporary unique ID to the order contents to prevent hack attempts during the checkout procedure
|
|
130
|
$this->cartID = $this->generate_cart_id();
|
|
131
|
}
|
|
132
|
}
|
|
133
|
}
|
|
134
|
|
|
135
|
function update_quantity($products_id, $quantity = '', $attributes = '') {
|
|
136
|
global $customer_id;
|
|
137
|
|
|
138
|
$products_id_string = tep_get_uprid($products_id, $attributes);
|
|
139
|
$products_id = tep_get_prid($products_id_string);
|
|
140
|
|
hpdl
|
1596
|
141
|
if (defined('MAX_QTY_IN_CART') && (MAX_QTY_IN_CART > 0) && ((int)$quantity > MAX_QTY_IN_CART)) {
|
|
142
|
$quantity = MAX_QTY_IN_CART;
|
|
143
|
}
|
|
144
|
|
hpdl
|
703
|
145
|
$attributes_pass_check = true;
|
|
146
|
|
|
147
|
if (is_array($attributes)) {
|
|
148
|
reset($attributes);
|
|
149
|
while (list($option, $value) = each($attributes)) {
|
|
150
|
if (!is_numeric($option) || !is_numeric($value)) {
|
|
151
|
$attributes_pass_check = false;
|
|
152
|
break;
|
|
153
|
}
|
|
154
|
}
|
|
155
|
}
|
|
156
|
|
|
157
|
if (is_numeric($products_id) && isset($this->contents[$products_id_string]) && is_numeric($quantity) && ($attributes_pass_check == true)) {
|
hpdl
|
1595
|
158
|
$this->contents[$products_id_string] = array('qty' => (int)$quantity);
|
hpdl
|
477
|
159
|
// update database
|
|
160
|
if (tep_session_is_registered('customer_id')) tep_db_query("update " . TABLE_CUSTOMERS_BASKET . " set customers_basket_quantity = '" . (int)$quantity . "' where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($products_id_string) . "'");
|
|
161
|
|
|
162
|
if (is_array($attributes)) {
|
|
163
|
reset($attributes);
|
|
164
|
while (list($option, $value) = each($attributes)) {
|
|
165
|
$this->contents[$products_id_string]['attributes'][$option] = $value;
|
|
166
|
// update database
|
|
167
|
if (tep_session_is_registered('customer_id')) tep_db_query("update " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . " set products_options_value_id = '" . (int)$value . "' where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($products_id_string) . "' and products_options_id = '" . (int)$option . "'");
|
|
168
|
}
|
|
169
|
}
|
|
170
|
}
|
|
171
|
}
|
|
172
|
|
|
173
|
function cleanup() {
|
|
174
|
global $customer_id;
|
|
175
|
|
|
176
|
reset($this->contents);
|
|
177
|
while (list($key,) = each($this->contents)) {
|
|
178
|
if ($this->contents[$key]['qty'] < 1) {
|
|
179
|
unset($this->contents[$key]);
|
|
180
|
// remove from database
|
|
181
|
if (tep_session_is_registered('customer_id')) {
|
|
182
|
tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET . " where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($key) . "'");
|
|
183
|
tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . " where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($key) . "'");
|
|
184
|
}
|
|
185
|
}
|
|
186
|
}
|
|
187
|
}
|
|
188
|
|
|
189
|
function count_contents() { // get total number of items in cart
|
|
190
|
$total_items = 0;
|
|
191
|
if (is_array($this->contents)) {
|
|
192
|
reset($this->contents);
|
|
193
|
while (list($products_id, ) = each($this->contents)) {
|
|
194
|
$total_items += $this->get_quantity($products_id);
|
|
195
|
}
|
|
196
|
}
|
|
197
|
|
|
198
|
return $total_items;
|
|
199
|
}
|
|
200
|
|
|
201
|
function get_quantity($products_id) {
|
|
202
|
if (isset($this->contents[$products_id])) {
|
|
203
|
return $this->contents[$products_id]['qty'];
|
|
204
|
} else {
|
|
205
|
return 0;
|
|
206
|
}
|
|
207
|
}
|
|
208
|
|
|
209
|
function in_cart($products_id) {
|
|
210
|
if (isset($this->contents[$products_id])) {
|
|
211
|
return true;
|
|
212
|
} else {
|
|
213
|
return false;
|
|
214
|
}
|
|
215
|
}
|
|
216
|
|
|
217
|
function remove($products_id) {
|
|
218
|
global $customer_id;
|
|
219
|
|
|
220
|
unset($this->contents[$products_id]);
|
|
221
|
// remove from database
|
|
222
|
if (tep_session_is_registered('customer_id')) {
|
|
223
|
tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET . " where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($products_id) . "'");
|
|
224
|
tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . " where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($products_id) . "'");
|
|
225
|
}
|
|
226
|
|
|
227
|
// assign a temporary unique ID to the order contents to prevent hack attempts during the checkout procedure
|
|
228
|
$this->cartID = $this->generate_cart_id();
|
|
229
|
}
|
|
230
|
|
|
231
|
function remove_all() {
|
|
232
|
$this->reset();
|
|
233
|
}
|
|
234
|
|
|
235
|
function get_product_id_list() {
|
|
236
|
$product_id_list = '';
|
|
237
|
if (is_array($this->contents)) {
|
|
238
|
reset($this->contents);
|
|
239
|
while (list($products_id, ) = each($this->contents)) {
|
|
240
|
$product_id_list .= ', ' . $products_id;
|
|
241
|
}
|
|
242
|
}
|
|
243
|
|
|
244
|
return substr($product_id_list, 2);
|
|
245
|
}
|
|
246
|
|
|
247
|
function calculate() {
|
hpdl
|
1592
|
248
|
global $currencies;
|
|
249
|
|
hpdl
|
477
|
250
|
$this->total = 0;
|
|
251
|
$this->weight = 0;
|
|
252
|
if (!is_array($this->contents)) return 0;
|
|
253
|
|
|
254
|
reset($this->contents);
|
|
255
|
while (list($products_id, ) = each($this->contents)) {
|
|
256
|
$qty = $this->contents[$products_id]['qty'];
|
|
257
|
|
|
258
|
// products price
|
|
259
|
$product_query = tep_db_query("select products_id, products_price, products_tax_class_id, products_weight from " . TABLE_PRODUCTS . " where products_id = '" . (int)$products_id . "'");
|
|
260
|
if ($product = tep_db_fetch_array($product_query)) {
|
|
261
|
$prid = $product['products_id'];
|
|
262
|
$products_tax = tep_get_tax_rate($product['products_tax_class_id']);
|
|
263
|
$products_price = $product['products_price'];
|
|
264
|
$products_weight = $product['products_weight'];
|
|
265
|
|
|
266
|
$specials_query = tep_db_query("select specials_new_products_price from " . TABLE_SPECIALS . " where products_id = '" . (int)$prid . "' and status = '1'");
|
|
267
|
if (tep_db_num_rows ($specials_query)) {
|
|
268
|
$specials = tep_db_fetch_array($specials_query);
|
|
269
|
$products_price = $specials['specials_new_products_price'];
|
|
270
|
}
|
|
271
|
|
hpdl
|
1592
|
272
|
$this->total += $currencies->calculate_price($products_price, $products_tax, $qty);
|
hpdl
|
477
|
273
|
$this->weight += ($qty * $products_weight);
|
|
274
|
}
|
|
275
|
|
|
276
|
// attributes price
|
|
277
|
if (isset($this->contents[$products_id]['attributes'])) {
|
|
278
|
reset($this->contents[$products_id]['attributes']);
|
|
279
|
while (list($option, $value) = each($this->contents[$products_id]['attributes'])) {
|
|
280
|
$attribute_price_query = tep_db_query("select options_values_price, price_prefix from " . TABLE_PRODUCTS_ATTRIBUTES . " where products_id = '" . (int)$prid . "' and options_id = '" . (int)$option . "' and options_values_id = '" . (int)$value . "'");
|
|
281
|
$attribute_price = tep_db_fetch_array($attribute_price_query);
|
|
282
|
if ($attribute_price['price_prefix'] == '+') {
|
hpdl
|
1592
|
283
|
$this->total += $currencies->calculate_price($attribute_price['options_values_price'], $products_tax, $qty);
|
hpdl
|
477
|
284
|
} else {
|
hpdl
|
1592
|
285
|
$this->total -= $currencies->calculate_price($attribute_price['options_values_price'], $products_tax, $qty);
|
hpdl
|
477
|
286
|
}
|
|
287
|
}
|
|
288
|
}
|
|
289
|
}
|
|
290
|
}
|
|
291
|
|
|
292
|
function attributes_price($products_id) {
|
|
293
|
$attributes_price = 0;
|
|
294
|
|
|
295
|
if (isset($this->contents[$products_id]['attributes'])) {
|
|
296
|
reset($this->contents[$products_id]['attributes']);
|
|
297
|
while (list($option, $value) = each($this->contents[$products_id]['attributes'])) {
|
|
298
|
$attribute_price_query = tep_db_query("select options_values_price, price_prefix from " . TABLE_PRODUCTS_ATTRIBUTES . " where products_id = '" . (int)$products_id . "' and options_id = '" . (int)$option . "' and options_values_id = '" . (int)$value . "'");
|
|
299
|
$attribute_price = tep_db_fetch_array($attribute_price_query);
|
|
300
|
if ($attribute_price['price_prefix'] == '+') {
|
|
301
|
$attributes_price += $attribute_price['options_values_price'];
|
|
302
|
} else {
|
|
303
|
$attributes_price -= $attribute_price['options_values_price'];
|
|
304
|
}
|
|
305
|
}
|
|
306
|
}
|
|
307
|
|
|
308
|
return $attributes_price;
|
|
309
|
}
|
|
310
|
|
|
311
|
function get_products() {
|
|
312
|
global $languages_id;
|
|
313
|
|
|
314
|
if (!is_array($this->contents)) return false;
|
|
315
|
|
|
316
|
$products_array = array();
|
|
317
|
reset($this->contents);
|
|
318
|
while (list($products_id, ) = each($this->contents)) {
|
|
319
|
$products_query = tep_db_query("select p.products_id, pd.products_name, p.products_model, p.products_image, p.products_price, p.products_weight, p.products_tax_class_id from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd where p.products_id = '" . (int)$products_id . "' and pd.products_id = p.products_id and pd.language_id = '" . (int)$languages_id . "'");
|
|
320
|
if ($products = tep_db_fetch_array($products_query)) {
|
|
321
|
$prid = $products['products_id'];
|
|
322
|
$products_price = $products['products_price'];
|
|
323
|
|
|
324
|
$specials_query = tep_db_query("select specials_new_products_price from " . TABLE_SPECIALS . " where products_id = '" . (int)$prid . "' and status = '1'");
|
|
325
|
if (tep_db_num_rows($specials_query)) {
|
|
326
|
$specials = tep_db_fetch_array($specials_query);
|
|
327
|
$products_price = $specials['specials_new_products_price'];
|
|
328
|
}
|
|
329
|
|
|
330
|
$products_array[] = array('id' => $products_id,
|
|
331
|
'name' => $products['products_name'],
|
|
332
|
'model' => $products['products_model'],
|
|
333
|
'image' => $products['products_image'],
|
|
334
|
'price' => $products_price,
|
|
335
|
'quantity' => $this->contents[$products_id]['qty'],
|
|
336
|
'weight' => $products['products_weight'],
|
|
337
|
'final_price' => ($products_price + $this->attributes_price($products_id)),
|
|
338
|
'tax_class_id' => $products['products_tax_class_id'],
|
|
339
|
'attributes' => (isset($this->contents[$products_id]['attributes']) ? $this->contents[$products_id]['attributes'] : ''));
|
|
340
|
}
|
|
341
|
}
|
|
342
|
|
|
343
|
return $products_array;
|
|
344
|
}
|
|
345
|
|
|
346
|
function show_total() {
|
|
347
|
$this->calculate();
|
|
348
|
|
|
349
|
return $this->total;
|
|
350
|
}
|
|
351
|
|
|
352
|
function show_weight() {
|
|
353
|
$this->calculate();
|
|
354
|
|
|
355
|
return $this->weight;
|
|
356
|
}
|
|
357
|
|
|
358
|
function generate_cart_id($length = 5) {
|
|
359
|
return tep_create_random_value($length, 'digits');
|
|
360
|
}
|
|
361
|
|
|
362
|
function get_content_type() {
|
|
363
|
$this->content_type = false;
|
|
364
|
|
|
365
|
if ( (DOWNLOAD_ENABLED == 'true') && ($this->count_contents() > 0) ) {
|
|
366
|
reset($this->contents);
|
|
367
|
while (list($products_id, ) = each($this->contents)) {
|
|
368
|
if (isset($this->contents[$products_id]['attributes'])) {
|
|
369
|
reset($this->contents[$products_id]['attributes']);
|
|
370
|
while (list(, $value) = each($this->contents[$products_id]['attributes'])) {
|
|
371
|
$virtual_check_query = tep_db_query("select count(*) as total from " . TABLE_PRODUCTS_ATTRIBUTES . " pa, " . TABLE_PRODUCTS_ATTRIBUTES_DOWNLOAD . " pad where pa.products_id = '" . (int)$products_id . "' and pa.options_values_id = '" . (int)$value . "' and pa.products_attributes_id = pad.products_attributes_id");
|
|
372
|
$virtual_check = tep_db_fetch_array($virtual_check_query);
|
|
373
|
|
|
374
|
if ($virtual_check['total'] > 0) {
|
|
375
|
switch ($this->content_type) {
|
|
376
|
case 'physical':
|
|
377
|
$this->content_type = 'mixed';
|
|
378
|
|
|
379
|
return $this->content_type;
|
|
380
|
break;
|
|
381
|
default:
|
|
382
|
$this->content_type = 'virtual';
|
|
383
|
break;
|
|
384
|
}
|
|
385
|
} else {
|
|
386
|
switch ($this->content_type) {
|
|
387
|
case 'virtual':
|
|
388
|
$this->content_type = 'mixed';
|
|
389
|
|
|
390
|
return $this->content_type;
|
|
391
|
break;
|
|
392
|
default:
|
|
393
|
$this->content_type = 'physical';
|
|
394
|
break;
|
|
395
|
}
|
|
396
|
}
|
|
397
|
}
|
|
398
|
} else {
|
|
399
|
switch ($this->content_type) {
|
|
400
|
case 'virtual':
|
|
401
|
$this->content_type = 'mixed';
|
|
402
|
|
|
403
|
return $this->content_type;
|
|
404
|
break;
|
|
405
|
default:
|
|
406
|
$this->content_type = 'physical';
|
|
407
|
break;
|
|
408
|
}
|
|
409
|
}
|
|
410
|
}
|
|
411
|
} else {
|
|
412
|
$this->content_type = 'physical';
|
|
413
|
}
|
|
414
|
|
|
415
|
return $this->content_type;
|
|
416
|
}
|
|
417
|
|
|
418
|
function unserialize($broken) {
|
|
419
|
for(reset($broken);$kv=each($broken);) {
|
|
420
|
$key=$kv['key'];
|
|
421
|
if (gettype($this->$key)!="user function")
|
|
422
|
$this->$key=$kv['value'];
|
|
423
|
}
|
|
424
|
}
|
|
425
|
|
|
426
|
}
|
|
427
|
?>
|