📄 shopping_cart.php
字号:
<?php
/**
* File contains just the shopping cart class
*
* @package classes
* @copyright Copyright 2003-2006 Zen Cart Development Team
* @copyright Portions Copyright 2003 osCommerce
* @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0
* @version $Id: shopping_cart.php 3379 2006-04-06 00:54:13Z drbyte $
*/
/**
* Class for managing the Shopping Cart
*
* @package classes
* @copyright Copyright 2003-2006 Zen Cart Development Team
* @copyright Portions Copyright 2003 osCommerce
* @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0
*/
if (!defined('IS_ADMIN_FLAG')) {
die('Illegal Access');
}
class shoppingCart extends base {
/**
* shopping cart contents
* @var array
*/
var $contents;
/**
* shopping cart total price
* @var decimal
*/
var $total;
/**
* shopping cart total weight
* @var decimal
*/
var $weight;
/**
* cart identifier
* @var integer
*/
var $cartID;
/**
* overall content type of shopping cart
* @var string
*/
var $content_type;
/**
* number of free shipping items in cart
* @var decimal
*/
var $free_shipping_item;
/**
* total price of free shipping items in cart
* @var decimal
*/
var $free_shipping_weight;
/**
* total weight of free shipping items in cart
* @var decimal
*/
var $free_shipping_price;
/**
* constructor method
*
* Simply resets the users cart.
* @return void
*/
function shoppingCart() {
$this->notify('NOTIFIER_CART_INSTANTIATE_START');
$this->reset();
$this->notify('NOTIFIER_CART_INSTANTIATE_END');
}
/**
* Method to restore cart contents
*
* For customers who login, cart contents are also stored in the database.
* {TABLE_CUSTOMER_BASKET et al}. This allows the system to remember the
* contents of their cart over multiple sessions.
* This method simply retrieve the content of the databse store cart
* for a given customer. Note also that if the customer already has
* some items in their cart before thet login, these are merged with
* the stored contents.
*
* @return void
* @global object access to the db object
*/
function restore_contents() {
global $db;
if (!$_SESSION['customer_id']) return false;
$this->notify('NOTIFIER_CART_RESTORE_CONTENTS_START');
// insert current cart contents in database
if (is_array($this->contents)) {
reset($this->contents);
while (list($products_id, ) = each($this->contents)) {
// $products_id = urldecode($products_id);
$qty = $this->contents[$products_id]['qty'];
$product_query = "select products_id
from " . TABLE_CUSTOMERS_BASKET . "
where customers_id = '" . (int)$_SESSION['customer_id'] . "'
and products_id = '" . zen_db_input($products_id) . "'";
$product = $db->Execute($product_query);
if ($product->RecordCount()<=0) {
$sql = "insert into " . TABLE_CUSTOMERS_BASKET . "
(customers_id, products_id, customers_basket_quantity,
customers_basket_date_added)
values ('" . (int)$_SESSION['customer_id'] . "', '" . zen_db_input($products_id) . "', '" .
$qty . "', '" . date('Ymd') . "')";
$db->Execute($sql);
if (isset($this->contents[$products_id]['attributes'])) {
reset($this->contents[$products_id]['attributes']);
while (list($option, $value) = each($this->contents[$products_id]['attributes'])) {
//clr 031714 udate query to include attribute value. This is needed for text attributes.
$attr_value = $this->contents[$products_id]['attributes_values'][$option];
// zen_db_query("insert into " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . " (customers_id, products_id, products_options_id, products_options_value_id, products_options_value_text) values ('" . (int)$customer_id . "', '" . zen_db_input($products_id) . "', '" . (int)$option . "', '" . (int)$value . "', '" . zen_db_input($attr_value) . "')");
$products_options_sort_order= zen_get_attributes_options_sort_order(zen_get_prid($products_id), $option, $value);
if ($attr_value) {
$attr_value = zen_db_input($attr_value);
}
$sql = "insert into " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . "
(customers_id, products_id, products_options_id,
products_options_value_id, products_options_value_text, products_options_sort_order)
values ('" . (int)$_SESSION['customer_id'] . "', '" . zen_db_input($products_id) . "', '" .
$option . "', '" . $value . "', '" . $attr_value . "', '" . $products_options_sort_order . "')";
$db->Execute($sql);
}
}
} else {
$sql = "update " . TABLE_CUSTOMERS_BASKET . "
set customers_basket_quantity = '" . $qty . "'
where customers_id = '" . (int)$_SESSION['customer_id'] . "'
and products_id = '" . zen_db_input($products_id) . "'";
$db->Execute($sql);
}
}
}
// reset per-session cart contents, but not the database contents
$this->reset(false);
$products_query = "select products_id, customers_basket_quantity
from " . TABLE_CUSTOMERS_BASKET . "
where customers_id = '" . (int)$_SESSION['customer_id'] . "'";
$products = $db->Execute($products_query);
while (!$products->EOF) {
$this->contents[$products->fields['products_id']] = array('qty' => $products->fields['customers_basket_quantity']);
// attributes
// set contents in sort order
//CLR 020606 update query to pull attribute value_text. This is needed for text attributes.
// $attributes_query = zen_db_query("select products_options_id, products_options_value_id, products_options_value_text from " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . " where customers_id = '" . (int)$customer_id . "' and products_id = '" . zen_db_input($products['products_id']) . "'");
$order_by = ' order by LPAD(products_options_sort_order,11,"0")';
$attributes = $db->Execute("select products_options_id, products_options_value_id, products_options_value_text
from " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . "
where customers_id = '" . (int)$_SESSION['customer_id'] . "'
and products_id = '" . zen_db_input($products->fields['products_id']) . "' " . $order_by);
while (!$attributes->EOF) {
$this->contents[$products->fields['products_id']]['attributes'][$attributes->fields['products_options_id']] = $attributes->fields['products_options_value_id'];
//CLR 020606 if text attribute, then set additional information
if ($attributes->fields['products_options_value_id'] == PRODUCTS_OPTIONS_VALUES_TEXT_ID) {
$this->contents[$products->fields['products_id']]['attributes_values'][$attributes->fields['products_options_id']] = $attributes->fields['products_options_value_text'];
}
$attributes->MoveNext();
}
$products->MoveNext();
}
$this->notify('NOTIFIER_CART_RESTORE_CONTENTS_END');
$this->cleanup();
}
/**
* Method to reset cart contents
*
* resets the contents of the session cart(e,g, empties it)
* Depending on the setting of the $reset_database parameter will
* also empty the contents of the database stored cart. (Only relevant
* if the customer is logged in)
*
* @param boolean whether to reset customers db basket
* @return void
* @global object access to the db object
*/
function reset($reset_database = false) {
global $db;
$this->notify('NOTIFIER_CART_RESET_START');
$this->contents = array();
$this->total = 0;
$this->weight = 0;
$this->content_type = false;
// shipping adjustment
$this->free_shipping_item = 0;
$this->free_shipping_price = 0;
$this->free_shipping_weight = 0;
if (isset($_SESSION['customer_id']) && ($reset_database == true)) {
$sql = "delete from " . TABLE_CUSTOMERS_BASKET . "
where customers_id = '" . (int)$_SESSION['customer_id'] . "'";
$db->Execute($sql);
$sql = "delete from " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . "
where customers_id = '" . (int)$_SESSION['customer_id'] . "'";
$db->Execute($sql);
}
unset($this->cartID);
$_SESSION['cartID'] = '';
$this->notify('NOTIFIER_CART_RESET_END');
}
/**
* Method to add an item to the cart
*
* This method is usually called as the result of a user action.
* As the method name applies it adds an item to the uses current cart
* and if the customer is logged in, also adds to the database sored
* cart.
*
* @param integer the product ID of the item to be added
* @param decimal the quantity of the item to be added
* @param array any attributes that are attache to the product
* @param boolean whether to add the product to the notify list
* @return void
* @global object access to the db object
* @todo ICW - documentation stub
*/
function add_cart($products_id, $qty = '1', $attributes = '', $notify = true) {
global $db;
$this->notify('NOTIFIER_CART_ADD_CART_START');
$products_id = zen_get_uprid($products_id, $attributes);
if ($notify == true) {
$_SESSION['new_products_id_in_cart'] = $products_id;
}
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);
// insert into database
if (isset($_SESSION['customer_id'])) {
$sql = "insert into " . TABLE_CUSTOMERS_BASKET . "
(customers_id, products_id, customers_basket_quantity,
customers_basket_date_added)
values ('" . (int)$_SESSION['customer_id'] . "', '" . zen_db_input($products_id) . "', '" .
$qty . "', '" . date('Ymd') . "')";
$db->Execute($sql);
}
if (is_array($attributes)) {
reset($attributes);
while (list($option, $value) = each($attributes)) {
//CLR 020606 check if input was from text box. If so, store additional attribute information
//CLR 020708 check if text input is blank, if so do not add to attribute lists
//CLR 030228 add htmlspecialchars processing. This handles quotes and other special chars in the user input.
$attr_value = NULL;
$blank_value = FALSE;
if (strstr($option, TEXT_PREFIX)) {
if (trim($value) == NULL) {
$blank_value = TRUE;
} else {
$option = substr($option, strlen(TEXT_PREFIX));
$attr_value = stripslashes($value);
$value = PRODUCTS_OPTIONS_VALUES_TEXT_ID;
$this->contents[$products_id]['attributes_values'][$option] = $attr_value;
}
}
if (!$blank_value) {
if (is_array($value) ) {
reset($value);
while (list($opt, $val) = each($value)) {
$this->contents[$products_id]['attributes'][$option.'_chk'.$val] = $val;
}
} else {
$this->contents[$products_id]['attributes'][$option] = $value;
}
// insert into database
//CLR 020606 update db insert to include attribute value_text. This is needed for text attributes.
//CLR 030228 add zen_db_input() processing
if (isset($_SESSION['customer_id'])) {
// if (zen_session_is_registered('customer_id')) zen_db_query("insert into " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . " (customers_id, products_id, products_options_id, products_options_value_id, products_options_value_text) values ('" . (int)$customer_id . "', '" . zen_db_input($products_id) . "', '" . (int)$option . "', '" . (int)$value . "', '" . zen_db_input($attr_value) . "')");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -