Quick Search:

View

Revision:

Diff

Diff from 1 to:

Annotations

Annotate by Age | Author | Mixed | None
/fisheye/browse/osCommerce/branches/hpdl/oscommerce/includes/classes/shopping_cart.php

Annotated File View

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