Quick Search:

View

Revision:

Diff

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