Quick Search:

View

Revision:

Diff

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