📄 shopping_cart.php
字号:
<?php
/*
$Id: shopping_cart.php,v 1.29 2002/05/26 10:57:21 thomasamoulton Exp $
网络商店 - 吉鑫网络
http://www.chinaifc.com
Copyright (c) 2000,2001 网络商店
汗化版权所有吉鑫网络
*/
class shoppingCart {
var $contents, $total, $weight;
function shoppingCart() {
$this->reset();
}
function restore_contents() {
global $customer_id;
if (!$customer_id) return 0;
// insert current cart contents in database
if ($this->contents) {
reset($this->contents);
while (list($products_id, ) = each($this->contents)) {
$qty = $this->contents[$products_id]['qty'];
$product_query = tep_db_query("select products_id from " . TABLE_CUSTOMERS_BASKET . " where customers_id = '" . $customer_id . "' and products_id = '" . $products_id . "'");
if (!tep_db_num_rows($product_query)) {
tep_db_query("insert into " . TABLE_CUSTOMERS_BASKET . " (customers_id, products_id, customers_basket_quantity, customers_basket_date_added) values ('" . $customer_id . "', '" . $products_id . "', '" . $qty . "', '" . date('Ymd') . "')");
if ($this->contents[$products_id]['attributes']) {
reset($this->contents[$products_id]['attributes']);
while (list($option, $value) = each($this->contents[$products_id]['attributes'])) {
tep_db_query("insert into " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . " (customers_id, products_id, products_options_id, products_options_value_id) values ('" . $customer_id . "', '" . $products_id . "', '" . $option . "', '" . $value . "')");
}
}
} else {
tep_db_query("update " . TABLE_CUSTOMERS_BASKET . " set customers_basket_quantity = '" . $qty . "' where customers_id = '" . $customer_id . "' and products_id = '" . $products_id . "'");
}
}
}
// reset per-session cart contents, but not the database contents
$this->reset(FALSE);
$products_query = tep_db_query("select products_id, customers_basket_quantity from " . TABLE_CUSTOMERS_BASKET . " where customers_id = '" . $customer_id . "'");
while ($products = tep_db_fetch_array($products_query)) {
$this->contents[$products['products_id']] = array('qty' => $products['customers_basket_quantity']);
// attributes
$attributes_query = tep_db_query("select products_options_id, products_options_value_id from " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . " where customers_id = '" . $customer_id . "' and products_id = '" . $products['products_id'] . "'");
while ($attributes = tep_db_fetch_array($attributes_query)) {
$this->contents[$products['products_id']]['attributes'][$attributes['products_options_id']] = $attributes['products_options_value_id'];
}
}
$this->cleanup();
}
function reset($reset_database = FALSE) {
global $customer_id;
$this->contents = array();
$this->total = 0;
if ($customer_id && $reset_database) {
tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET . " where customers_id = '" . $customer_id . "'");
tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . " where customers_id = '" . $customer_id . "'");
}
}
function add_cart($products_id, $qty = '', $attributes = '', $notify = true) {
global $new_products_id_in_cart, $customer_id;
$products_id = tep_get_uprid($products_id, $attributes);
if ($notify == true) {
$new_products_id_in_cart = $products_id;
tep_session_register('new_products_id_in_cart');
}
if ($this->in_cart($products_id)) {
$this->update_quantity($products_id, $qty, $attributes);
} else {
if ($qty == '') $qty = '1'; // if no quantity is supplied, then add '1' to the customers basket
$this->contents[] = array($products_id);
$this->contents[$products_id] = array('qty' => $qty);
// insert into database
if ($customer_id) tep_db_query("insert into " . TABLE_CUSTOMERS_BASKET . " (customers_id, products_id, customers_basket_quantity, customers_basket_date_added) values ('" . $customer_id . "', '" . $products_id . "', '" . $qty . "', '" . date('Ymd') . "')");
if (is_array($attributes)) {
reset($attributes);
while (list($option, $value) = each($attributes)) {
$this->contents[$products_id]['attributes'][$option] = $value;
// insert into database
if ($customer_id) tep_db_query("insert into " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . " (customers_id, products_id, products_options_id, products_options_value_id) values ('" . $customer_id . "', '" . $products_id . "', '" . $option . "', '" . $value . "')");
}
}
}
$this->cleanup();
}
function update_quantity($products_id, $quantity = '', $attributes = '') {
global $customer_id;
if ($quantity == '') return true; // nothing needs to be updated if theres no quantity, so we return true..
$this->contents[$products_id] = array('qty' => $quantity);
// update database
if ($customer_id) tep_db_query("update " . TABLE_CUSTOMERS_BASKET . " set customers_basket_quantity = '" . $quantity . "' where customers_id = '" . $customer_id . "' and products_id = '" . $products_id . "'");
if (is_array($attributes)) {
reset($attributes);
while (list($option, $value) = each($attributes)) {
$this->contents[$products_id]['attributes'][$option] = $value;
// update database
if ($customer_id) tep_db_query("update " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . " set products_options_value_id = '" . $value . "' where customers_id = '" . $customer_id . "' and products_id = '" . $products_id . "' and products_options_id = '" . $option . "'");
}
}
}
function cleanup() {
global $customer_id;
reset($this->contents);
while (list($key,) = each($this->contents)) {
if ($this->contents[$key]['qty'] < 1) {
unset($this->contents[$key]);
// remove from database
if ($customer_id) {
tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET . " where customers_id = '" . $customer_id . "' and products_id = '" . $key . "'");
tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . " where customers_id = '" . $customer_id . "' and products_id = '" . $key . "'");
}
}
}
}
function count_contents() { // get total number of items in cart
$total_items = 0;
if (is_array($this->contents)) {
reset($this->contents);
while (list($products_id, ) = each($this->contents)) {
$total_items += $this->get_quantity($products_id);
}
}
return $total_items;
}
function get_quantity($products_id) {
if ($this->contents[$products_id]) {
return $this->contents[$products_id]['qty'];
} else {
return 0;
}
}
function in_cart($products_id) {
if ($this->contents[$products_id]) {
return true;
} else {
return false;
}
}
function remove($products_id) {
global $customer_id;
unset($this->contents[$products_id]);
// remove from database
if ($customer_id) {
tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET . " where customers_id = '" . $customer_id . "' and products_id = '" . $products_id . "'");
tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . " where customers_id = '" . $customer_id . "' and products_id = '" . $products_id . "'");
}
}
function remove_all() {
$this->reset();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -