📄 shopping_cart.php
字号:
if (is_array($value) ) {
reset($value);
while (list($opt, $val) = each($value)) {
$products_options_sort_order= zen_get_attributes_options_sort_order(zen_get_prid($products_id), $option, $opt);
$sql = "insert into " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . "
(customers_id, products_id, products_options_id, products_options_value_id, products_options_sort_order)
values ('" . (int)$_SESSION['customer_id'] . "', '" . zen_db_input($products_id) . "', '" .
(int)$option.'_chk'.$val . "', '" . $val . "', '" . $products_options_sort_order . "')";
$db->Execute($sql);
}
} else {
if ($attr_value) {
$attr_value = zen_db_input($attr_value);
}
$products_options_sort_order= zen_get_attributes_options_sort_order(zen_get_prid($products_id), $option, $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) . "', '" .
(int)$option . "', '" . $value . "', '" . $attr_value . "', '" . $products_options_sort_order . "')";
$db->Execute($sql);
}
}
}
}
}
}
$this->cleanup();
// assign a temporary unique ID to the order contents to prevent hack attempts during the checkout procedure
$this->cartID = $this->generate_cart_id();
$this->notify('NOTIFIER_CART_ADD_CART_END');
}
/**
* Method to update a cart items quantity
*
* Changes the current quamtity of a certain item in the cart to
* a new value. Also updates the database sored cart if customer is
* logged in.
*
* @param mixed product ID of item to update
* @param decimal the quantity to update the item to
* @param array product atributes attached to the item
* @return void
* @global object access to the db object
*/
function update_quantity($products_id, $quantity = '', $attributes = '') {
global $db;
$this->notify('NOTIFIER_CART_UPDATE_QUANTITY_START');
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);
// update database
if (isset($_SESSION['customer_id'])) {
$sql = "update " . TABLE_CUSTOMERS_BASKET . "
set customers_basket_quantity = '" . $quantity . "'
where customers_id = '" . (int)$_SESSION['customer_id'] . "'
and products_id = '" . zen_db_input($products_id) . "'";
$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 030108 check if text input is blank, if so do not update 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;
}
// update 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 (zen_session_is_registered('customer_id')) zen_db_query("update " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . " set products_options_value_id = '" . (int)$value . "', products_options_value_text = '" . zen_db_input($attr_value) . "' where customers_id = '" . (int)$customer_id . "' and products_id = '" . zen_db_input($products_id) . "' and products_options_id = '" . (int)$option . "'");
if ($attr_value) {
$attr_value = zen_db_input($attr_value);
}
if (is_array($value) ) {
reset($value);
while (list($opt, $val) = each($value)) {
$products_options_sort_order= zen_get_attributes_options_sort_order(zen_get_prid($products_id), $option, $opt);
$sql = "update " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . "
set products_options_value_id = '" . $val . "'
where customers_id = '" . (int)$_SESSION['customer_id'] . "'
and products_id = '" . zen_db_input($products_id) . "'
and products_options_id = '" . (int)$option.'_chk'.$val . "'";
$db->Execute($sql);
}
} else {
if (isset($_SESSION['customer_id'])) {
$sql = "update " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . "
set products_options_value_id = '" . $value . "', products_options_value_text = '" . $attr_value . "'
where customers_id = '" . (int)$_SESSION['customer_id'] . "'
and products_id = '" . zen_db_input($products_id) . "'
and products_options_id = '" . (int)$option . "'";
$db->Execute($sql);
}
}
}
}
}
$this->notify('NOTIFIER_CART_UPDATE_QUANTITY_END');
}
/**
* Method to clean up carts contents
*
* For various reasons, the quantity of an item in the cart can
* fall to zero. This method removes from the cart
* all items that have reached this state. The database-stored cart
* is also updated where necessary
*
* @return void
* @global object access to the db object
*/
function cleanup() {
global $db;
$this->notify('NOTIFIER_CART_CLEANUP_START');
reset($this->contents);
while (list($key,) = each($this->contents)) {
if (!isset($this->contents[$key]['qty']) || $this->contents[$key]['qty'] <= 0) {
unset($this->contents[$key]);
// remove from database
if (isset($_SESSION['customer_id'])) {
$sql = "delete from " . TABLE_CUSTOMERS_BASKET . "
where customers_id = '" . (int)$_SESSION['customer_id'] . "'
and products_id = '" . $key . "'";
$db->Execute($sql);
$sql = "delete from " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . "
where customers_id = '" . (int)$_SESSION['customer_id'] . "'
and products_id = '" . $key . "'";
$db->Execute($sql);
}
}
}
$this->notify('NOTIFIER_CART_CLEANUP_END');
}
/**
* Method to count total number of items in cart
*
* Note this is not just the number of distinct items in the cart,
* but the number of items adjusted for the quantity of each item
* in the cart, So we have had 2 items in the cart, one with a quantity
* of 3 and the other with a quantity of 4 our total number of items
* would be 7
*
* @return total number of items in cart
*/
function count_contents() {
$this->notify('NOTIFIER_CART_COUNT_CONTENTS_START');
$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);
}
}
$this->notify('NOTIFIER_CART_COUNT_CONTENTS_END');
return $total_items;
}
/**
* Method to get the quantity of an item in the cart
*
* @param mixed product ID of item to check
* @return decimal the quantity of the item
*/
function get_quantity($products_id) {
$this->notify('NOTIFIER_CART_GET_QUANTITY_START');
if (isset($this->contents[$products_id])) {
$this->notify('NOTIFIER_CART_GET_QUANTITY_END_QTY');
return $this->contents[$products_id]['qty'];
} else {
$this->notify('NOTIFIER_CART_GET_QUANTITY_END_FALSE');
return 0;
}
}
/**
* Method to check wheter a product exists in the cart
*
* @param mixed product ID of item to check
* @return boolean
*/
function in_cart($products_id) {
// die($products_id);
$this->notify('NOTIFIER_CART_IN_CART_START');
if (isset($this->contents[$products_id])) {
$this->notify('NOTIFIER_CART_IN_CART_END_TRUE');
return true;
} else {
$this->notify('NOTIFIER_CART_IN_CART_END_FALSE');
return false;
}
}
/**
* Method to remove an item from the cart
*
* @param mixed product ID of item to remove
* @return void
* @global object access to the db object
*/
function remove($products_id) {
global $db;
$this->notify('NOTIFIER_CART_REMOVE_START');
//die($products_id);
//CLR 030228 add call zen_get_uprid to correctly format product ids containing quotes
// $products_id = zen_get_uprid($products_id, $attributes);
unset($this->contents[$products_id]);
// remove from database
if ($_SESSION['customer_id']) {
// zen_db_query("delete from " . TABLE_CUSTOMERS_BASKET . " where customers_id = '" . (int)$customer_id . "' and products_id = '" . zen_db_input($products_id) . "'");
$sql = "delete from " . TABLE_CUSTOMERS_BASKET . "
where customers_id = '" . (int)$_SESSION['customer_id'] . "'
and products_id = '" . zen_db_input($products_id) . "'";
$db->Execute($sql);
// zen_db_query("delete from " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . " where customers_id = '" . (int)$customer_id . "' and products_id = '" . zen_db_input($products_id) . "'");
$sql = "delete from " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . "
where customers_id = '" . (int)$_SESSION['customer_id'] . "'
and products_id = '" . zen_db_input($products_id) . "'";
$db->Execute($sql);
}
// assign a temporary unique ID to the order contents to prevent hack attempts during the checkout procedure
$this->cartID = $this->generate_cart_id();
$this->notify('NOTIFIER_CART_REMOVE_END');
}
/**
* Method remove all products from the cart
*
* @return void
*/
function remove_all() {
$this->notify('NOTIFIER_CART_REMOVE_ALL_START');
$this->reset();
$this->notify('NOTIFIER_CART_REMOVE_ALL_END');
}
/**
* Method return a comma separated list of all products in the cart
*
* @return string
* @todo ICW - is this actually used anywhere?
*/
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 .= ', ' . zen_db_input($products_id);
}
}
return substr($product_id_list, 2);
}
/**
* Method to calculate cart totals(price and weight)
*
* @return void
* @global object access to the db object
*/
function calculate() {
global $db;
$this->total = 0;
$this->weight = 0;
// shipping adjustment
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -