Quick Search:

View

Revision:

Diff

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