Quick Search:

View

Revision:

Diff

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