Quick Search:

View

Revision:

Diff

Diff from 19 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 /*
hpdl
19
3   $Id: shopping_cart.php 19 2005-02-25 02:57:18Z hpdl $
hpdl
1
4
5   osCommerce, Open Source E-Commerce Solutions
6   http://www.oscommerce.com
7
hpdl
19
8   Copyright (c) 2005 osCommerce
hpdl
1
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() {
hpdl
19
21       global $osC_Database, $osC_Customer;
hpdl
1
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'];
hpdl
19
30
31           $Qproduct = $osC_Database->query('select products_id from :table_customers_basket where customers_id = :customers_id and products_id = :products_id');
32           $Qproduct->bindTable(':table_customers_basket', TABLE_CUSTOMERS_BASKET);
33           $Qproduct->bindInt(':customers_id', $osC_Customer->id);
34           $Qproduct->bindValue(':products_id', $products_id);
35           $Qproduct->execute();
36
37           if ($Qproduct->numberOfRows() < 1) {
38             $Qnew = $osC_Database->query('insert into :table_customers_basket (customers_id, products_id, customers_basket_quantity, customers_basket_date_added) values (:customers_id, :products_id, :customers_basket_quantity, :customers_basket_date_added)');
39             $Qnew->bindTable(':table_customers_basket', TABLE_CUSTOMERS_BASKET);
40             $Qnew->bindInt(':customers_id', $osC_Customer->id);
41             $Qnew->bindValue(':products_id', $products_id);
42             $Qnew->bindInt(':customers_basket_quantity', $qty);
43             $Qnew->bindValue(':customers_basket_date_added', date('Ymd'));
44             $Qnew->execute();
45
hpdl
1
46             if (isset($this->contents[$products_id]['attributes'])) {
47               reset($this->contents[$products_id]['attributes']);
48               while (list($option, $value) = each($this->contents[$products_id]['attributes'])) {
hpdl
19
49                 $Qnew = $osC_Database->query('insert into :table_customers_basket_attributes (customers_id, products_id, products_options_id, products_options_value_id) values (:customers_id, :products_id, :products_options_id, :products_options_value_id)');
50                 $Qnew->bindTable(':table_customers_basket_attributes', TABLE_CUSTOMERS_BASKET_ATTRIBUTES);
51                 $Qnew->bindInt(':customers_id', $osC_Customer->id);
52                 $Qnew->bindValue(':products_id', $products_id);
53                 $Qnew->bindInt(':products_options_id', $option);
54                 $Qnew->bindInt(':products_options_value_id', $value);
55                 $Qnew->execute();
hpdl
1
56               }
57             }
58           } else {
hpdl
19
59             $Qupdate = $osC_Database->query('update :table_customers_basket set customers_basket_quantity = :customers_basket_quantity where customers_id = :customers_id and products_id = :products_id');
60             $Qupdate->bindTable(':table_customers_basket', TABLE_CUSTOMERS_BASKET);
61             $Qupdate->bindInt(':customers_basket_quantity', $qty);
62             $Qupdate->bindInt(':customers_id', $osC_Customer->id);
63             $Qupdate->bindValue(':products_id', $products_id);
64             $Qupdate->execute();
hpdl
1
65           }
66         }
67       }
68
69 // reset per-session cart contents, but not the database contents
70       $this->reset(false);
71
hpdl
19
72       $Qproducts = $osC_Database->query('select products_id, customers_basket_quantity from :table_customers_basket where customers_id = :customers_id');
73       $Qproducts->bindTable(':table_customers_basket', TABLE_CUSTOMERS_BASKET);
74       $Qproducts->bindInt(':customers_id', $osC_Customer->id);
75       $Qproducts->execute();
76
77       while ($Qproducts->next()) {
78         $this->contents[$Qproducts->value('products_id')] = array('qty' => $Qproducts->valueInt('customers_basket_quantity'));
hpdl
1
79 // attributes
hpdl
19
80         $Qattributes = $osC_Database->query('select products_options_id, products_options_value_id from :table_customers_basket_attributes where customers_id = :customers_id and products_id = :products_id');
81         $Qattributes->bindTable(':table_customers_basket_attributes', TABLE_CUSTOMERS_BASKET_ATTRIBUTES);
82         $Qattributes->bindInt(':customers_id', $osC_Customer->id);
83         $Qattributes->bindValue(':products_id', $Qproducts->value('products_id'));
84         $Qattributes->execute();
85
86         while ($Qattributes->next()) {
87           $this->contents[$Qproducts->value('products_id')]['attributes'][$Qattributes->valueInt('products_options_id')] = $Qattributes->valueInt('products_options_value_id');
hpdl
1
88         }
89       }
90
91       $this->cleanup();
92     }
93
94     function reset($reset_database = false) {
hpdl
19
95       global $osC_Database, $osC_Session, $osC_Customer;
hpdl
1
96
97       $this->contents = array();
98       $this->total = 0;
99       $this->weight = 0;
100       $this->content_type = false;
101
102       if (($reset_database == true) && $osC_Customer->isLoggedOn()) {
hpdl
19
103         $Qdelete = $osC_Database->query('delete from :table_customers_basket where customers_id = :customers_id');
104         $Qdelete->bindTable(':table_customers_basket', TABLE_CUSTOMERS_BASKET);
105         $Qdelete->bindInt(':customers_id', $osC_Customer->id);
106         $Qdelete->execute();
107
108         $Qdelete = $osC_Database->query('delete from :table_customers_basket_attributes where customers_id = :customers_id');
109         $Qdelete->bindTable(':table_customers_basket_attributes', TABLE_CUSTOMERS_BASKET_ATTRIBUTES);
110         $Qdelete->bindInt(':customers_id', $osC_Customer->id);
111         $Qdelete->execute();
hpdl
1
112       }
113
114       unset($this->cartID);
115       $osC_Session->remove('cartID');
116     }
117
118     function add_cart($products_id, $qty = '1', $attributes = '', $notify = true) {
hpdl
19
119       global $osC_Database, $osC_Session, $osC_Customer;
hpdl
1
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) && is_numeric($qty)) {
hpdl
19
125         $Qcheck = $osC_Database->query('select products_status from :table_products where products_id = :products_id');
126         $Qcheck->bindTable(':table_products', TABLE_PRODUCTS);
127         $Qcheck->bindInt(':products_id', $products_id);
128         $Qcheck->execute();
hpdl
1
129
hpdl
19
130         if (($check_product !== false) && ($Qcheck->valueInt('products_status') == '1')) {
hpdl
1
131           if ($notify == true) {
132             $osC_Session->set('new_products_id_in_cart', $products_id_string);
133           }
134
135           if ($this->in_cart($products_id_string)) {
136             $this->update_quantity($products_id_string, $qty, $attributes);
137           } else {
138             $this->contents[$products_id_string] = array('qty' => $qty);
139 // insert into database
hpdl
19
140             if ($osC_Customer->isLoggedOn()) {
141               $Qnew = $osC_Database->query('insert into :table_customers_basket (customers_id, products_id, customers_basket_quantity, customers_basket_date_added) values (:customers_id, :products_id, :customers_basket_quantity, :customers_basket_date_added)');
142               $Qnew->bindTable(':table_customers_basket', TABLE_CUSTOMERS_BASKET);
143               $Qnew->bindInt(':customers_id', $osC_Customer->id);
144               $Qnew->bindValue(':products_id', $products_id_string);
145               $Qnew->bindInt(':customers_basket_quantity', $qty);
146               $Qnew->bindValue(':customers_basket_date_added', date('Ymd'));
147               $Qnew->execute();
148             }
hpdl
1
149
150             if (is_array($attributes)) {
151               reset($attributes);
152               while (list($option, $value) = each($attributes)) {
153                 $this->contents[$products_id_string]['attributes'][$option] = $value;
154 // insert into database
hpdl
19
155                 if ($osC_Customer->isLoggedOn()) {
156                   $Qnew = $osC_Database->query('insert into :table_customers_basket_attributes (customers_id, products_id, products_options_id, products_options_value_id) values (:customers_id, :products_id, :products_options_id, :products_options_value_id)');
157                   $Qnew->bindTable(':table_customers_basket_attributes', TABLE_CUSTOMERS_BASKET_ATTRIBUTES);
158                   $Qnew->bindInt(':customers_id', $osC_Customer->id);
159                   $Qnew->bindValue(':products_id', $products_id_string);
160                   $Qnew->bindInt(':products_options_id', $option);
161                   $Qnew->bindInt(':products_options_value_id', $value);
162                   $Qnew->execute();
163                 }
hpdl
1
164               }
165             }
166           }
167
168           $this->cleanup();
169
170 // assign a temporary unique ID to the order contents to prevent hack attempts during the checkout procedure
171           $this->cartID = $this->generate_cart_id();
172         }
173       }
174     }
175
176     function update_quantity($products_id, $quantity = '', $attributes = '') {
hpdl
19
177       global $osC_Database, $osC_Customer;
hpdl
1
178
179       $products_id_string = tep_get_uprid($products_id, $attributes);
180       $products_id = tep_get_prid($products_id_string);
181
182       if (is_numeric($products_id) && isset($this->contents[$products_id_string]) && is_numeric($quantity)) {
183         $this->contents[$products_id_string] = array('qty' => $quantity);
184 // update database
hpdl
19
185         if ($osC_Customer->isLoggedOn()) {
186           $Qupdate = $osC_Database->query('update :table_customers_basket set customers_basket_quantity = :customers_basket_quantity where customers_id = :customers_id and products_id = :products_id');
187           $Qupdate->bindTable(':table_customers_basket', TABLE_CUSTOMERS_BASKET);
188           $Qupdate->bindInt(':customers_basket_quantity', $quantity);
189           $Qupdate->bindInt(':customers_id', $osC_Customer->id);
190           $Qupdate->bindValue(':products_id', $products_id_string);
191           $Qupdate->execute();
192         }
hpdl
1
193
194         if (is_array($attributes)) {
195           reset($attributes);
196           while (list($option, $value) = each($attributes)) {
197             $this->contents[$products_id_string]['attributes'][$option] = $value;
198 // update database
hpdl
19
199             if ($osC_Customer->isLoggedOn()) {
200               $Qupdate = $osC_Database->query('update :table_customers_basket_attributes set products_options_value_id = :products_options_value_id where customers_id = :customers_id and products_id = :products_id and products_options_id = :products_options_id');
201               $Qupdate->bindTable(':table_customers_basket_attributes', TABLE_CUSTOMERS_BASKET_ATTRIBUTES);
202               $Qupdate->bindInt(':products_options_value_id', $value);
203               $Qupdate->bindInt(':customers_id', $osC_Customer->id);
204               $Qupdate->bindValue(':products_id', $products_id_string);
205               $Qupdate->bindInt(':products_options_id', $option);
206               $Qupdate->execute();
207             }
hpdl
1
208           }
209         }
210       }
211     }
212
213     function cleanup() {
hpdl
19
214       global $osC_Database, $osC_Customer;
hpdl
1
215
216       reset($this->contents);
217       while (list($key,) = each($this->contents)) {
218         if ($this->contents[$key]['qty'] < 1) {
219           unset($this->contents[$key]);
220 // remove from database
221           if ($osC_Customer->isLoggedOn()) {
hpdl
19
222             $Qdelete = $osC_Database->query('delete from :table_customers_basket where customers_id = :customers_id and products_id = :products_id');
223             $Qdelete->bindTable(':table_customers_basket', TABLE_CUSTOMERS_BASKET);
224             $Qdelete->bindInt(':customers_id', $osC_Customer->id);
225             $Qdelete->bindValue(':products_id', $key);
226             $Qdelete->execute();
227
228             $Qdelete = $osC_Database->query('delete from :table_customers_basket_attributes where customers_id = :customers_id and products_id = :products_id');
229             $Qdelete->bindTable(':table_customers_basket_attributes', TABLE_CUSTOMERS_BASKET_ATTRIBUTES);
230             $Qdelete->bindInt(':customers_id', $osC_Customer->id);
231             $Qdelete->bindValue(':products_id', $key);
232             $Qdelete->execute();
hpdl
1
233           }
234         }
235       }
236     }
237
238     function count_contents() {  // get total number of items in cart
239       $total_items = 0;
240       if (is_array($this->contents)) {
241         reset($this->contents);
242         while (list($products_id, ) = each($this->contents)) {
243           $total_items += $this->get_quantity($products_id);
244         }
245       }
246
247       return $total_items;
248     }
249
250     function get_quantity($products_id) {
251       if (isset($this->contents[$products_id])) {
252         return $this->contents[$products_id]['qty'];
253       } else {
254         return 0;
255       }
256     }
257
258     function in_cart($products_id) {
259       if (isset($this->contents[$products_id])) {
260         return true;
261       } else {
262         return false;
263       }
264     }
265
266     function remove($products_id) {
hpdl
19
267       global $osC_Database, $osC_Customer;
hpdl
1
268
269       unset($this->contents[$products_id]);
270 // remove from database
271       if ($osC_Customer->isLoggedOn()) {
hpdl
19
272         $Qdelete = $osC_Database->query('delete from :table_customers_basket where customers_id = :customers_id and products_id = :products_id');
273         $Qdelete->bindTable(':table_customers_basket', TABLE_CUSTOMERS_BASKET);
274         $Qdelete->bindInt(':customers_id', $osC_Customer->id);
275         $Qdelete->bindValue(':products_id', $products_id);
276         $Qdelete->execute();
277
278         $Qdelete = $osC_Database->query('delete from :table_customers_basket_attributes where customers_id = :customers_id and products_id = :products_id');
279         $Qdelete->bindTable(':table_customers_basket_attributes', TABLE_CUSTOMERS_BASKET_ATTRIBUTES);
280         $Qdelete->bindInt(':customers_id', $osC_Customer->id);
281         $Qdelete->bindValue(':products_id', $products_id);
282         $Qdelete->execute();
hpdl
1
283       }
284
285 // assign a temporary unique ID to the order contents to prevent hack attempts during the checkout procedure
286       $this->cartID = $this->generate_cart_id();
287     }
288
289     function remove_all() {
290       $this->reset();
291     }
292
293     function get_product_id_list() {
294       $product_id_list = '';
295       if (is_array($this->contents)) {
296         reset($this->contents);
297         while (list($products_id, ) = each($this->contents)) {
298           $product_id_list .= ', ' . $products_id;
299         }
300       }
301
302       return substr($product_id_list, 2);
303     }
304
305     function calculate() {
hpdl
19
306       global $osC_Database, $osC_Tax, $osC_Weight;
hpdl
1
307
308       $this->total = 0;
309       $this->weight = 0;
310       if (!is_array($this->contents)) return 0;
311
312       reset($this->contents);
313       while (list($products_id, ) = each($this->contents)) {
314         $qty = $this->contents[$products_id]['qty'];
315
316 // products price
hpdl
19
317         $Qproduct = $osC_Database->query('select products_id, products_price, products_tax_class_id, products_weight, products_weight_class from :table_products where products_id = :products_id');
318         $Qproduct->bindTable(':table_products', TABLE_PRODUCTS);
319         $Qproduct->bindInt(':products_id', $products_id);
320         $Qproduct->execute();
hpdl
1
321
hpdl
19
322         if ($Qproduct->numberOfRows()) {
323           $prid = $Qproduct->valueInt('products_id');
324           $products_tax = $osC_Tax->getTaxRate($Qproduct->valueInt('products_tax_class_id'));
325           $products_price = $Qproduct->value('products_price');
hpdl
1
326
hpdl
19
327           $products_weight = $osC_Weight->convert($Qproduct->value('products_weight'), $Qproduct->valueInt('products_weight_class'), SHIPPING_WEIGHT_UNIT);
328
329           $Qspecials = $osC_Database->query('select specials_new_products_price from :table_specials where products_id = :products_id and status = :status');
330           $Qspecials->bindTable(':table_specials', TABLE_SPECIALS);
331           $Qspecials->bindInt(':products_id', $prid);
332           $Qspecials->bindInt(':status', 1);
333           $Qspecials->execute();
334
335           if ($Qspecials->numberOfRows()) {
336             $products_price = $Qspecials->value('specials_new_products_price');
hpdl
1
337           }
338
339           $this->total += tep_add_tax($products_price, $products_tax) * $qty;
340           $this->weight += ($qty * $products_weight);
341         }
342
343 // attributes price
344         if (isset($this->contents[$products_id]['attributes'])) {
345           reset($this->contents[$products_id]['attributes']);
346           while (list($option, $value) = each($this->contents[$products_id]['attributes'])) {
hpdl
19
347             $Qattributes = $osC_Database->query('select options_values_price, price_prefix from :table_products_attributes where products_id = :products_id and options_id = :options_id and options_values_id = :options_values_id');
348             $Qattributes->bindTable(':table_products_attributes', TABLE_PRODUCTS_ATTRIBUTES);
349             $Qattributes->bindInt(':products_id', $prid);
350             $Qattributes->bindInt(':options_id', $option);
351             $Qattributes->bindInt(':options_values_id', $value);
352             $Qattributes->execute();
353
354             if ($Qattributes->value('price_prefix') == '+') {
355               $this->total += $qty * tep_add_tax($Qattributes->value('options_values_price'), $products_tax);
hpdl
1
356             } else {
hpdl
19
357               $this->total -= $qty * tep_add_tax($Qattributes->value('options_values_price'), $products_tax);
hpdl
1
358             }
359           }
360         }
361       }
362     }
363
364     function attributes_price($products_id) {
hpdl
19
365       global $osC_Database;
366
hpdl
1
367       $attributes_price = 0;
368
369       if (isset($this->contents[$products_id]['attributes'])) {
370         reset($this->contents[$products_id]['attributes']);
371         while (list($option, $value) = each($this->contents[$products_id]['attributes'])) {
hpdl
19
372           $Qattributes = $osC_Database->query('select options_values_price, price_prefix from :table_products_attributes where products_id = :products_id and options_id = :options_id and options_values_id = :options_values_id');
373           $Qattributes->bindTable(':table_products_attributes', TABLE_PRODUCTS_ATTRIBUTES);
374           $Qattributes->bindInt(':products_id', $products_id);
375           $Qattributes->bindInt(':options_id', $option);
376           $Qattributes->bindInt(':options_values_id', $value);
377           $Qattributes->execute();
378
379           if ($Qattributes->value('price_prefix') == '+') {
380             $attributes_price += $Qattributes->value('options_values_price');
hpdl
1
381           } else {
hpdl
19
382             $attributes_price -= $Qattributes->value('options_values_price');
hpdl
1
383           }
384         }
385       }
386
387       return $attributes_price;
388     }
389
390     function get_products() {
hpdl
19
391       global $osC_Database, $osC_Session;
hpdl
1
392
393       if (!is_array($this->contents)) return false;
394
395       $products_array = array();
396       reset($this->contents);
397       while (list($products_id, ) = each($this->contents)) {
hpdl
19
398         $Qproducts = $osC_Database->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 = :products_id and pd.products_id = p.products_id and pd.language_id = :language_id');
399         $Qproducts->bindTable(':table_products', TABLE_PRODUCTS);
400         $Qproducts->bindTable(':table_products_description', TABLE_PRODUCTS_DESCRIPTION);
401         $Qproducts->bindInt(':products_id', $products_id);
402         $Qproducts->bindInt(':language_id', $osC_Session->value('languages_id'));
403         $Qproducts->execute();
hpdl
1
404
hpdl
19
405         if ($Qproducts->numberOfRows()) {
406           $prid = $Qproducts->valueInt('products_id');
407           $products_price = $Qproducts->value('products_price');
408
409           $Qspecials = $osC_Database->query('select specials_new_products_price from :table_specials where products_id = :products_id and status = :status');
410           $Qspecials->bindTable(':table_specials', TABLE_SPECIALS);
411           $Qspecials->bindInt(':products_id', $prid);
412           $Qspecials->bindInt(':status', 1);
413           $Qspecials->execute();
414
415           if ($Qspecials->numberOfRows()) {
416             $products_price = $Qspecials->value('specials_new_products_price');
hpdl
1
417           }
418
419           $products_array[] = array('id' => $products_id,
hpdl
19
420                                     'name' => $Qproducts->value('products_name'),
421                                     'model' => $Qproducts->value('products_model'),
422                                     'image' => $Qproducts->value('products_image'),
hpdl
1
423                                     'price' => $products_price,
424                                     'quantity' => $this->contents[$products_id]['qty'],
hpdl
19
425                                     'weight' => $Qproducts->value('products_weight'),
hpdl
1
426                                     'final_price' => ($products_price + $this->attributes_price($products_id)),
hpdl
19
427                                     'tax_class_id' => $Qproducts->valueInt('products_tax_class_id'),
hpdl
1
428                                     'attributes' => (isset($this->contents[$products_id]['attributes']) ? $this->contents[$products_id]['attributes'] : ''));
429         }
430       }
431
432       return $products_array;
433     }
434
435     function show_total() {
436       $this->calculate();
437
438       return $this->total;
439     }
440
441     function show_weight() {
442       $this->calculate();
443
444       return $this->weight;
445     }
446
447     function generate_cart_id($length = 5) {
448       return tep_create_random_value($length, 'digits');
449     }
450
451     function get_content_type() {
hpdl
19
452       global $osC_Database;
453
hpdl
1
454       $this->content_type = false;
455
456       if ( (DOWNLOAD_ENABLED == 'true') && ($this->count_contents() > 0) ) {
457         reset($this->contents);
458         while (list($products_id, ) = each($this->contents)) {
459           if (isset($this->contents[$products_id]['attributes'])) {
460             reset($this->contents[$products_id]['attributes']);
461             while (list(, $value) = each($this->contents[$products_id]['attributes'])) {
hpdl
19
462               $Qcheck = $osC_Database->query('select count(*) as total from :table_products_attributes pa, :table_products_attributes_download pad where pa.products_id = :products_id and pa.options_values_id = :options_values_id and pa.products_attributes_id = pad.products_attributes_id');
463               $Qcheck->bindTable(':table_products_attributes', TABLE_PRODUCTS_ATTRIBUTES);
464               $Qcheck->bindTable(':table_products_attributes_download', TABLE_PRODUCTS_ATTRIBUTES_DOWNLOAD);
465               $Qcheck->bindInt(':products_id', $products_id);
466               $Qcheck->bindInt(':options_values_id', $value);
467               $Qcheck->execute();
hpdl
1
468
hpdl
19
469               if ($Qcheck->valueInt('total') > 0) {
hpdl
1
470                 switch ($this->content_type) {
471                   case 'physical':
472                     $this->content_type = 'mixed';
473
474                     return $this->content_type;
475                     break;
476                   default:
477                     $this->content_type = 'virtual';
478                     break;
479                 }
480               } else {
481                 switch ($this->content_type) {
482                   case 'virtual':
483                     $this->content_type = 'mixed';
484
485                     return $this->content_type;
486                     break;
487                   default:
488                     $this->content_type = 'physical';
489                     break;
490                 }
491               }
492             }
493           } else {
494             switch ($this->content_type) {
495               case 'virtual':
496                 $this->content_type = 'mixed';
497
498                 return $this->content_type;
499                 break;
500               default:
501                 $this->content_type = 'physical';
502                 break;
503             }
504           }
505         }
506       } else {
507         $this->content_type = 'physical';
508       }
509
510       return $this->content_type;
511     }
512   }
513 ?>