| [ Index ] |
|
Code source de osCommerce 2.2ms2-060817 |
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_string = tep_get_uprid($products_id, $attributes); 82 $products_id = tep_get_prid($products_id_string); 83 84 $attributes_pass_check = true; 85 86 if (is_array($attributes)) { 87 reset($attributes); 88 while (list($option, $value) = each($attributes)) { 89 if (!is_numeric($option) || !is_numeric($value)) { 90 $attributes_pass_check = false; 91 break; 92 } 93 } 94 } 95 96 if (is_numeric($products_id) && is_numeric($qty) && ($attributes_pass_check == true)) { 97 $check_product_query = tep_db_query("select products_status from " . TABLE_PRODUCTS . " where products_id = '" . (int)$products_id . "'"); 98 $check_product = tep_db_fetch_array($check_product_query); 99 100 if (($check_product !== false) && ($check_product['products_status'] == '1')) { 101 if ($notify == true) { 102 $new_products_id_in_cart = $products_id; 103 tep_session_register('new_products_id_in_cart'); 104 } 105 106 if ($this->in_cart($products_id_string)) { 107 $this->update_quantity($products_id_string, $qty, $attributes); 108 } else { 109 $this->contents[$products_id_string] = array('qty' => $qty); 110 // insert into database 111 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_string) . "', '" . (int)$qty . "', '" . date('Ymd') . "')"); 112 113 if (is_array($attributes)) { 114 reset($attributes); 115 while (list($option, $value) = each($attributes)) { 116 $this->contents[$products_id_string]['attributes'][$option] = $value; 117 // insert into database 118 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_string) . "', '" . (int)$option . "', '" . (int)$value . "')"); 119 } 120 } 121 } 122 123 $this->cleanup(); 124 125 // assign a temporary unique ID to the order contents to prevent hack attempts during the checkout procedure 126 $this->cartID = $this->generate_cart_id(); 127 } 128 } 129 } 130 131 function update_quantity($products_id, $quantity = '', $attributes = '') { 132 global $customer_id; 133 134 $products_id_string = tep_get_uprid($products_id, $attributes); 135 $products_id = tep_get_prid($products_id_string); 136 137 $attributes_pass_check = true; 138 139 if (is_array($attributes)) { 140 reset($attributes); 141 while (list($option, $value) = each($attributes)) { 142 if (!is_numeric($option) || !is_numeric($value)) { 143 $attributes_pass_check = false; 144 break; 145 } 146 } 147 } 148 149 if (is_numeric($products_id) && isset($this->contents[$products_id_string]) && is_numeric($quantity) && ($attributes_pass_check == true)) { 150 $this->contents[$products_id_string] = array('qty' => $quantity); 151 // update database 152 if (tep_session_is_registered('customer_id')) tep_db_query("update " . TABLE_CUSTOMERS_BASKET . " set customers_basket_quantity = '" . (int)$quantity . "' where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($products_id_string) . "'"); 153 154 if (is_array($attributes)) { 155 reset($attributes); 156 while (list($option, $value) = each($attributes)) { 157 $this->contents[$products_id_string]['attributes'][$option] = $value; 158 // update database 159 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_string) . "' and products_options_id = '" . (int)$option . "'"); 160 } 161 } 162 } 163 } 164 165 function cleanup() { 166 global $customer_id; 167 168 reset($this->contents); 169 while (list($key,) = each($this->contents)) { 170 if ($this->contents[$key]['qty'] < 1) { 171 unset($this->contents[$key]); 172 // remove from database 173 if (tep_session_is_registered('customer_id')) { 174 tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET . " where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($key) . "'"); 175 tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . " where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($key) . "'"); 176 } 177 } 178 } 179 } 180 181 function count_contents() { // get total number of items in cart 182 $total_items = 0; 183 if (is_array($this->contents)) { 184 reset($this->contents); 185 while (list($products_id, ) = each($this->contents)) { 186 $total_items += $this->get_quantity($products_id); 187 } 188 } 189 190 return $total_items; 191 } 192 193 function get_quantity($products_id) { 194 if (isset($this->contents[$products_id])) { 195 return $this->contents[$products_id]['qty']; 196 } else { 197 return 0; 198 } 199 } 200 201 function in_cart($products_id) { 202 if (isset($this->contents[$products_id])) { 203 return true; 204 } else { 205 return false; 206 } 207 } 208 209 function remove($products_id) { 210 global $customer_id; 211 212 unset($this->contents[$products_id]); 213 // remove from database 214 if (tep_session_is_registered('customer_id')) { 215 tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET . " where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($products_id) . "'"); 216 tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . " where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($products_id) . "'"); 217 } 218 219 // assign a temporary unique ID to the order contents to prevent hack attempts during the checkout procedure 220 $this->cartID = $this->generate_cart_id(); 221 } 222 223 function remove_all() { 224 $this->reset(); 225 } 226 227 function get_product_id_list() { 228 $product_id_list = ''; 229 if (is_array($this->contents)) { 230 reset($this->contents); 231 while (list($products_id, ) = each($this->contents)) { 232 $product_id_list .= ', ' . $products_id; 233 } 234 } 235 236 return substr($product_id_list, 2); 237 } 238 239 function calculate() { 240 $this->total = 0; 241 $this->weight = 0; 242 if (!is_array($this->contents)) return 0; 243 244 reset($this->contents); 245 while (list($products_id, ) = each($this->contents)) { 246 $qty = $this->contents[$products_id]['qty']; 247 248 // products price 249 $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 . "'"); 250 if ($product = tep_db_fetch_array($product_query)) { 251 $prid = $product['products_id']; 252 $products_tax = tep_get_tax_rate($product['products_tax_class_id']); 253 $products_price = $product['products_price']; 254 $products_weight = $product['products_weight']; 255 256 $specials_query = tep_db_query("select specials_new_products_price from " . TABLE_SPECIALS . " where products_id = '" . (int)$prid . "' and status = '1'"); 257 if (tep_db_num_rows ($specials_query)) { 258 $specials = tep_db_fetch_array($specials_query); 259 $products_price = $specials['specials_new_products_price']; 260 } 261 262 $this->total += tep_add_tax($products_price, $products_tax) * $qty; 263 $this->weight += ($qty * $products_weight); 264 } 265 266 // attributes price 267 if (isset($this->contents[$products_id]['attributes'])) { 268 reset($this->contents[$products_id]['attributes']); 269 while (list($option, $value) = each($this->contents[$products_id]['attributes'])) { 270 $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 . "'"); 271 $attribute_price = tep_db_fetch_array($attribute_price_query); 272 if ($attribute_price['price_prefix'] == '+') { 273 $this->total += $qty * tep_add_tax($attribute_price['options_values_price'], $products_tax); 274 } else { 275 $this->total -= $qty * tep_add_tax($attribute_price['options_values_price'], $products_tax); 276 } 277 } 278 } 279 } 280 } 281 282 function attributes_price($products_id) { 283 $attributes_price = 0; 284 285 if (isset($this->contents[$products_id]['attributes'])) { 286 reset($this->contents[$products_id]['attributes']); 287 while (list($option, $value) = each($this->contents[$products_id]['attributes'])) { 288 $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 . "'"); 289 $attribute_price = tep_db_fetch_array($attribute_price_query); 290 if ($attribute_price['price_prefix'] == '+') { 291 $attributes_price += $attribute_price['options_values_price']; 292 } else { 293 $attributes_price -= $attribute_price['options_values_price']; 294 } 295 } 296 } 297 298 return $attributes_price; 299 } 300 301 function get_products() { 302 global $languages_id; 303 304 if (!is_array($this->contents)) return false; 305 306 $products_array = array(); 307 reset($this->contents); 308 while (list($products_id, ) = each($this->contents)) { 309 $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 . "'"); 310 if ($products = tep_db_fetch_array($products_query)) { 311 $prid = $products['products_id']; 312 $products_price = $products['products_price']; 313 314 $specials_query = tep_db_query("select specials_new_products_price from " . TABLE_SPECIALS . " where products_id = '" . (int)$prid . "' and status = '1'"); 315 if (tep_db_num_rows($specials_query)) { 316 $specials = tep_db_fetch_array($specials_query); 317 $products_price = $specials['specials_new_products_price']; 318 } 319 320 $products_array[] = array('id' => $products_id, 321 'name' => $products['products_name'], 322 'model' => $products['products_model'], 323 'image' => $products['products_image'], 324 'price' => $products_price, 325 'quantity' => $this->contents[$products_id]['qty'], 326 'weight' => $products['products_weight'], 327 'final_price' => ($products_price + $this->attributes_price($products_id)), 328 'tax_class_id' => $products['products_tax_class_id'], 329 'attributes' => (isset($this->contents[$products_id]['attributes']) ? $this->contents[$products_id]['attributes'] : '')); 330 } 331 } 332 333 return $products_array; 334 } 335 336 function show_total() { 337 $this->calculate(); 338 339 return $this->total; 340 } 341 342 function show_weight() { 343 $this->calculate(); 344 345 return $this->weight; 346 } 347 348 function generate_cart_id($length = 5) { 349 return tep_create_random_value($length, 'digits'); 350 } 351 352 function get_content_type() { 353 $this->content_type = false; 354 355 if ( (DOWNLOAD_ENABLED == 'true') && ($this->count_contents() > 0) ) { 356 reset($this->contents); 357 while (list($products_id, ) = each($this->contents)) { 358 if (isset($this->contents[$products_id]['attributes'])) { 359 reset($this->contents[$products_id]['attributes']); 360 while (list(, $value) = each($this->contents[$products_id]['attributes'])) { 361 $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"); 362 $virtual_check = tep_db_fetch_array($virtual_check_query); 363 364 if ($virtual_check['total'] > 0) { 365 switch ($this->content_type) { 366 case 'physical': 367 $this->content_type = 'mixed'; 368 369 return $this->content_type; 370 break; 371 default: 372 $this->content_type = 'virtual'; 373 break; 374 } 375 } else { 376 switch ($this->content_type) { 377 case 'virtual': 378 $this->content_type = 'mixed'; 379 380 return $this->content_type; 381 break; 382 default: 383 $this->content_type = 'physical'; 384 break; 385 } 386 } 387 } 388 } else { 389 switch ($this->content_type) { 390 case 'virtual': 391 $this->content_type = 'mixed'; 392 393 return $this->content_type; 394 break; 395 default: 396 $this->content_type = 'physical'; 397 break; 398 } 399 } 400 } 401 } else { 402 $this->content_type = 'physical'; 403 } 404 405 return $this->content_type; 406 } 407 408 function unserialize($broken) { 409 for(reset($broken);$kv=each($broken);) { 410 $key=$kv['key']; 411 if (gettype($this->$key)!="user function") 412 $this->$key=$kv['value']; 413 } 414 } 415 416 } 417 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
| Généré le : Mon Nov 26 19:48:25 2007 | par Balluche grâce à PHPXref 0.7 |
|