Quick Search:

View

Revision:

Diff

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