| [ Index ] | osCommerce
Docs :: PHP Cross Reference For osCommerce 2.2 MS2 Provided By OSCdox.com |
1 <?php 2 /* 3 $Id: shopping_cart.php,v 1.35 2003/06/25 21:14:33 hpdl Exp $ 4 5 osCommerce, Open Source E-Commerce Solutions 6 http://www.oscommerce.com 7 8 Copyright (c) 2003 osCommerce 9 10 Released under the GNU General Public License 11 */ 12 13 class shoppingCart { 14 var $contents, $total, $weight, $cartID, $content_type; 15 16 function shoppingCart() { 17 $this->reset(); 18 } 19 20 function restore_contents() { 21 global $customer_id; 22 23 if (!tep_session_is_registered('customer_id')) return false; 24 25 // insert current cart contents in database 26 if (is_array($this->contents)) { 27 reset($this->contents); 28 while (list($products_id, ) = each($this->contents)) { 29 $qty = $this->contents[$products_id]['qty']; 30 $product_query = tep_db_query("select products_id from " . TABLE_CUSTOMERS_BASKET . " where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($products_id) . "'"); 31 if (!tep_db_num_rows($product_query)) { 32 tep_db_query("insert into " . TABLE_CUSTOMERS_BASKET . " (customers_id, products_id, customers_basket_quantity, customers_basket_date_added) values ('" . (int)$customer_id . "', '" . tep_db_input($products_id) . "', '" . $qty . "', '" . date('Ymd') . "')"); 33 if (isset($this->contents[$products_id]['attributes'])) { 34 reset($this->contents[$products_id]['attributes']); 35 while (list($option, $value) = each($this->contents[$products_id]['attributes'])) { 36 tep_db_query("insert into " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . " (customers_id, products_id, products_options_id, products_options_value_id) values ('" . (int)$customer_id . "', '" . tep_db_input($products_id) . "', '" . (int)$option . "', '" . (int)$value . "')"); 37 } 38 } 39 } else { 40 tep_db_query("update " . TABLE_CUSTOMERS_BASKET . " set customers_basket_quantity = '" . $qty . "' where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($products_id) . "'"); 41 } 42 } 43 } 44 45 // reset per-session cart contents, but not the database contents 46 $this->reset(false); 47 48 $products_query = tep_db_query("select products_id, customers_basket_quantity from " . TABLE_CUSTOMERS_BASKET . " where customers_id = '" . (int)$customer_id . "'"); 49 while ($products = tep_db_fetch_array($products_query)) { 50 $this->contents[$products['products_id']] = array('qty' => $products['customers_basket_quantity']); 51 // attributes 52 $attributes_query = tep_db_query("select products_options_id, products_options_value_id from " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . " where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($products['products_id']) . "'"); 53 while ($attributes = tep_db_fetch_array($attributes_query)) { 54 $this->contents[$products['products_id']]['attributes'][$attributes['products_options_id']] = $attributes['products_options_value_id']; 55 } 56 } 57 58 $this->cleanup(); 59 } 60 61 function reset($reset_database = false) { 62 global $customer_id; 63 64 $this->contents = array(); 65 $this->total = 0; 66 $this->weight = 0; 67 $this->content_type = false; 68 69 if (tep_session_is_registered('customer_id') && ($reset_database == true)) { 70 tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET . " where customers_id = '" . (int)$customer_id . "'"); 71 tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . " where customers_id = '" . (int)$customer_id . "'"); 72 } 73 74 unset($this->cartID); 75 if (tep_session_is_registered('cartID')) tep_session_unregister('cartID'); 76 } 77 78 function add_cart($products_id, $qty = '1', $attributes = '', $notify = true) { 79 global $new_products_id_in_cart, $customer_id; 80 81 $products_id = tep_get_uprid($products_id, $attributes); 82 if ($notify == true) { 83 $new_products_id_in_cart = $products_id; 84 tep_session_register('new_products_id_in_cart'); 85 } 86 87 if ($this->in_cart($products_id)) { 88 $this->update_quantity($products_id, $qty, $attributes); 89 } else { 90 $this->contents[] = array($products_id); 91 $this->contents[$products_id] = array('qty' => $qty); 92 // insert into database 93 if (tep_session_is_registered('customer_id')) tep_db_query("insert into " . TABLE_CUSTOMERS_BASKET . " (customers_id, products_id, customers_basket_quantity, customers_basket_date_added) values ('" . (int)$customer_id . "', '" . tep_db_input($products_id) . "', '" . $qty . "', '" . date('Ymd') . "')"); 94 95 if (is_array($attributes)) { 96 reset($attributes); 97 while (list($option, $value) = each($attributes)) { 98 $this->contents[$products_id]['attributes'][$option] = $value; 99 // insert into database 100 if (tep_session_is_registered('customer_id')) tep_db_query("insert into " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . " (customers_id, products_id, products_options_id, products_options_value_id) values ('" . (int)$customer_id . "', '" . tep_db_input($products_id) . "', '" . (int)$option . "', '" . (int)$value . "')"); 101 } 102 } 103 } 104 $this->cleanup(); 105 106 // assign a temporary unique ID to the order contents to prevent hack attempts during the checkout procedure 107 $this->cartID = $this->generate_cart_id(); 108 } 109 110 function update_quantity($products_id, $quantity = '', $attributes = '') { 111 global $customer_id; 112 113 if (empty($quantity)) return true; // nothing needs to be updated if theres no quantity, so we return true.. 114 115 $this->contents[$products_id] = array('qty' => $quantity); 116 // update database 117 if (tep_session_is_registered('customer_id')) tep_db_query("update " . TABLE_CUSTOMERS_BASKET . " set customers_basket_quantity = '" . $quantity . "' where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($products_id) . "'"); 118 119 if (is_array($attributes)) { 120 reset($attributes); 121 while (list($option, $value) = each($attributes)) { 122 $this->contents[$products_id]['attributes'][$option] = $value; 123 // update database 124 if (tep_session_is_registered('customer_id')) tep_db_query("update " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . " set products_options_value_id = '" . (int)$value . "' where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($products_id) . "' and products_options_id = '" . (int)$option . "'"); 125 } 126 } 127 } 128 129 function cleanup() { 130 global $customer_id; 131 132 reset($this->contents); 133 while (list($key,) = each($this->contents)) { 134 if ($this->contents[$key]['qty'] < 1) { 135 unset($this->contents[$key]); 136 // remove from database 137 if (tep_session_is_registered('customer_id')) { 138 tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET . " where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($key) . "'"); 139 tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . " where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($key) . "'"); 140 } 141 } 142 } 143 } 144 145 function count_contents() { // get total number of items in cart 146 $total_items = 0; 147 if (is_array($this->contents)) { 148 reset($this->contents); 149 while (list($products_id, ) = each($this->contents)) { 150 $total_items += $this->get_quantity($products_id); 151 } 152 } 153 154 return $total_items; 155 } 156 157 function get_quantity($products_id) { 158 if (isset($this->contents[$products_id])) { 159 return $this->contents[$products_id]['qty']; 160 } else { 161 return 0; 162 } 163 } 164 165 function in_cart($products_id) { 166 if (isset($this->contents[$products_id])) { 167 return true; 168 } else { 169 return false; 170 } 171 } 172 173 function remove($products_id) { 174 global $customer_id; 175 176 unset($this->contents[$products_id]); 177 // remove from database 178 if (tep_session_is_registered('customer_id')) { 179 tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET . " where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($products_id) . "'"); 180 tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . " where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($products_id) . "'"); 181 } 182 183 // assign a temporary unique ID to the order contents to prevent hack attempts during the checkout procedure 184 $this->cartID = $this->generate_cart_id(); 185 } 186 187 function remove_all() { 188 $this->reset(); 189 } 190 191 function get_product_id_list() { 192 $product_id_list = ''; 193 if (is_array($this->contents)) { 194 reset($this->contents); 195 while (list($products_id, ) = each($this->contents)) { 196 $product_id_list .= ', ' . $products_id; 197 } 198 } 199 200 return substr($product_id_list, 2); 201 } 202 203 function calculate() { 204 $this->total = 0; 205 $this->weight = 0; 206 if (!is_array($this->contents)) return 0; 207 208 reset($this->contents); 209 while (list($products_id, ) = each($this->contents)) { 210 $qty = $this->contents[$products_id]['qty']; 211 212 // products price 213 $product_query = tep_db_query("select products_id, products_price, products_tax_class_id, products_weight from " . TABLE_PRODUCTS . " where products_id = '" . (int)$products_id . "'"); 214 if ($product = tep_db_fetch_array($product_query)) { 215 $prid = $product['products_id']; 216 $products_tax = tep_get_tax_rate($product['products_tax_class_id']); 217 $products_price = $product['products_price']; 218 $products_weight = $product['products_weight']; 219 220 $specials_query = tep_db_query("select specials_new_products_price from " . TABLE_SPECIALS . " where products_id = '" . (int)$prid . "' and status = '1'"); 221 if (tep_db_num_rows ($specials_query)) { 222 $specials = tep_db_fetch_array($specials_query); 223 $products_price = $specials['specials_new_products_price']; 224 } 225 226 $this->total += tep_add_tax($products_price, $products_tax) * $qty; 227 $this->weight += ($qty * $products_weight); 228 } 229 230 // attributes price 231 if (isset($this->contents[$products_id]['attributes'])) { 232 reset($this->contents[$products_id]['attributes']); 233 while (list($option, $value) = each($this->contents[$products_id]['attributes'])) { 234 $attribute_price_query = tep_db_query("select options_values_price, price_prefix from " . TABLE_PRODUCTS_ATTRIBUTES . " where products_id = '" . (int)$prid . "' and options_id = '" . (int)$option . "' and options_values_id = '" . (int)$value . "'"); 235 $attribute_price = tep_db_fetch_array($attribute_price_query); 236 if ($attribute_price['price_prefix'] == '+') { 237 $this->total += $qty * tep_add_tax($attribute_price['options_values_price'], $products_tax); 238 } else { 239 $this->total -= $qty * tep_add_tax($attribute_price['options_values_price'], $products_tax); 240 } 241 } 242 } 243 } 244 } 245 246 function attributes_price($products_id) { 247 $attributes_price = 0; 248 249 if (isset($this->contents[$products_id]['attributes'])) { 250 reset($this->contents[$products_id]['attributes']); 251 while (list($option, $value) = each($this->contents[$products_id]['attributes'])) { 252 $attribute_price_query = tep_db_query("select options_values_price, price_prefix from " . TABLE_PRODUCTS_ATTRIBUTES . " where products_id = '" . (int)$products_id . "' and options_id = '" . (int)$option . "' and options_values_id = '" . (int)$value . "'"); 253 $attribute_price = tep_db_fetch_array($attribute_price_query); 254 if ($attribute_price['price_prefix'] == '+') { 255 $attributes_price += $attribute_price['options_values_price']; 256 } else { 257 $attributes_price -= $attribute_price['options_values_price']; 258 } 259 } 260 } 261 262 return $attributes_price; 263 } 264 265 function get_products() { 266 global $languages_id; 267 268 if (!is_array($this->contents)) return false; 269 270 $products_array = array(); 271 reset($this->contents); 272 while (list($products_id, ) = each($this->contents)) { 273 $products_query = tep_db_query("select p.products_id, pd.products_name, p.products_model, p.products_image, p.products_price, p.products_weight, p.products_tax_class_id from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd where p.products_id = '" . (int)$products_id . "' and pd.products_id = p.products_id and pd.language_id = '" . (int)$languages_id . "'"); 274 if ($products = tep_db_fetch_array($products_query)) { 275 $prid = $products['products_id']; 276 $products_price = $products['products_price']; 277 278 $specials_query = tep_db_query("select specials_new_products_price from " . TABLE_SPECIALS . " where products_id = '" . (int)$prid . "' and status = '1'"); 279 if (tep_db_num_rows($specials_query)) { 280 $specials = tep_db_fetch_array($specials_query); 281 $products_price = $specials['specials_new_products_price']; 282 } 283 284 $products_array[] = array('id' => $products_id, 285 'name' => $products['products_name'], 286 'model' => $products['products_model'], 287 'image' => $products['products_image'], 288 'price' => $products_price, 289 'quantity' => $this->contents[$products_id]['qty'], 290 'weight' => $products['products_weight'], 291 'final_price' => ($products_price + $this->attributes_price($products_id)), 292 'tax_class_id' => $products['products_tax_class_id'], 293 'attributes' => (isset($this->contents[$products_id]['attributes']) ? $this->contents[$products_id]['attributes'] : '')); 294 } 295 } 296 297 return $products_array; 298 } 299 300 function show_total() { 301 $this->calculate(); 302 303 return $this->total; 304 } 305 306 function show_weight() { 307 $this->calculate(); 308 309 return $this->weight; 310 } 311 312 function generate_cart_id($length = 5) { 313 return tep_create_random_value($length, 'digits'); 314 } 315 316 function get_content_type() { 317 $this->content_type = false; 318 319 if ( (DOWNLOAD_ENABLED == 'true') && ($this->count_contents() > 0) ) { 320 reset($this->contents); 321 while (list($products_id, ) = each($this->contents)) { 322 if (isset($this->contents[$products_id]['attributes'])) { 323 reset($this->contents[$products_id]['attributes']); 324 while (list(, $value) = each($this->contents[$products_id]['attributes'])) { 325 $virtual_check_query = tep_db_query("select count(*) as total from " . TABLE_PRODUCTS_ATTRIBUTES . " pa, " . TABLE_PRODUCTS_ATTRIBUTES_DOWNLOAD . " pad where pa.products_id = '" . (int)$products_id . "' and pa.options_values_id = '" . (int)$value . "' and pa.products_attributes_id = pad.products_attributes_id"); 326 $virtual_check = tep_db_fetch_array($virtual_check_query); 327 328 if ($virtual_check['total'] > 0) { 329 switch ($this->content_type) { 330 case 'physical': 331 $this->content_type = 'mixed'; 332 333 return $this->content_type; 334 break; 335 default: 336 $this->content_type = 'virtual'; 337 break; 338 } 339 } else { 340 switch ($this->content_type) { 341 case 'virtual': 342 $this->content_type = 'mixed'; 343 344 return $this->content_type; 345 break; 346 default: 347 $this->content_type = 'physical'; 348 break; 349 } 350 } 351 } 352 } else { 353 switch ($this->content_type) { 354 case 'virtual': 355 $this->content_type = 'mixed'; 356 357 return $this->content_type; 358 break; 359 default: 360 $this->content_type = 'physical'; 361 break; 362 } 363 } 364 } 365 } else { 366 $this->content_type = 'physical'; 367 } 368 369 return $this->content_type; 370 } 371 372 function unserialize($broken) { 373 for(reset($broken);$kv=each($broken);) { 374 $key=$kv['key']; 375 if (gettype($this->$key)!="user function") 376 $this->$key=$kv['value']; 377 } 378 } 379 380 } 381 ?>
title
Description
Body
title
Description
Body
| Generated: Tue Nov 4 23:53:39 2003 | Hosted By :: AABox.com |
Cross-referenced by PHPXref 0.4 |