Quick Search:

View

Revision:

Diff

Diff from 1687 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 1687 2007-09-03 19:43:04Z hpdl $
hpdl
1
4
5   osCommerce, Open Source E-Commerce Solutions
6   http://www.oscommerce.com
7
hpdl
1420
8   Copyright (c) 2007 osCommerce
hpdl
1
9
hpdl
1497
10   This program is free software; you can redistribute it and/or modify
11   it under the terms of the GNU General Public License v2 (1991)
12   as published by the Free Software Foundation.
hpdl
1
13 */
14
hpdl
418
15   class osC_ShoppingCart {
hpdl
1674
16     private $_contents = array();
17     private $_sub_total = 0;
18     private $_total = 0;
19     private $_weight = 0;
20     private $_tax = 0;
21     private $_tax_groups = array();
22     private $_content_type;
23     private $_products_in_stock = true;
hpdl
1
24
hpdl
1674
25     public function __construct() {
26       if ( !isset($_SESSION['osC_ShoppingCart_data']) ) {
hpdl
418
27         $_SESSION['osC_ShoppingCart_data'] = array('contents' => array(),
hpdl
435
28                                                    'sub_total_cost' => 0,
hpdl
419
29                                                    'total_cost' => 0,
30                                                    'total_weight' => 0,
hpdl
435
31                                                    'tax' => 0,
32                                                    'tax_groups' => array(),
hpdl
439
33                                                    'shipping_boxes_weight' => 0,
34                                                    'shipping_boxes' => 1,
hpdl
1677
35                                                    'shipping_address' => array('zone_id' => STORE_ZONE,
36                                                                                'country_id' => STORE_COUNTRY),
hpdl
421
37                                                    'shipping_method' => array(),
hpdl
1677
38                                                    'billing_address' => array('zone_id' => STORE_ZONE,
39                                                                               'country_id' => STORE_COUNTRY),
hpdl
439
40                                                    'billing_method' => array(),
hpdl
1420
41                                                    'shipping_quotes' => array(),
hpdl
439
42                                                    'order_totals' => array());
hpdl
438
43
44         $this->resetShippingAddress();
45         $this->resetBillingAddress();
hpdl
418
46       }
47
hpdl
420
48       $this->_contents =& $_SESSION['osC_ShoppingCart_data']['contents'];
hpdl
435
49       $this->_sub_total =& $_SESSION['osC_ShoppingCart_data']['sub_total_cost'];
hpdl
420
50       $this->_total =& $_SESSION['osC_ShoppingCart_data']['total_cost'];
51       $this->_weight =& $_SESSION['osC_ShoppingCart_data']['total_weight'];
hpdl
435
52       $this->_tax =& $_SESSION['osC_ShoppingCart_data']['tax'];
53       $this->_tax_groups =& $_SESSION['osC_ShoppingCart_data']['tax_groups'];
hpdl
439
54       $this->_shipping_boxes_weight =& $_SESSION['osC_ShoppingCart_data']['shipping_boxes_weight'];
55       $this->_shipping_boxes =& $_SESSION['osC_ShoppingCart_data']['shipping_boxes'];
hpdl
421
56       $this->_shipping_address =& $_SESSION['osC_ShoppingCart_data']['shipping_address'];
57       $this->_shipping_method =& $_SESSION['osC_ShoppingCart_data']['shipping_method'];
58       $this->_billing_address =& $_SESSION['osC_ShoppingCart_data']['billing_address'];
59       $this->_billing_method =& $_SESSION['osC_ShoppingCart_data']['billing_method'];
hpdl
1420
60       $this->_shipping_quotes =& $_SESSION['osC_ShoppingCart_data']['shipping_quotes'];
hpdl
439
61       $this->_order_totals =& $_SESSION['osC_ShoppingCart_data']['order_totals'];
hpdl
1
62     }
63
hpdl
1677
64     public function refresh() {
hpdl
1420
65       if ( !isset($_SESSION['cartID']) ) {
66         $this->_calculate();
67       }
68     }
69
hpdl
1674
70     public function hasContents() {
hpdl
420
71       return !empty($this->_contents);
hpdl
418
72     }
73
hpdl
1674
74     public function synchronizeWithDatabase() {
75       global $osC_Database, $osC_Services, $osC_Language, $osC_Customer, $osC_Specials;
hpdl
1
76
hpdl
1674
77       if ( !$osC_Customer->isLoggedOn() ) {
hpdl
418
78         return false;
79       }
hpdl
1
80
hpdl
1677
81       foreach ( $this->_contents as $item_id => $data ) {
82         $db_action = 'check';
83
84         if ( isset($data['variants']) ) {
85           foreach ( $data['variants'] as $variant ) {
86             if ( $variant['has_custom_value'] === true ) {
87               $db_action = 'insert';
88
89               break;
90             }
91           }
92         }
93
94         if ( $db_action == 'check' ) {
95           $Qproduct = $osC_Database->query('select item_id, quantity from :table_shopping_carts where customers_id = :customers_id and products_id = :products_id');
96           $Qproduct->bindTable(':table_shopping_carts', TABLE_SHOPPING_CARTS);
hpdl
184
97           $Qproduct->bindInt(':customers_id', $osC_Customer->getID());
hpdl
1677
98           $Qproduct->bindInt(':products_id', $data['id']);
hpdl
19
99           $Qproduct->execute();
100
hpdl
1674
101           if ( $Qproduct->numberOfRows() > 0 ) {
hpdl
1677
102             $Qupdate = $osC_Database->query('update :table_shopping_carts set quantity = :quantity where customers_id = :customers_id and item_id = :item_id');
103             $Qupdate->bindTable(':table_shopping_carts', TABLE_SHOPPING_CARTS);
104             $Qupdate->bindInt(':quantity', $data['quantity'] + $Qproduct->valueInt('quantity'));
hpdl
418
105             $Qupdate->bindInt(':customers_id', $osC_Customer->getID());
hpdl
1677
106             $Qupdate->bindInt(':item_id', $Qproduct->valueInt('item_id'));
hpdl
418
107             $Qupdate->execute();
108           } else {
hpdl
1677
109             $db_action = 'insert';
hpdl
1
110           }
111         }
hpdl
1677
112
113         if ( $db_action == 'insert') {
114           $Qid = $osC_Database->query('select max(item_id) as item_id from :table_shopping_carts where customers_id = :customers_id');
115           $Qid->bindTable(':table_shopping_carts', TABLE_SHOPPING_CARTS);
116           $Qid->bindInt(':customers_id', $osC_Customer->getID());
117           $Qid->execute();
118
119           $db_item_id = $Qid->valueInt('item_id') + 1;
120
121           $Qnew = $osC_Database->query('insert into :table_shopping_carts (customers_id, item_id, products_id, quantity, date_added) values (:customers_id, :item_id, :products_id, :quantity, :date_added)');
122           $Qnew->bindTable(':table_shopping_carts', TABLE_SHOPPING_CARTS);
123           $Qnew->bindInt(':customers_id', $osC_Customer->getID());
124           $Qnew->bindInt(':item_id', $db_item_id);
125           $Qnew->bindInt(':products_id', $data['id']);
126           $Qnew->bindInt(':quantity', $data['quantity']);
127           $Qnew->bindRaw(':date_added', 'now()');
128           $Qnew->execute();
129
130           if ( isset($data['variants']) ) {
131             foreach ( $data['variants'] as $variant ) {
132               if ( $variant['has_custom_value'] === true ) {
133                 $Qnew = $osC_Database->query('insert into :table_shopping_carts_custom_variants_values (shopping_carts_item_id, customers_id, products_id, products_variants_values_id, products_variants_values_text) values (:shopping_carts_item_id, :customers_id, :products_id, :products_variants_values_id, :products_variants_values_text)');
134                 $Qnew->bindTable(':table_shopping_carts_custom_variants_values', TABLE_SHOPPING_CARTS_CUSTOM_VARIANTS_VALUES);
135                 $Qnew->bindInt(':shopping_carts_item_id', $db_item_id);
136                 $Qnew->bindInt(':customers_id', $osC_Customer->getID());
137                 $Qnew->bindInt(':products_id', $data['id']);
138                 $Qnew->bindInt(':products_variants_values_id', $variant['value_id']);
139                 $Qnew->bindValue(':products_variants_values_text', $variant['value_title']);
140                 $Qnew->execute();
141               }
142             }
143           }
144         }
hpdl
1
145       }
146
147 // reset per-session cart contents, but not the database contents
hpdl
418
148       $this->reset();
hpdl
1
149
hpdl
1674
150       $_delete_array = array();
151
hpdl
1677
152       $Qproducts = $osC_Database->query('select sc.item_id, sc.products_id, sc.quantity, sc.date_added, p.parent_id, p.products_price, p.products_tax_class_id, p.products_weight, p.products_weight_class, p.products_status from :table_shopping_carts sc, :table_products p where sc.customers_id = :customers_id and sc.products_id = p.products_id order by sc.date_added desc');
153       $Qproducts->bindTable(':table_shopping_carts', TABLE_SHOPPING_CARTS);
hpdl
418
154       $Qproducts->bindTable(':table_products', TABLE_PRODUCTS);
hpdl
184
155       $Qproducts->bindInt(':customers_id', $osC_Customer->getID());
hpdl
19
156       $Qproducts->execute();
157
hpdl
1674
158       while ( $Qproducts->next() ) {
159         if ( $Qproducts->valueInt('products_status') === 1 ) {
160           $Qdesc = $osC_Database->query('select products_name, products_keyword from :table_products_description where products_id = :products_id and language_id = :language_id');
161           $Qdesc->bindTable(':table_products_description', TABLE_PRODUCTS_DESCRIPTION);
162           $Qdesc->bindInt(':products_id', ($Qproducts->valueInt('parent_id') > 0) ? $Qproducts->valueInt('parent_id') : $Qproducts->valueInt('products_id'));
163           $Qdesc->bindInt(':language_id', $osC_Language->getID());
164           $Qdesc->execute();
hpdl
809
165
hpdl
1674
166           $Qimage = $osC_Database->query('select image from :table_products_images where products_id = :products_id and i.default_flag = :default_flag');
167           $Qimage->bindTable(':table_products_images', TABLE_PRODUCTS_IMAGES);
168           $Qimage->bindInt(':products_id', ($Qproducts->valueInt('parent_id') > 0) ? $Qproducts->valueInt('parent_id') : $Qproducts->valueInt('products_id'));
169           $Qimage->bindInt(':default_flag', 1);
170           $Qimage->execute();
hpdl
809
171
hpdl
1674
172           $price = $Qproducts->value('products_price');
hpdl
809
173
hpdl
1674
174           if ( $osC_Services->isStarted('specials') ) {
175             if ( $new_price = $osC_Specials->getPrice($Qproducts->valueInt('products_id')) ) {
176               $price = $new_price;
hpdl
809
177             }
178           }
179
hpdl
1677
180           $this->_contents[$Qproducts->valueInt('item_id')] = array('item_id' => $Qproducts->valueInt('item_id'),
181                                                                     'id' => $Qproducts->valueInt('products_id'),
182                                                                     'parent_id' => $Qproducts->valueInt('parent_id'),
183                                                                     'name' => $Qdesc->value('products_name'),
184                                                                     'keyword' => $Qdesc->value('products_keyword'),
185                                                                     'image' => ($Qimage->numberOfRows() === 1) ? $Qimage->value('image') : '',
186                                                                     'price' => $price,
187                                                                     'quantity' => $Qproducts->valueInt('quantity'),
188                                                                     'weight' => $Qproducts->value('products_weight'),
189                                                                     'tax_class_id' => $Qproducts->valueInt('products_tax_class_id'),
190                                                                     'date_added' => osC_DateTime::getShort($Qproducts->value('date_added')),
191                                                                     'weight_class_id' => $Qproducts->valueInt('products_weight_class'));
hpdl
418
192
hpdl
1674
193           if ( $Qproducts->valueInt('parent_id') > 0 ) {
194             $Qcheck = $osC_Database->query('select products_status from :table_products where products_id = :products_id');
195             $Qcheck->bindTable(':table_products', TABLE_PRODUCTS);
196             $Qcheck->bindInt(':products_id', $Qproducts->valueInt('parent_id'));
197             $Qcheck->execute();
hpdl
418
198
hpdl
1674
199             if ( $Qcheck->valueInt('products_status') === 1 ) {
hpdl
1680
200               $Qvariant = $osC_Database->query('select pvg.id as group_id, pvg.title as group_title, pvg.module, pvv.id as value_id, pvv.title as value_title from :table_products_variants pv, :table_products_variants_values pvv, :table_products_variants_groups pvg where pv.products_id = :products_id and pv.products_variants_values_id = pvv.id and pvv.languages_id = :languages_id and pvv.products_variants_groups_id = pvg.id and pvg.languages_id = :languages_id');
hpdl
1674
201               $Qvariant->bindTable(':table_products_variants', TABLE_PRODUCTS_VARIANTS);
202               $Qvariant->bindTable(':table_products_variants_values', TABLE_PRODUCTS_VARIANTS_VALUES);
203               $Qvariant->bindTable(':table_products_variants_groups', TABLE_PRODUCTS_VARIANTS_GROUPS);
204               $Qvariant->bindInt(':products_id', $Qproducts->valueInt('products_id'));
205               $Qvariant->bindInt(':languages_id', $osC_Language->getID());
206               $Qvariant->bindInt(':languages_id', $osC_Language->getID());
207               $Qvariant->execute();
hpdl
418
208
hpdl
1677
209               if ( $Qvariant->numberOfRows() > 0 ) {
hpdl
1674
210                 while ( $Qvariant->next() ) {
hpdl
1677
211                   $group_title = osC_Variants::getGroupTitle($Qvariant->value('module'), $Qvariant->toArray());
212                   $value_title = $Qvariant->value('value_title');
213                   $has_custom_value = false;
214
215                   $Qcvv = $osC_Database->query('select products_variants_values_text from :table_shopping_carts_custom_variants_values where customers_id = :customers_id and shopping_carts_item_id = :shopping_carts_item_id and products_id = :products_id and products_variants_values_id = :products_variants_values_id');
216                   $Qcvv->bindTable(':table_shopping_carts_custom_variants_values', TABLE_SHOPPING_CARTS_CUSTOM_VARIANTS_VALUES);
217                   $Qcvv->bindInt(':customers_id', $osC_Customer->getID());
218                   $Qcvv->bindInt(':shopping_carts_item_id', $Qproducts->valueInt('item_id'));
219                   $Qcvv->bindInt(':products_id', $Qproducts->valueInt('products_id'));
220                   $Qcvv->bindInt(':products_variants_values_id', $Qvariant->valueInt('value_id'));
221                   $Qcvv->execute();
222
223                   if ( $Qcvv->numberOfRows() === 1 ) {
224                     $value_title = $Qcvv->value('products_variants_values_text');
225                     $has_custom_value = true;
226                   }
227
228                   $this->_contents[$Qproducts->valueInt('item_id')]['variants'][] = array('group_id' => $Qvariant->valueInt('group_id'),
229                                                                                           'value_id' => $Qvariant->valueInt('value_id'),
230                                                                                           'group_title' => $group_title,
231                                                                                           'value_title' => $value_title,
232                                                                                           'has_custom_value' => $has_custom_value);
hpdl
1674
233                 }
hpdl
809
234               } else {
hpdl
1677
235                 $_delete_array[] = $Qproducts->valueInt('item_id');
hpdl
809
236               }
hpdl
810
237             } else {
hpdl
1677
238               $_delete_array[] = $Qproducts->valueInt('item_id');
hpdl
809
239             }
hpdl
420
240           }
hpdl
1674
241         } else {
hpdl
1677
242           $_delete_array[] = $Qproducts->valueInt('item_id');
hpdl
1
243         }
244       }
245
hpdl
1674
246       if ( !empty($_delete_array) ) {
hpdl
1677
247         foreach ( $_delete_array as $id ) {
248           unset($this->_contents[$id]);
hpdl
1674
249         }
250
hpdl
1677
251         $Qdelete = $osC_Database->query('delete from :table_shopping_carts where customers_id = :customers_id and item_id in (":item_id")');
252         $Qdelete->bindTable(':table_shopping_carts', TABLE_SHOPPING_CARTS);
hpdl
1674
253         $Qdelete->bindInt(':customers_id', $osC_Customer->getID());
hpdl
1677
254         $Qdelete->bindRaw(':item_id', implode('", "', $_delete_array));
hpdl
1674
255         $Qdelete->execute();
hpdl
1677
256
257         $Qdelete = $osC_Database->query('delete from :table_shopping_carts_custom_variants_values where customers_id = :customers_id and shopping_carts_item_id in (":shopping_carts_item_id")');
258         $Qdelete->bindTable(':table_shopping_carts_custom_variants_values', TABLE_SHOPPING_CARTS_CUSTOM_VARIANTS_VALUES);
259         $Qdelete->bindInt(':customers_id', $osC_Customer->getID());
260         $Qdelete->bindRaw(':shopping_carts_item_id', implode('", "', $_delete_array));
261         $Qdelete->execute();
hpdl
1674
262       }
263
hpdl
418
264       $this->_cleanUp();
hpdl
419
265       $this->_calculate();
hpdl
1
266     }
267
hpdl
1674
268     public function reset($reset_database = false) {
hpdl
199
269       global $osC_Database, $osC_Customer;
hpdl
1
270
hpdl
1674
271       if ( ($reset_database === true) && $osC_Customer->isLoggedOn() ) {
hpdl
1677
272         $Qdelete = $osC_Database->query('delete from :table_shopping_carts where customers_id = :customers_id');
273         $Qdelete->bindTable(':table_shopping_carts', TABLE_SHOPPING_CARTS);
hpdl
184
274         $Qdelete->bindInt(':customers_id', $osC_Customer->getID());
hpdl
19
275         $Qdelete->execute();
hpdl
1677
276
277         $Qdelete = $osC_Database->query('delete from :table_shopping_carts_custom_variants_values where customers_id = :customers_id');
278         $Qdelete->bindTable(':table_shopping_carts_custom_variants_values', TABLE_SHOPPING_CARTS_CUSTOM_VARIANTS_VALUES);
279         $Qdelete->bindInt(':customers_id', $osC_Customer->getID());
280         $Qdelete->execute();
hpdl
418
281       }
hpdl
19
282
hpdl
420
283       $this->_contents = array();
hpdl
435
284       $this->_sub_total = 0;
hpdl
420
285       $this->_total = 0;
286       $this->_weight = 0;
hpdl
435
287       $this->_tax = 0;
288       $this->_tax_groups = array();
hpdl
420
289       $this->_content_type = null;
hpdl
439
290
hpdl
438
291       $this->resetShippingAddress();
hpdl
439
292       $this->resetShippingMethod();
hpdl
438
293       $this->resetBillingAddress();
hpdl
439
294       $this->resetBillingMethod();
hpdl
1420
295
296       if ( isset($_SESSION['cartID']) ) {
297         unset($_SESSION['cartID']);
298       }
hpdl
1
299     }
300
hpdl
1674
301     public function add($product_id, $quantity = null) {
302       global $osC_Database, $osC_Services, $osC_Language, $osC_Customer;
hpdl
1
303
hpdl
1687
304       if ( !is_numeric($product_id) ) {
305         return false;
306       }
hpdl
1
307
hpdl
1687
308       $Qproduct = $osC_Database->query('select p.parent_id, p.products_price, p.products_tax_class_id, p.products_weight, p.products_weight_class, p.products_status, i.image from :table_products p left join :table_products_images i on (p.products_id = i.products_id and i.default_flag = :default_flag) where p.products_id = :products_id');
309       $Qproduct->bindTable(':table_products', TABLE_PRODUCTS);
310       $Qproduct->bindTable(':table_products_images', TABLE_PRODUCTS_IMAGES);
311       $Qproduct->bindInt(':default_flag', 1);
312       $Qproduct->bindInt(':products_id', $product_id);
313       $Qproduct->execute();
hpdl
1677
314
hpdl
1687
315       if ( $Qproduct->valueInt('products_status') === 1 ) {
316         if ( $this->exists($product_id) ) {
317           $item_id = $this->getBasketID($product_id);
hpdl
418
318
hpdl
1687
319           if ( !is_numeric($quantity) ) {
320             $quantity = $this->getQuantity($item_id) + 1;
321           }
hpdl
418
322
hpdl
1687
323           $this->_contents[$item_id]['quantity'] = $quantity;
hpdl
418
324
hpdl
1687
325           if ( $osC_Customer->isLoggedOn() ) {
326             $Qupdate = $osC_Database->query('update :table_shopping_carts set quantity = :quantity where customers_id = :customers_id and item_id = :item_id');
327             $Qupdate->bindTable(':table_shopping_carts', TABLE_SHOPPING_CARTS);
328             $Qupdate->bindInt(':quantity', $quantity);
329             $Qupdate->bindInt(':customers_id', $osC_Customer->getID());
330             $Qupdate->bindInt(':item_id', $item_id);
331             $Qupdate->execute();
332           }
333         } else {
334           if ( !is_numeric($quantity) ) {
335             $quantity = 1;
336           }
hpdl
418
337
hpdl
1687
338           $Qdescription = $osC_Database->query('select products_name, products_keyword from :table_products_description where products_id = :products_id and language_id = :language_id');
339           $Qdescription->bindTable(':table_products_description', TABLE_PRODUCTS_DESCRIPTION);
340           $Qdescription->bindInt(':products_id', ($Qproduct->valueInt('parent_id') > 0) ? $Qproduct->valueInt('parent_id') : $product_id);
341           $Qdescription->bindInt(':language_id', $osC_Language->getID());
342           $Qdescription->execute();
hpdl
420
343
hpdl
1687
344           $price = $Qproduct->value('products_price');
hpdl
418
345
hpdl
1687
346           if ( $osC_Services->isStarted('specials') ) {
347             global $osC_Specials;
348
349             if ( $new_price = $osC_Specials->getPrice($product_id) ) {
350               $price = $new_price;
hpdl
418
351             }
hpdl
1687
352           }
hpdl
418
353
hpdl
1687
354           if ( $osC_Customer->isLoggedOn() ) {
355             $Qid = $osC_Database->query('select max(item_id) as item_id from :table_shopping_carts where customers_id = :customers_id');
356             $Qid->bindTable(':table_shopping_carts', TABLE_SHOPPING_CARTS);
357             $Qid->bindInt(':customers_id', $osC_Customer->getID());
358             $Qid->execute();
hpdl
420
359
hpdl
1687
360             $item_id = $Qid->valueInt('item_id') + 1;
361           } else {
362             $item_id = max(array_keys($this->_contents)) + 1;
363           }
hpdl
1677
364
hpdl
1687
365           $this->_contents[$item_id] = array('item_id' => $item_id,
366                                              'id' => $product_id,
367                                              'parent_id' => $Qproduct->valueInt('parent_id'),
368                                              'name' => $Qdescription->value('products_name'),
369                                              'keyword' => $Qdescription->value('products_keyword'),
370                                              'image' => $Qproduct->value('image'),
371                                              'price' => $price,
372                                              'quantity' => $quantity,
373                                              'weight' => $Qproduct->value('products_weight'),
374                                              'tax_class_id' => $Qproduct->valueInt('products_tax_class_id'),
375                                              'date_added' => osC_DateTime::getShort(osC_DateTime::getNow()),
376                                              'weight_class_id' => $Qproduct->valueInt('products_weight_class'));
hpdl
1677
377
hpdl
1687
378           if ( $osC_Customer->isLoggedOn() ) {
379             $Qnew = $osC_Database->query('insert into :table_shopping_carts (customers_id, item_id, products_id, quantity, date_added) values (:customers_id, :item_id, :products_id, :quantity, :date_added)');
380             $Qnew->bindTable(':table_shopping_carts', TABLE_SHOPPING_CARTS);
381             $Qnew->bindInt(':customers_id', $osC_Customer->getID());
382             $Qnew->bindInt(':item_id', $item_id);
383             $Qnew->bindInt(':products_id', $product_id);
384             $Qnew->bindInt(':quantity', $quantity);
385             $Qnew->bindRaw(':date_added', 'now()');
386             $Qnew->execute();
387           }
hpdl
1
388
hpdl
1687
389           if ( $Qproduct->valueInt('parent_id') > 0 ) {
390             $Qvariant = $osC_Database->query('select pvg.id as group_id, pvg.title as group_title, pvg.module, pvv.id as value_id, pvv.title as value_title from :table_products_variants pv, :table_products_variants_values pvv, :table_products_variants_groups pvg where pv.products_id = :products_id and pv.products_variants_values_id = pvv.id and pvv.languages_id = :languages_id and pvv.products_variants_groups_id = pvg.id and pvg.languages_id = :languages_id');
391             $Qvariant->bindTable(':table_products_variants', TABLE_PRODUCTS_VARIANTS);
392             $Qvariant->bindTable(':table_products_variants_values', TABLE_PRODUCTS_VARIANTS_VALUES);
393             $Qvariant->bindTable(':table_products_variants_groups', TABLE_PRODUCTS_VARIANTS_GROUPS);
394             $Qvariant->bindInt(':products_id', $product_id);
395             $Qvariant->bindInt(':languages_id', $osC_Language->getID());
396             $Qvariant->bindInt(':languages_id', $osC_Language->getID());
397             $Qvariant->execute();
hpdl
418
398
hpdl
1687
399             while ( $Qvariant->next() ) {
400               $group_title = osC_Variants::getGroupTitle($Qvariant->value('module'), $Qvariant->toArray());
401               $value_title = osC_Variants::getValueTitle($Qvariant->value('module'), $Qvariant->toArray());
402               $has_custom_value = osC_Variants::hasCustomValue($Qvariant->value('module'));
hpdl
1677
403
hpdl
1687
404               $this->_contents[$item_id]['variants'][] = array('group_id' => $Qvariant->valueInt('group_id'),
405                                                                'value_id' => $Qvariant->valueInt('value_id'),
406                                                                'group_title' => $group_title,
407                                                                'value_title' => $value_title,
408                                                                'has_custom_value' => $has_custom_value);
hpdl
1677
409
hpdl
1687
410               if ( $osC_Customer->isLoggedOn() && ($has_custom_value === true) ) {
411                 $Qnew = $osC_Database->query('insert into :table_shopping_carts_custom_variants_values (shopping_carts_item_id, customers_id, products_id, products_variants_values_id, products_variants_values_text) values (:shopping_carts_item_id, :customers_id, :products_id, :products_variants_values_id, :products_variants_values_text)');
412                 $Qnew->bindTable(':table_shopping_carts_custom_variants_values', TABLE_SHOPPING_CARTS_CUSTOM_VARIANTS_VALUES);
413                 $Qnew->bindInt(':shopping_carts_item_id', $item_id);
414                 $Qnew->bindInt(':customers_id', $osC_Customer->getID());
415                 $Qnew->bindInt(':products_id', $product_id);
416                 $Qnew->bindInt(':products_variants_values_id', $Qvariant->valueInt('value_id'));
417                 $Qnew->bindValue(':products_variants_values_text', $value_title);
418                 $Qnew->execute();
hpdl
1
419               }
420             }
421           }
hpdl
1687
422         }
hpdl
1
423
hpdl
1687
424         $this->_cleanUp();
425         $this->_calculate();
hpdl
1
426       }
427     }
428
hpdl
1674
429     public function numberOfItems() {
hpdl
418
430       $total = 0;
hpdl
1
431
hpdl
1677
432       foreach ( $this->_contents as $product ) {
433         $total += $product['quantity'];
hpdl
1
434       }
435
hpdl
418
436       return $total;
hpdl
1
437     }
438
hpdl
1677
439     public function getBasketID($product_id) {
440       foreach ( $this->_contents as $item_id => $product ) {
441         if ( $product['id'] === $product_id ) {
442           return $item_id;
443         }
444       }
hpdl
1
445     }
446
hpdl
1677
447     public function getQuantity($item_id) {
448       return ( isset($this->_contents[$item_id]) ) ? $this->_contents[$item_id]['quantity'] : 0;
449     }
450
hpdl
1674
451     public function exists($product_id) {
hpdl
1677
452       foreach ( $this->_contents as $product ) {
453         if ( $product['id'] === $product_id ) {
454           if ( isset($product['variants']) ) {
455             foreach ( $product['variants'] as $variant ) {
456               if ( $variant['has_custom_value'] === true ) {
457                 return false;
458               }
459             }
460           }
461
462           return true;
463         }
464       }
465
466       return false;
hpdl
1
467     }
468
hpdl
1677
469     public function update($item_id, $quantity) {
hpdl
19
470       global $osC_Database, $osC_Customer;
hpdl
1
471
hpdl
1677
472       if ( !is_numeric($quantity) ) {
473         $quantity = $this->getQuantity($item_id) + 1;
474       }
hpdl
418
475
hpdl
1677
476       $this->_contents[$item_id]['quantity'] = $quantity;
477
hpdl
1674
478       if ( $osC_Customer->isLoggedOn() ) {
hpdl
1677
479         $Qupdate = $osC_Database->query('update :table_shopping_carts set quantity = :quantity where customers_id = :customers_id and item_id = :item_id');
480         $Qupdate->bindTable(':table_shopping_carts', TABLE_SHOPPING_CARTS);
481         $Qupdate->bindInt(':quantity', $quantity);
482         $Qupdate->bindInt(':customers_id', $osC_Customer->getID());
483         $Qupdate->bindInt(':item_id', $item_id);
484         $Qupdate->execute();
485       }
486
487       $this->_cleanUp();
488       $this->_calculate();
489     }
490
491     public function remove($item_id) {
492       global $osC_Database, $osC_Customer;
493
494       unset($this->_contents[$item_id]);
495
496       if ( $osC_Customer->isLoggedOn() ) {
497         $Qdelete = $osC_Database->query('delete from :table_shopping_carts where customers_id = :customers_id and item_id = :item_id');
498         $Qdelete->bindTable(':table_shopping_carts', TABLE_SHOPPING_CARTS);
hpdl
184
499         $Qdelete->bindInt(':customers_id', $osC_Customer->getID());
hpdl
1677
500         $Qdelete->bindInt(':item_id', $item_id);
hpdl
19
501         $Qdelete->execute();
hpdl
1677
502
503         $Qdelete = $osC_Database->query('delete from :table_shopping_carts_custom_variants_values where customers_id = :customers_id and shopping_carts_item_id = :shopping_carts_item_id');
504         $Qdelete->bindTable(':table_shopping_carts_custom_variants_values', TABLE_SHOPPING_CARTS_CUSTOM_VARIANTS_VALUES);
505         $Qdelete->bindInt(':customers_id', $osC_Customer->getID());
506         $Qdelete->bindInt(':shopping_carts_item_id', $item_id);
507         $Qdelete->execute();
hpdl
1
508       }
509
hpdl
419
510       $this->_calculate();
hpdl
1
511     }
512
hpdl
1674
513     public function getProducts() {
hpdl
420
514       static $_is_sorted = false;
hpdl
19
515
hpdl
1674
516       if ( $_is_sorted === false ) {
hpdl
420
517         $_is_sorted = true;
hpdl
1
518
hpdl
420
519         uasort($this->_contents, array('osC_ShoppingCart', '_uasortProductsByDateAdded'));
hpdl
1
520       }
521
hpdl
420
522       return $this->_contents;
hpdl
1
523     }
524
hpdl
1674
525     public function getSubTotal() {
hpdl
435
526       return $this->_sub_total;
527     }
528
hpdl
1674
529     public function getTotal() {
hpdl
420
530       return $this->_total;
531     }
hpdl
1
532
hpdl
1674
533     public function getWeight() {
hpdl
420
534       return $this->_weight;
hpdl
1
535     }
536
hpdl
1674
537     public function generateCartID($length = 5) {
hpdl
734
538       return osc_create_random_string($length, 'digits');
hpdl
1
539     }
540
hpdl
1674
541     public function getCartID() {
hpdl
1420
542       return $_SESSION['cartID'];
hpdl
1
543     }
544
hpdl
1674
545     public function getContentType() {
hpdl
19
546       global $osC_Database;
547
hpdl
420
548       $this->_content_type = 'physical';
hpdl
1
549
hpdl
500
550       if ( (DOWNLOAD_ENABLED == '1') && $this->hasContents() ) {
hpdl
1674
551         foreach ( $this->_contents as $product_id => $data ) {
552 /* HPDL
hpdl
418
553           if (isset($data['attributes'])) {
554             foreach ($data['attributes'] as $value) {
hpdl
19
555               $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');
556               $Qcheck->bindTable(':table_products_attributes', TABLE_PRODUCTS_ATTRIBUTES);
557               $Qcheck->bindTable(':table_products_attributes_download', TABLE_PRODUCTS_ATTRIBUTES_DOWNLOAD);
558               $Qcheck->bindInt(':products_id', $products_id);
hpdl
420
559               $Qcheck->bindInt(':options_values_id', $value['options_values_id']);
hpdl
19
560               $Qcheck->execute();
hpdl
1
561
hpdl
19
562               if ($Qcheck->valueInt('total') > 0) {
hpdl
420
563                 switch ($this->_content_type) {
hpdl
1
564                   case 'physical':
hpdl
420
565                     $this->_content_type = 'mixed';
hpdl
1
566
hpdl
420
567                     return $this->_content_type;
hpdl
1
568                     break;
569                   default:
hpdl
420
570                     $this->_content_type = 'virtual';
hpdl
1
571                     break;
572                 }
573               } else {
hpdl
420
574                 switch ($this->_content_type) {
hpdl
1
575                   case 'virtual':
hpdl
420
576                     $this->_content_type = 'mixed';
hpdl
1
577
hpdl
420
578                     return $this->_content_type;
hpdl
1
579                     break;
580                   default:
hpdl
420
581                     $this->_content_type = 'physical';
hpdl
1
582                     break;
583                 }
584               }
585             }
586           } else {
hpdl
1674
587 */
588             switch ( $this->_content_type ) {
hpdl
1
589               case 'virtual':
hpdl
420
590                 $this->_content_type = 'mixed';
hpdl
1
591
hpdl
1674
592                 break 2;
593
hpdl
1
594               default:
hpdl
420
595                 $this->_content_type = 'physical';
hpdl
1674
596
hpdl
1
597                 break;
598             }
hpdl
1674
599 //          }
hpdl
1
600         }
601       }
602
hpdl
420
603       return $this->_content_type;
hpdl
1
604     }
hpdl
418
605
hpdl
1677
606     public function isVariant($item_id) {
607       return isset($this->_contents[$item_id]['variants']) && !empty($this->_contents[$item_id]['variants']);
hpdl
420
608     }
609
hpdl
1677
610     public function getVariant($item_id) {
611       if ( isset($this->_contents[$item_id]['variants']) && !empty($this->_contents[$item_id]['variants']) ) {
612         return $this->_contents[$item_id]['variants'];
hpdl
420
613       }
614     }
615
hpdl
1677
616     public function isInStock($item_id) {
hpdl
420
617       global $osC_Database;
618
619       $Qstock = $osC_Database->query('select products_quantity from :table_products where products_id = :products_id');
620       $Qstock->bindTable(':table_products', TABLE_PRODUCTS);
hpdl
1677
621       $Qstock->bindInt(':products_id', $this->_contents[$item_id]['id']);
hpdl
420
622       $Qstock->execute();
623
hpdl
1677
624       if ( ($Qstock->valueInt('products_quantity') - $this->_contents[$item_id]['quantity']) > 0 ) {
hpdl
420
625         return true;
hpdl
1674
626       } elseif ( $this->_products_in_stock === true ) {
hpdl
420
627         $this->_products_in_stock = false;
628       }
629
630       return false;
631     }
632
hpdl
1674
633     public function hasStock() {
hpdl
420
634       return $this->_products_in_stock;
635     }
636
hpdl
1674
637     public function hasShippingAddress() {
638       return isset($this->_shipping_address['id']);
hpdl
421
639     }
640
hpdl
1674
641     public function setShippingAddress($address_id) {
hpdl
421
642       global $osC_Database, $osC_Customer;
643
hpdl
1674
644       $previous_address = null;
hpdl
439
645
hpdl
1674
646       if ( isset($this->_shipping_address['id']) ) {
hpdl
439
647         $previous_address = $this->getShippingAddress();
648       }
649
hpdl
732
650       $Qaddress = $osC_Database->query('select ab.entry_firstname, ab.entry_lastname, ab.entry_company, ab.entry_street_address, ab.entry_suburb, ab.entry_postcode, ab.entry_city, ab.entry_zone_id, ab.entry_telephone, z.zone_code, z.zone_name, ab.entry_country_id, c.countries_name, c.countries_iso_code_2, c.countries_iso_code_3, c.address_format, ab.entry_state from :table_address_book ab left join :table_zones z on (ab.entry_zone_id = z.zone_id) left join :table_countries c on (ab.entry_country_id = c.countries_id) where ab.customers_id = :customers_id and ab.address_book_id = :address_book_id');
hpdl
425
651       $Qaddress->bindTable(':table_address_book', TABLE_ADDRESS_BOOK);
652       $Qaddress->bindTable(':table_zones', TABLE_ZONES);
653       $Qaddress->bindTable(':table_countries', TABLE_COUNTRIES);
654       $Qaddress->bindInt(':customers_id', $osC_Customer->getID());
655       $Qaddress->bindInt(':address_book_id', $address_id);
656       $Qaddress->execute();
hpdl
421
657
hpdl
1674
658       if ( $Qaddress->numberOfRows() === 1 ) {
659         $this->_shipping_address = array('id' => $address_id,
660                                          'firstname' => $Qaddress->valueProtected('entry_firstname'),
661                                          'lastname' => $Qaddress->valueProtected('entry_lastname'),
662                                          'company' => $Qaddress->valueProtected('entry_company'),
663                                          'street_address' => $Qaddress->valueProtected('entry_street_address'),
664                                          'suburb' => $Qaddress->valueProtected('entry_suburb'),
665                                          'city' => $Qaddress->valueProtected('entry_city'),
666                                          'postcode' => $Qaddress->valueProtected('entry_postcode'),
667                                          'state' => (!osc_empty($Qaddress->valueProtected('entry_state'))) ? $Qaddress->valueProtected('entry_state') : $Qaddress->valueProtected('zone_name'),
668                                          'zone_id' => $Qaddress->valueInt('entry_zone_id'),
669                                          'zone_code' => $Qaddress->value('zone_code'),
670                                          'country_id' => $Qaddress->valueInt('entry_country_id'),
671                                          'country_title' => $Qaddress->value('countries_name'),
672                                          'country_iso_code_2' => $Qaddress->value('countries_iso_code_2'),
673                                          'country_iso_code_3' => $Qaddress->value('countries_iso_code_3'),
674                                          'format' => $Qaddress->value('address_format'),
675                                          'telephone_number' => $Qaddress->value('entry_telephone'));
hpdl
439
676
hpdl
1674
677         if ( is_array($previous_address) && ( ($previous_address['id'] != $this->_shipping_address['id']) || ($previous_address['country_id'] != $this->_shipping_address['country_id']) || ($previous_address['zone_id'] != $this->_shipping_address['zone_id']) || ($previous_address['state'] != $this->_shipping_address['state']) || ($previous_address['postcode'] != $this->_shipping_address['postcode']) ) ) {
678           $this->_calculate();
679         }
hpdl
439
680       }
hpdl
421
681     }
682
hpdl
1674
683     public function getShippingAddress($key = null) {
684       if ( empty($key) ) {
hpdl
421
685         return $this->_shipping_address;
686       }
687
688       return $this->_shipping_address[$key];
689     }
690
hpdl
1674
691     public function resetShippingAddress() {
hpdl
438
692       global $osC_Customer;
693
hpdl
1674
694       $this->_shipping_address = array('zone_id' => STORE_ZONE,
695                                        'country_id' => STORE_COUNTRY);
hpdl
438
696
hpdl
1674
697       if ( $osC_Customer->isLoggedOn() && $osC_Customer->hasDefaultAddress() ) {
hpdl
438
698         $this->setShippingAddress($osC_Customer->getDefaultAddressID());
699       }
hpdl
422
700     }
701
hpdl
1674
702     public function setShippingMethod($shipping_array, $calculate_total = true) {
hpdl
421
703       $this->_shipping_method = $shipping_array;
hpdl
439
704
hpdl
1674
705       if ( $calculate_total === true ) {
hpdl
439
706         $this->_calculate(false);
707       }
hpdl
421
708     }
709
hpdl
1674
710     public function getShippingMethod($key = null) {
711       if ( empty($key) ) {
hpdl
421
712         return $this->_shipping_method;
713       }
714
715       return $this->_shipping_method[$key];
716     }
717
hpdl
1674
718     public function resetShippingMethod() {
hpdl
421
719       $this->_shipping_method = array();
hpdl
438
720
hpdl
439
721       $this->_calculate();
hpdl
421
722     }
723
hpdl
1674
724     public function hasShippingMethod() {
hpdl
421
725       return !empty($this->_shipping_method);
726     }
727
hpdl
1674
728     public function hasBillingAddress() {
729       return isset($this->_billing_address['id']);
hpdl
421
730     }
731
hpdl
1674
732     public function setBillingAddress($address_id) {
hpdl
421
733       global $osC_Database, $osC_Customer;
734
hpdl
439
735       $previous_address = false;
736
hpdl
1674
737       if ( isset($this->_billing_address['id']) ) {
hpdl
439
738         $previous_address = $this->getBillingAddress();
739       }
740
hpdl
732
741       $Qaddress = $osC_Database->query('select ab.entry_firstname, ab.entry_lastname, ab.entry_company, ab.entry_street_address, ab.entry_suburb, ab.entry_postcode, ab.entry_city, ab.entry_zone_id, ab.entry_telephone, z.zone_code, z.zone_name, ab.entry_country_id, c.countries_name, c.countries_iso_code_2, c.countries_iso_code_3, c.address_format, ab.entry_state from :table_address_book ab left join :table_zones z on (ab.entry_zone_id = z.zone_id) left join :table_countries c on (ab.entry_country_id = c.countries_id) where ab.customers_id = :customers_id and ab.address_book_id = :address_book_id');
hpdl
421
742       $Qaddress->bindTable(':table_address_book', TABLE_ADDRESS_BOOK);
743       $Qaddress->bindTable(':table_zones', TABLE_ZONES);
744       $Qaddress->bindTable(':table_countries', TABLE_COUNTRIES);
745       $Qaddress->bindInt(':customers_id', $osC_Customer->getID());
746       $Qaddress->bindInt(':address_book_id', $address_id);
747       $Qaddress->execute();
748
hpdl
1674
749       if ( $Qaddress->numberOfRows() === 1 ) {
750         $this->_billing_address = array('id' => $address_id,
751                                         'firstname' => $Qaddress->valueProtected('entry_firstname'),
752                                         'lastname' => $Qaddress->valueProtected('entry_lastname'),
753                                         'company' => $Qaddress->valueProtected('entry_company'),
754                                         'street_address' => $Qaddress->valueProtected('entry_street_address'),
755                                         'suburb' => $Qaddress->valueProtected('entry_suburb'),
756                                         'city' => $Qaddress->valueProtected('entry_city'),
757                                         'postcode' => $Qaddress->valueProtected('entry_postcode'),
758                                         'state' => (!osc_empty($Qaddress->valueProtected('entry_state'))) ? $Qaddress->valueProtected('entry_state') : $Qaddress->valueProtected('zone_name'),
759                                         'zone_id' => $Qaddress->valueInt('entry_zone_id'),
760                                         'zone_code' => $Qaddress->value('zone_code'),
761                                         'country_id' => $Qaddress->valueInt('entry_country_id'),
762                                         'country_title' => $Qaddress->value('countries_name'),
763                                         'country_iso_code_2' => $Qaddress->value('countries_iso_code_2'),
764                                         'country_iso_code_3' => $Qaddress->value('countries_iso_code_3'),
765                                         'format' => $Qaddress->value('address_format'),
766                                         'telephone_number' => $Qaddress->value('entry_telephone'));
hpdl
439
767
hpdl
1674
768         if ( is_array($previous_address) && ( ($previous_address['id'] != $this->_billing_address['id']) || ($previous_address['country_id'] != $this->_billing_address['country_id']) || ($previous_address['zone_id'] != $this->_billing_address['zone_id']) || ($previous_address['state'] != $this->_billing_address['state']) || ($previous_address['postcode'] != $this->_billing_address['postcode']) ) ) {
769           $this->_calculate();
770         }
hpdl
439
771       }
hpdl
421
772     }
773
hpdl
1674
774     public function getBillingAddress($key = null) {
775       if ( empty($key) ) {
hpdl
421
776         return $this->_billing_address;
777       }
778
779       return $this->_billing_address[$key];
780     }
781
hpdl
1674
782     public function resetBillingAddress() {
hpdl
438
783       global $osC_Customer;
784
hpdl
1674
785       $this->_billing_address = array('zone_id' => STORE_ZONE,
786                                       'country_id' => STORE_COUNTRY);
hpdl
438
787
hpdl
1674
788       if ( $osC_Customer->isLoggedOn() && $osC_Customer->hasDefaultAddress() ) {
hpdl
438
789         $this->setBillingAddress($osC_Customer->getDefaultAddressID());
790       }
hpdl
424
791     }
792
hpdl
1674
793     public function setBillingMethod($billing_array) {
hpdl
421
794       $this->_billing_method = $billing_array;
hpdl
439
795
796       $this->_calculate();
hpdl
421
797     }
798
hpdl
1674
799     public function getBillingMethod($key = null) {
800       if ( empty($key) ) {
hpdl
421
801         return $this->_billing_method;
802       }
803
804       return $this->_billing_method[$key];
805     }
806
hpdl
1674
807