shopping_cart.php
来自「全新且完善的强大网上商店系统」· PHP 代码 · 共 377 行 · 第 1/2 页
PHP
377 行
<?php
class shoppingCart {
var $contents, $total, $weight, $cartID, $content_type;
function shoppingCart() {
$this->reset();
}
function restore_contents() {
global $customer_id,$db,$table_customers_basket,$table_customers_basket_attributes;
if (!tep_session_is_registered('customer_id')) return false;
if (is_array($this->contents)) {
reset($this->contents);
while (list($products_id, ) = each($this->contents)) {
$qty = $this->contents[$products_id]['qty'];
$product_query = $db->query("select products_id from $table_customers_basket where customers_id = '" . (int)$customer_id . "' and products_id = '" . ($products_id) . "'");
if (!$db->num_rows($product_query)) {
$db->query("insert into $table_customers_basket (customers_id, products_id, customers_basket_quantity, customers_basket_date_added) values ('" . (int)$customer_id . "', '" . ($products_id) . "', '" . $qty . "', '" . date('Ymd') . "')");
if (isset($this->contents[$products_id]['attributes'])) {
reset($this->contents[$products_id]['attributes']);
while (list($option, $value) = each($this->contents[$products_id]['attributes'])) {
$db->query("insert into $table_customers_basket_attributes (customers_id, products_id, products_options_id, products_options_value_id) values ('" . (int)$customer_id . "', '" . ($products_id) . "', '" . (int)$option . "', '" . (int)$value . "')");
}
}
} else {
$db->query("update $table_customers_basket set customers_basket_quantity = '" . $qty . "' where customers_id = '" . (int)$customer_id . "' and products_id = '" . ($products_id) . "'");
}
}
}
$this->reset(false);
$products_query = $db->query("select products_id, customers_basket_quantity from $table_customers_basket where customers_id = '" . (int)$customer_id . "'");
while ($products = $db->fetch_array($products_query)) {
$this->contents[$products['products_id']] = array('qty' => $products['customers_basket_quantity']);
$attributes_query = $db->query("select products_options_id, products_options_value_id from $table_customers_basket_attributes where customers_id = '" . (int)$customer_id . "' and products_id = '" . ($products['products_id']) . "'");
while ($attributes = $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, $db, $table_customers_basket, $table_customers_basket_attributes;
$this->contents = array();
$this->total = 0;
$this->weight = 0;
$this->content_type = false;
if (tep_session_is_registered('customer_id') && ($reset_database == true)) {
$db->query("delete from $table_customers_basket where customers_id = '" . (int)$customer_id . "'");
$db->query("delete from $table_customers_basket_attributes where customers_id = '" . (int)$customer_id . "'");
}
unset($this->cartID);
if (tep_session_is_registered('cartID')) tep_session_unregister('cartID');
}
function add_cart($products_id, $qty = '1', $attributes = '', $notify = true) {
global $new_products_id_in_cart, $customer_id, $db, $table_customers_basket, $table_customers_basket_attributes;
$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 {
$this->contents[] = array($products_id);
$this->contents[$products_id] = array('qty' => $qty);
if (tep_session_is_registered('customer_id')) $db->query("insert into $table_customers_basket (customers_id, products_id, customers_basket_quantity, customers_basket_date_added) values ('" . (int)$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;
if (tep_session_is_registered('customer_id')) $db->query("insert into $table_customers_basket_attributes (customers_id, products_id, products_options_id, products_options_value_id) values ('" . (int)$customer_id . "', '" .$products_id. "', '" . (int)$option . "', '" . (int)$value . "')");
}
}
}
$this->cleanup();
$this->cartID = $this->generate_cart_id();
}
function update_quantity($products_id, $quantity = '', $attributes = '') {
global $customer_id, $db, $table_customers_basket, $table_customers_basket_attributes;
if (empty($quantity)) return true; // nothing needs to be updated if theres no quantity, so we return true..
$this->contents[$products_id] = array('qty' => $quantity);
if (tep_session_is_registered('customer_id')) $db->query("update $table_customers_basket set customers_basket_quantity = '" . $quantity . "' where customers_id = '" . (int)$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;
if (tep_session_is_registered('customer_id')) $db->query("update $table_customers_basket_attributes set products_options_value_id = '" . (int)$value . "' where customers_id = '" . (int)$customer_id . "' and products_id = '" . ($products_id) . "' and products_options_id = '" . (int)$option . "'");
}
}
}
function cleanup() {
global $customer_id,$db,$table_customers_basket_attributes,$table_customers_basket;
reset($this->contents);
while (list($key,) = each($this->contents)) {
if ($this->contents[$key]['qty'] < 1) {
unset($this->contents[$key]);
if (tep_session_is_registered('customer_id')) {
$db->query("delete from $table_customers_basket where customers_id = '" . (int)$customer_id . "' and products_id = '" . ($key) . "'");
$db->query("delete from $table_customers_basket_attributes where customers_id = '" . (int)$customer_id . "' and products_id = '" . ($key) . "'");
}
}
}
}
function count_contents() {
$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 (isset($this->contents[$products_id])) {
return $this->contents[$products_id]['qty'];
} else {
return 0;
}
}
function in_cart($products_id) {
if (isset($this->contents[$products_id])) {
return true;
} else {
return false;
}
}
function remove($products_id) {
global $customer_id,$db,$table_customers_basket_attributes,$table_customers_basket;
unset($this->contents[$products_id]);
if (tep_session_is_registered('customer_id')) {
$db->query("delete from $table_customers_basket where customers_id = '" . (int)$customer_id . "' and products_id = '" . ($products_id) . "'");
$db->query("delete from $table_customers_basket_attributes where customers_id = '" . (int)$customer_id . "' and products_id = '" . ($products_id) . "'");
}
$this->cartID = $this->generate_cart_id();
}
function remove_all() {
$this->reset();
}
function get_product_id_list() {
$product_id_list = '';
if (is_array($this->contents)) {
reset($this->contents);
while (list($products_id, ) = each($this->contents)) {
$product_id_list .= ', ' . $products_id;
}
}
return substr($product_id_list, 2);
}
function calculate() {
global $table_products,$db,$table_specials,$table_products_attributes, $customer_email, $groupdiscount;
$this->total = 0;
$this->weight = 0;
if (!is_array($this->contents)) return 0;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?