Quick Search:

View

Revision:

Diff

Diff from 1842 to:

Annotations

Annotate by Age | Author | Mixed | None
/fisheye/browse/osCommerce/oscommerce2/trunk/catalog/includes/classes/shopping_cart.php

Annotated File View

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