Quick Search:

View

Revision:

Diff

Diff from 1680 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 1680 2007-08-29 21:02:22Z 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
1674
304       if ( is_numeric($product_id) ) {
305         $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');
306         $Qproduct->bindTable(':table_products', TABLE_PRODUCTS);
307         $Qproduct->bindTable(':table_products_images', TABLE_PRODUCTS_IMAGES);
308         $Qproduct->bindInt(':default_flag', 1);
309         $Qproduct->bindInt(':products_id', $product_id);
310         $Qproduct->execute();
hpdl
1
311
hpdl
1674
312         if ( $Qproduct->valueInt('products_status') === 1 ) {
313           if ( $this->exists($product_id) ) {
hpdl
1677
314             $item_id = $this->getBasketID($product_id);
315
hpdl
1674
316             if ( !is_numeric($quantity) ) {
hpdl
1677
317               $quantity = $this->getQuantity($item_id) + 1;
hpdl
418
318             }
319
hpdl
1677
320             $this->_contents[$item_id]['quantity'] = $quantity;
hpdl
418
321
hpdl
1674
322             if ( $osC_Customer->isLoggedOn() ) {
hpdl
1677
323               $Qupdate = $osC_Database->query('update :table_shopping_carts set quantity = :quantity where customers_id = :customers_id and item_id = :item_id');
324               $Qupdate->bindTable(':table_shopping_carts', TABLE_SHOPPING_CARTS);
325               $Qupdate->bindInt(':quantity', $quantity);
hpdl
418
326               $Qupdate->bindInt(':customers_id', $osC_Customer->getID());
hpdl
1677
327               $Qupdate->bindInt(':item_id', $item_id);
hpdl
418
328               $Qupdate->execute();
329             }
hpdl
1
330           } else {
hpdl
1674
331             if ( !is_numeric($quantity) ) {
hpdl
418
332               $quantity = 1;
333             }
334
hpdl
1674
335             $Qdescription = $osC_Database->query('select products_name, products_keyword from :table_products_description where products_id = :products_id and language_id = :language_id');
336             $Qdescription->bindTable(':table_products_description', TABLE_PRODUCTS_DESCRIPTION);
337             $Qdescription->bindInt(':products_id', ($Qproduct->valueInt('parent_id') > 0) ? $Qproduct->valueInt('parent_id') : $product_id);
338             $Qdescription->bindInt(':language_id', $osC_Language->getID());
339             $Qdescription->execute();
hpdl
418
340
hpdl
1674
341             $price = $Qproduct->value('products_price');
hpdl
420
342
hpdl
1674
343             if ( $osC_Services->isStarted('specials') ) {
hpdl
809
344               global $osC_Specials;
hpdl
418
345
hpdl
1674
346               if ( $new_price = $osC_Specials->getPrice($product_id) ) {
hpdl
809
347                 $price = $new_price;
348               }
hpdl
418
349             }
350
hpdl
1677
351             if ( $osC_Customer->isLoggedOn() ) {
352               $Qid = $osC_Database->query('select max(item_id) as item_id from :table_shopping_carts where customers_id = :customers_id');
353               $Qid->bindTable(':table_shopping_carts', TABLE_SHOPPING_CARTS);
354               $Qid->bindInt(':customers_id', $osC_Customer->getID());
355               $Qid->execute();
hpdl
420
356
hpdl
1677
357               $item_id = $Qid->valueInt('item_id') + 1;
358             } else {
359               $item_id = max(array_keys($this->_contents)) + 1;
360             }
361
362             $this->_contents[$item_id] = array('item_id' => $item_id,
363                                                'id' => $product_id,
364                                                'parent_id' => $Qproduct->valueInt('parent_id'),
365                                                'name' => $Qdescription->value('products_name'),
366                                                'keyword' => $Qdescription->value('products_keyword'),
367                                                'image' => $Qproduct->value('image'),
368                                                'price' => $price,
369                                                'quantity' => $quantity,
370                                                'weight' => $Qproduct->value('products_weight'),
371                                                'tax_class_id' => $Qproduct->valueInt('products_tax_class_id'),
372                                                'date_added' => osC_DateTime::getShort(osC_DateTime::getNow()),
373                                                'weight_class_id' => $Qproduct->valueInt('products_weight_class'));
374
hpdl
1674
375             if ( $osC_Customer->isLoggedOn() ) {
hpdl
1677
376               $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)');
377               $Qnew->bindTable(':table_shopping_carts', TABLE_SHOPPING_CARTS);
hpdl
184
378               $Qnew->bindInt(':customers_id', $osC_Customer->getID());
hpdl
1677
379               $Qnew->bindInt(':item_id', $item_id);
hpdl
1674
380               $Qnew->bindInt(':products_id', $product_id);
hpdl
1677
381               $Qnew->bindInt(':quantity', $quantity);
382               $Qnew->bindRaw(':date_added', 'now()');
hpdl
19
383               $Qnew->execute();
384             }
hpdl
1
385
hpdl
1674
386             if ( $Qproduct->valueInt('parent_id') > 0 ) {
hpdl
1680
387               $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
388               $Qvariant->bindTable(':table_products_variants', TABLE_PRODUCTS_VARIANTS);
389               $Qvariant->bindTable(':table_products_variants_values', TABLE_PRODUCTS_VARIANTS_VALUES);
390               $Qvariant->bindTable(':table_products_variants_groups', TABLE_PRODUCTS_VARIANTS_GROUPS);
391               $Qvariant->bindInt(':products_id', $product_id);
392               $Qvariant->bindInt(':languages_id', $osC_Language->getID());
393               $Qvariant->bindInt(':languages_id', $osC_Language->getID());
394               $Qvariant->execute();
hpdl
418
395
hpdl
1674
396               while ( $Qvariant->next() ) {
hpdl
1677
397                 $group_title = osC_Variants::getGroupTitle($Qvariant->value('module'), $Qvariant->toArray());
398                 $value_title = osC_Variants::getValueTitle($Qvariant->value('module'), $Qvariant->toArray());
399                 $has_custom_value = osC_Variants::hasCustomValue($Qvariant->value('module'));
400
401                 $this->_contents[$item_id]['variants'][] = array('group_id' => $Qvariant->valueInt('group_id'),
402                                                                  'value_id' => $Qvariant->valueInt('value_id'),
403                                                                  'group_title' => $group_title,
404                                                                  'value_title' => $value_title,
405                                                                  'has_custom_value' => $has_custom_value);
406
407                 if ( $osC_Customer->isLoggedOn() && ($has_custom_value === true) ) {
408                   $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)');
409                   $Qnew->bindTable(':table_shopping_carts_custom_variants_values', TABLE_SHOPPING_CARTS_CUSTOM_VARIANTS_VALUES);
410                   $Qnew->bindInt(':shopping_carts_item_id', $item_id);
411                   $Qnew->bindInt(':customers_id', $osC_Customer->getID());
412                   $Qnew->bindInt(':products_id', $product_id);
413                   $Qnew->bindInt(':products_variants_values_id', $Qvariant->valueInt('value_id'));
414                   $Qnew->bindValue(':products_variants_values_text', $value_title);
415                   $Qnew->execute();
416                 }
hpdl
1
417               }
418             }
419           }
420
hpdl
418
421           $this->_cleanUp();
hpdl
419
422           $this->_calculate();
hpdl
1
423         }
424       }
425     }
426
hpdl
1674
427     public function numberOfItems() {
hpdl
418
428       $total = 0;
hpdl
1
429
hpdl
1677
430       foreach ( $this->_contents as $product ) {
431         $total += $product['quantity'];
hpdl
1
432       }
433
hpdl
418
434       return $total;
hpdl
1
435     }
436
hpdl
1677
437     public function getBasketID($product_id) {
438       foreach ( $this->_contents as $item_id => $product ) {
439         if ( $product['id'] === $product_id ) {
440           return $item_id;
441         }
442       }
hpdl
1
443     }
444
hpdl
1677
445     public function getQuantity($item_id) {
446       return ( isset($this->_contents[$item_id]) ) ? $this->_contents[$item_id]['quantity'] : 0;
447     }
448
hpdl
1674
449     public function exists($product_id) {
hpdl
1677
450       foreach ( $this->_contents as $product ) {
451         if ( $product['id'] === $product_id ) {
452           if ( isset($product['variants']) ) {
453             foreach ( $product['variants'] as $variant ) {
454               if ( $variant['has_custom_value'] === true ) {
455                 return false;
456               }
457             }
458           }
459
460           return true;
461         }
462       }
463
464       return false;
hpdl
1
465     }
466
hpdl
1677
467     public function update($item_id, $quantity) {
hpdl
19
468       global $osC_Database, $osC_Customer;
hpdl
1
469
hpdl
1677
470       if ( !is_numeric($quantity) ) {
471         $quantity = $this->getQuantity($item_id) + 1;
472       }
hpdl
418
473
hpdl
1677
474       $this->_contents[$item_id]['quantity'] = $quantity;
475
hpdl
1674
476       if ( $osC_Customer->isLoggedOn() ) {
hpdl
1677
477         $Qupdate = $osC_Database->query('update :table_shopping_carts set quantity = :quantity where customers_id = :customers_id and item_id = :item_id');
478         $Qupdate->bindTable(':table_shopping_carts', TABLE_SHOPPING_CARTS);
479         $Qupdate->bindInt(':quantity', $quantity);
480         $Qupdate->bindInt(':customers_id', $osC_Customer->getID());
481         $Qupdate->bindInt(':item_id', $item_id);
482         $Qupdate->execute();
483       }
484
485       $this->_cleanUp();
486       $this->_calculate();
487     }
488
489     public function remove($item_id) {
490       global $osC_Database, $osC_Customer;
491
492       unset($this->_contents[$item_id]);
493
494       if ( $osC_Customer->isLoggedOn() ) {
495         $Qdelete = $osC_Database->query('delete from :table_shopping_carts where customers_id = :customers_id and item_id = :item_id');
496         $Qdelete->bindTable(':table_shopping_carts', TABLE_SHOPPING_CARTS);
hpdl
184
497         $Qdelete->bindInt(':customers_id', $osC_Customer->getID());
hpdl
1677
498         $Qdelete->bindInt(':item_id', $item_id);
hpdl
19
499         $Qdelete->execute();
hpdl
1677
500
501         $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');
502         $Qdelete->bindTable(':table_shopping_carts_custom_variants_values', TABLE_SHOPPING_CARTS_CUSTOM_VARIANTS_VALUES);
503         $Qdelete->bindInt(':customers_id', $osC_Customer->getID());
504         $Qdelete->bindInt(':shopping_carts_item_id', $item_id);
505         $Qdelete->execute();
hpdl
1
506       }
507
hpdl
419
508       $this->_calculate();
hpdl
1
509     }
510
hpdl
1674
511     public function getProducts() {
hpdl
420
512       static $_is_sorted = false;
hpdl
19
513
hpdl
1674
514       if ( $_is_sorted === false ) {
hpdl
420
515         $_is_sorted = true;
hpdl
1
516
hpdl
420
517         uasort($this->_contents, array('osC_ShoppingCart', '_uasortProductsByDateAdded'));
hpdl
1
518       }
519
hpdl
420
520       return $this->_contents;
hpdl
1
521     }
522
hpdl
1674
523     public function getSubTotal() {
hpdl
435
524       return $this->_sub_total;
525     }
526
hpdl
1674
527     public function getTotal() {
hpdl
420
528       return $this->_total;
529     }
hpdl
1
530
hpdl
1674
531     public function getWeight() {
hpdl
420
532       return $this->_weight;
hpdl
1
533     }
534
hpdl
1674
535     public function generateCartID($length = 5) {
hpdl
734
536       return osc_create_random_string($length, 'digits');
hpdl
1
537     }
538
hpdl
1674
539     public function getCartID() {
hpdl
1420
540       return $_SESSION['cartID'];
hpdl
1
541     }
542
hpdl
1674
543     public function getContentType() {
hpdl
19
544       global $osC_Database;
545
hpdl
420
546       $this->_content_type = 'physical';
hpdl
1
547
hpdl
500
548       if ( (DOWNLOAD_ENABLED == '1') && $this->hasContents() ) {
hpdl
1674
549         foreach ( $this->_contents as $product_id => $data ) {
550 /* HPDL
hpdl
418
551           if (isset($data['attributes'])) {
552             foreach ($data['attributes'] as $value) {
hpdl
19
553               $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');
554               $Qcheck->bindTable(':table_products_attributes', TABLE_PRODUCTS_ATTRIBUTES);
555               $Qcheck->bindTable(':table_products_attributes_download', TABLE_PRODUCTS_ATTRIBUTES_DOWNLOAD);
556               $Qcheck->bindInt(':products_id', $products_id);
hpdl
420
557               $Qcheck->bindInt(':options_values_id', $value['options_values_id']);
hpdl
19
558               $Qcheck->execute();
hpdl
1
559
hpdl
19
560               if ($Qcheck->valueInt('total') > 0) {
hpdl
420
561                 switch ($this->_content_type) {
hpdl
1
562                   case 'physical':
hpdl
420
563                     $this->_content_type = 'mixed';
hpdl
1
564
hpdl
420
565                     return $this->_content_type;
hpdl
1
566                     break;
567                   default:
hpdl
420
568                     $this->_content_type = 'virtual';
hpdl
1
569                     break;
570                 }
571               } else {
hpdl
420
572                 switch ($this->_content_type) {
hpdl
1
573                   case 'virtual':
hpdl
420
574                     $this->_content_type = 'mixed';
hpdl
1
575
hpdl
420
576                     return $this->_content_type;
hpdl
1
577                     break;
578                   default:
hpdl
420
579                     $this->_content_type = 'physical';
hpdl
1
580                     break;
581                 }
582               }
583             }
584           } else {
hpdl
1674
585 */
586             switch ( $this->_content_type ) {
hpdl
1
587               case 'virtual':
hpdl
420
588                 $this->_content_type = 'mixed';
hpdl
1
589
hpdl
1674
590                 break 2;
591
hpdl
1
592               default:
hpdl
420
593                 $this->_content_type = 'physical';
hpdl
1674
594
hpdl
1
595                 break;
596             }
hpdl
1674
597 //          }
hpdl
1
598         }
599       }
600
hpdl
420
601       return $this->_content_type;
hpdl
1
602     }
hpdl
418
603
hpdl
1677
604     public function isVariant($item_id) {
605       return isset($this->_contents[$item_id]['variants']) && !empty($this->_contents[$item_id]['variants']);
hpdl
420
606     }
607
hpdl
1677
608     public function getVariant($item_id) {
609       if ( isset($this->_contents[$item_id]['variants']) && !empty($this->_contents[$item_id]['variants']) ) {
610         return $this->_contents[$item_id]['variants'];
hpdl
420
611       }
612     }
613
hpdl
1677
614     public function isInStock($item_id) {
hpdl
420
615       global $osC_Database;
616
617       $Qstock = $osC_Database->query('select products_quantity from :table_products where products_id = :products_id');
618       $Qstock->bindTable(':table_products', TABLE_PRODUCTS);
hpdl
1677
619       $Qstock->bindInt(':products_id', $this->_contents[$item_id]['id']);
hpdl
420
620       $Qstock->execute();
621
hpdl
1677
622       if ( ($Qstock->valueInt('products_quantity') - $this->_contents[$item_id]['quantity']) > 0 ) {
hpdl
420
623         return true;
hpdl
1674
624       } elseif ( $this->_products_in_stock === true ) {
hpdl
420
625         $this->_products_in_stock = false;
626       }
627
628       return false;
629     }
630
hpdl
1674
631     public function hasStock() {
hpdl
420
632       return $this->_products_in_stock;
633     }
634
hpdl
1674
635     public function hasShippingAddress() {
636       return isset($this->_shipping_address['id']);
hpdl
421
637     }
638
hpdl
1674
639     public function setShippingAddress($address_id) {
hpdl
421
640       global $osC_Database, $osC_Customer;
641
hpdl
1674
642       $previous_address = null;
hpdl
439
643
hpdl
1674
644       if ( isset($this->_shipping_address['id']) ) {
hpdl
439
645         $previous_address = $this->getShippingAddress();
646       }
647
hpdl
732
648       $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
649       $Qaddress->bindTable(':table_address_book', TABLE_ADDRESS_BOOK);
650       $Qaddress->bindTable(':table_zones', TABLE_ZONES);
651       $Qaddress->bindTable(':table_countries', TABLE_COUNTRIES);
652       $Qaddress->bindInt(':customers_id', $osC_Customer->getID());
653       $Qaddress->bindInt(':address_book_id', $address_id);
654       $Qaddress->execute();
hpdl
421
655
hpdl
1674
656       if ( $Qaddress->numberOfRows() === 1 ) {
657         $this->_shipping_address = array('id' => $address_id,
658                                          'firstname' => $Qaddress->valueProtected('entry_firstname'),
659                                          'lastname' => $Qaddress->valueProtected('entry_lastname'),
660                                          'company' => $Qaddress->valueProtected('entry_company'),
661                                          'street_address' => $Qaddress->valueProtected('entry_street_address'),
662                                          'suburb' => $Qaddress->valueProtected('entry_suburb'),
663                                          'city' => $Qaddress->valueProtected('entry_city'),
664                                          'postcode' => $Qaddress->valueProtected('entry_postcode'),
665                                          'state' => (!osc_empty($Qaddress->valueProtected('entry_state'))) ? $Qaddress->valueProtected('entry_state') : $Qaddress->valueProtected('zone_name'),
666                                          'zone_id' => $Qaddress->valueInt('entry_zone_id'),
667                                          'zone_code' => $Qaddress->value('zone_code'),
668                                          'country_id' => $Qaddress->valueInt('entry_country_id'),
669                                          'country_title' => $Qaddress->value('countries_name'),
670                                          'country_iso_code_2' => $Qaddress->value('countries_iso_code_2'),
671                                          'country_iso_code_3' => $Qaddress->value('countries_iso_code_3'),
672                                          'format' => $Qaddress->value('address_format'),
673                                          'telephone_number' => $Qaddress->value('entry_telephone'));
hpdl
439
674
hpdl
1674
675         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']) ) ) {
676           $this->_calculate();
677         }
hpdl
439
678       }
hpdl
421
679     }
680
hpdl
1674
681     public function getShippingAddress($key = null) {
682       if ( empty($key) ) {
hpdl
421
683         return $this->_shipping_address;
684       }
685
686       return $this->_shipping_address[$key];
687     }
688
hpdl
1674
689     public function resetShippingAddress() {
hpdl
438
690       global $osC_Customer;
691
hpdl
1674
692       $this->_shipping_address = array('zone_id' => STORE_ZONE,
693                                        'country_id' => STORE_COUNTRY);
hpdl
438
694
hpdl
1674
695       if ( $osC_Customer->isLoggedOn() && $osC_Customer->hasDefaultAddress() ) {
hpdl
438
696         $this->setShippingAddress($osC_Customer->getDefaultAddressID());
697       }
hpdl
422
698     }
699
hpdl
1674
700     public function setShippingMethod($shipping_array, $calculate_total = true) {
hpdl
421
701       $this->_shipping_method = $shipping_array;
hpdl
439
702
hpdl
1674
703       if ( $calculate_total === true ) {
hpdl
439
704         $this->_calculate(false);
705       }
hpdl
421
706     }
707
hpdl
1674
708     public function getShippingMethod($key = null) {
709       if ( empty($key) ) {
hpdl
421
710         return $this->_shipping_method;
711       }
712
713       return $this->_shipping_method[$key];
714     }
715
hpdl
1674
716     public function resetShippingMethod() {
hpdl
421
717       $this->_shipping_method = array();
hpdl
438
718
hpdl
439
719       $this->_calculate();
hpdl
421
720     }
721
hpdl
1674
722     public function hasShippingMethod() {
hpdl
421
723       return !empty($this->_shipping_method);
724     }
725
hpdl
1674
726     public function hasBillingAddress() {
727       return isset($this->_billing_address['id']);
hpdl
421
728     }
729
hpdl
1674
730     public function setBillingAddress($address_id) {
hpdl
421
731       global $osC_Database, $osC_Customer;
732
hpdl
439
733       $previous_address = false;
734
hpdl
1674
735       if ( isset($this->_billing_address['id']) ) {
hpdl
439
736         $previous_address = $this->getBillingAddress();
737       }
738
hpdl
732
739       $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
740       $Qaddress->bindTable(':table_address_book', TABLE_ADDRESS_BOOK);
741       $Qaddress->bindTable(':table_zones', TABLE_ZONES);
742       $Qaddress->bindTable(':table_countries', TABLE_COUNTRIES);
743       $Qaddress->bindInt(':customers_id', $osC_Customer->getID());
744       $Qaddress->bindInt(':address_book_id', $address_id);
745       $Qaddress->execute();
746
hpdl
1674
747       if ( $Qaddress->numberOfRows() === 1 ) {
748         $this->_billing_address = array('id' => $address_id,
749                                         'firstname' => $Qaddress->valueProtected('entry_firstname'),
750                                         'lastname' => $Qaddress->valueProtected('entry_lastname'),
751                                         'company' => $Qaddress->valueProtected('entry_company'),
752                                         'street_address' => $Qaddress->valueProtected('entry_street_address'),
753                                         'suburb' => $Qaddress->valueProtected('entry_suburb'),
754                                         'city' => $Qaddress->valueProtected('entry_city'),
755                                         'postcode' => $Qaddress->valueProtected('entry_postcode'),
756                                         'state' => (!osc_empty($Qaddress->valueProtected('entry_state'))) ? $Qaddress->valueProtected('entry_state') : $Qaddress->valueProtected('zone_name'),
757                                         'zone_id' => $Qaddress->valueInt('entry_zone_id'),
758                                         'zone_code' => $Qaddress->value('zone_code'),
759                                         'country_id' => $Qaddress->valueInt('entry_country_id'),
760                                         'country_title' => $Qaddress->value('countries_name'),
761                                         'country_iso_code_2' => $Qaddress->value('countries_iso_code_2'),
762                                         'country_iso_code_3' => $Qaddress->value('countries_iso_code_3'),
763                                         'format' => $Qaddress->value('address_format'),
764                                         'telephone_number' => $Qaddress->value('entry_telephone'));
hpdl
439
765
hpdl
1674
766         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']) ) ) {
767           $this->_calculate();
768         }
hpdl
439
769       }
hpdl
421
770     }
771
hpdl
1674
772     public function getBillingAddress($key = null) {
773       if ( empty($key) ) {
hpdl
421
774         return $this->_billing_address;
775       }
776
777       return $this->_billing_address[$key];
778     }
779
hpdl
1674
780     public function resetBillingAddress() {
hpdl
438
781       global $osC_Customer;
782
hpdl
1674
783       $this->_billing_address = array('zone_id' => STORE_ZONE,
784                                       'country_id' => STORE_COUNTRY);
hpdl
438
785
hpdl
1674
786       if ( $osC_Customer->isLoggedOn() && $osC_Customer->hasDefaultAddress() ) {
hpdl
438
787         $this->setBillingAddress($osC_Customer->getDefaultAddressID());
788       }
hpdl
424
789     }
790
hpdl
1674
791     public function setBillingMethod($billing_array) {
hpdl
421
792       $this->_billing_method = $billing_array;
hpdl
439