Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Synchronize admin/includes/shopping_cart.php with the one in catalog/…

…includes/.
  • Loading branch information...
commit c382a005ece1e6e373373c26e2c4836c611b8432 1 parent b72d6e0
@wrwrwr authored
Showing with 183 additions and 50 deletions.
  1. +183 −50 catalog/admin/includes/classes/shopping_cart.php
View
233 catalog/admin/includes/classes/shopping_cart.php
@@ -11,7 +11,7 @@
*/
class shoppingCart {
- var $contents, $total, $weight;
+ var $contents, $total, $weight, $content_type;
function shoppingCart() {
$this->reset();
@@ -20,17 +20,17 @@ function shoppingCart() {
function restore_contents() {
global $customer_id;
- if (!$customer_id) return 0;
+ if (!tep_session_is_registered('customer_id')) return false;
// insert current cart contents in database
- if ($this->contents) {
+ if (is_array($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 = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($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 ('" . (int)$customer_id . "', '" . tep_db_input($products_id) . "', '" . tep_db_input($qty) . "', '" . date('Ymd') . "')");
- if ($this->contents[$products_id]['attributes']) {
+ if (isset($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 ('" . (int)$customer_id . "', '" . tep_db_input($products_id) . "', '" . (int)$option . "', '" . (int)$value . "')");
@@ -42,8 +42,8 @@ function restore_contents() {
}
}
-// reset per-session cart contents, but not the database contents
- $this->reset(FALSE);
+// reset per-session cart contents, but not the database contents (this also resets the checkout)
+ $this->reset(false);
$products_query = tep_db_query("select products_id, customers_basket_quantity from " . TABLE_CUSTOMERS_BASKET . " where customers_id = '" . (int)$customer_id . "'");
while ($products = tep_db_fetch_array($products_query)) {
@@ -58,62 +58,128 @@ function restore_contents() {
$this->cleanup();
}
- function reset($reset_database = FALSE) {
+ function reset($reset_database = false) {
global $customer_id;
+ $this->reset_checkout();
$this->contents = array();
$this->total = 0;
+ $this->weight = 0;
+ $this->content_type = false;
- if ($customer_id && $reset_database) {
+ if (tep_session_is_registered('customer_id') && ($reset_database == true)) {
tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET . " where customers_id = '" . (int)$customer_id . "'");
tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . " where customers_id = '" . (int)$customer_id . "'");
}
}
- function add_cart($products_id, $qty = '', $attributes = '') {
+// resets checkout data, should be used after any cart modification to prevent wrong shipping / payment costs
+ function reset_checkout() {
+ global $shipping, $payment;
+ $shipping = null;
+ $payment = null;
+ if (tep_session_is_registered('shipping')) tep_session_unregister('shipping');
+ if (tep_session_is_registered('payment')) tep_session_unregister('payment');
+ }
+
+ function add_cart($products_id, $qty = '1', $attributes = '', $notify = true) {
global $new_products_id_in_cart, $customer_id;
- $products_id = tep_get_uprid($products_id, $attributes);
+ $products_id_string = tep_get_uprid($products_id, $attributes);
+ $products_id = tep_get_prid($products_id_string);
- 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
+ if (defined('MAX_QTY_IN_CART') && (MAX_QTY_IN_CART > 0) && ((int)$qty > MAX_QTY_IN_CART)) {
+ $qty = MAX_QTY_IN_CART;
+ }
+
+ $attributes_pass_check = true;
+
+ if (is_array($attributes) && !empty($attributes)) {
+ reset($attributes);
+ while (list($option, $value) = each($attributes)) {
+ if (!is_numeric($option) || !is_numeric($value)) {
+ $attributes_pass_check = false;
+ break;
+ } else {
+ $check_query = tep_db_query("select products_attributes_id from " . TABLE_PRODUCTS_ATTRIBUTES . " where products_id = '" . (int)$products_id . "' and options_id = '" . (int)$option . "' and options_values_id = '" . (int)$value . "' limit 1");
+ if (tep_db_num_rows($check_query) < 1) {
+ $attributes_pass_check = false;
+ break;
+ }
+ }
+ }
+ } elseif (tep_has_product_attributes($products_id)) {
+ $attributes_pass_check = false;
+ }
+
+ if (is_numeric($products_id) && is_numeric($qty) && ($attributes_pass_check == true)) {
+ $check_product_query = tep_db_query("select products_status from " . TABLE_PRODUCTS . " where products_id = '" . (int)$products_id . "'");
+ $check_product = tep_db_fetch_array($check_product_query);
+
+ if (($check_product !== false) && ($check_product['products_status'] == '1')) {
+ if ($notify == true) {
+ $new_products_id_in_cart = $products_id;
+ tep_session_register('new_products_id_in_cart');
+ }
- $this->contents[] = array($products_id);
- $this->contents[$products_id] = array('qty' => $qty);
+ if ($this->in_cart($products_id_string)) {
+ $this->update_quantity($products_id_string, $qty, $attributes);
+ } else {
+ $this->reset_checkout();
+ $this->contents[$products_id_string] = array('qty' => (int)$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 ('" . (int)$customer_id . "', '" . tep_db_input($products_id) . "', '" . tep_db_input($qty) . "', '" . date('Ymd') . "')");
+ 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') . "')");
- if (is_array($attributes)) {
- reset($attributes);
- while (list($option, $value) = each($attributes)) {
- $this->contents[$products_id]['attributes'][$option] = $value;
+ if (is_array($attributes)) {
+ reset($attributes);
+ while (list($option, $value) = each($attributes)) {
+ $this->contents[$products_id_string]['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 ('" . (int)$customer_id . "', '" . tep_db_input($products_id) . "', '" . (int)$option . "', '" . (int)$value . "')");
+ 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 . "')");
+ }
+ }
}
+
+ $this->cleanup();
}
- $new_products_id_in_cart = $products_id;
- tep_session_register('new_products_id_in_cart');
}
- $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..
+ $products_id_string = tep_get_uprid($products_id, $attributes);
+ $products_id = tep_get_prid($products_id_string);
- $this->contents[$products_id] = array('qty' => $quantity);
-// update database
- if ($customer_id) tep_db_query("update " . TABLE_CUSTOMERS_BASKET . " set customers_basket_quantity = '" . tep_db_input($quantity) . "' where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($products_id) . "'");
+ if (defined('MAX_QTY_IN_CART') && (MAX_QTY_IN_CART > 0) && ((int)$quantity > MAX_QTY_IN_CART)) {
+ $quantity = MAX_QTY_IN_CART;
+ }
+
+ $attributes_pass_check = true;
if (is_array($attributes)) {
reset($attributes);
while (list($option, $value) = each($attributes)) {
- $this->contents[$products_id]['attributes'][$option] = $value;
+ if (!is_numeric($option) || !is_numeric($value)) {
+ $attributes_pass_check = false;
+ break;
+ }
+ }
+ }
+
+ if (is_numeric($products_id) && isset($this->contents[$products_id_string]) && is_numeric($quantity) && ($attributes_pass_check == true)) {
+ $this->reset_checkout();
+ $this->contents[$products_id_string] = array('qty' => (int)$quantity);
// update database
- if ($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) . "' and products_options_id = '" . (int)$option . "'");
+ 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) . "'");
+
+ if (is_array($attributes)) {
+ reset($attributes);
+ while (list($option, $value) = each($attributes)) {
+ $this->contents[$products_id_string]['attributes'][$option] = $value;
+// update database
+ 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 . "'");
+ }
}
}
}
@@ -126,7 +192,7 @@ function cleanup() {
if ($this->contents[$key]['qty'] < 1) {
unset($this->contents[$key]);
// remove from database
- if ($customer_id) {
+ if (tep_session_is_registered('customer_id')) {
tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET . " where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($key) . "'");
tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . " where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($key) . "'");
}
@@ -135,18 +201,19 @@ function cleanup() {
}
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);
- }
+ $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;
+ }
+
+ return $total_items;
}
function get_quantity($products_id) {
- if ($this->contents[$products_id]) {
+ if (isset($this->contents[$products_id])) {
return $this->contents[$products_id]['qty'];
} else {
return 0;
@@ -154,7 +221,7 @@ function get_quantity($products_id) {
}
function in_cart($products_id) {
- if ($this->contents[$products_id]) {
+ if (isset($this->contents[$products_id])) {
return true;
} else {
return false;
@@ -164,9 +231,10 @@ function in_cart($products_id) {
function remove($products_id) {
global $customer_id;
+ $this->reset_checkout();
unset($this->contents[$products_id]);
// remove from database
- if ($customer_id) {
+ if (tep_session_is_registered('customer_id')) {
tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET . " where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($products_id) . "'");
tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . " where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($products_id) . "'");
}
@@ -178,17 +246,19 @@ function remove_all() {
function get_product_id_list() {
$product_id_list = '';
- if (is_array($this->contents))
- {
+ 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 $currencies;
+
$this->total = 0;
$this->weight = 0;
if (!is_array($this->contents)) return 0;
@@ -198,7 +268,7 @@ function calculate() {
$qty = $this->contents[$products_id]['qty'];
// products price
- $product_query = tep_db_query("select products_id, products_price, products_tax_class_id, products_weight from " . TABLE_PRODUCTS . " where products_id='" . (int)tep_get_prid($products_id) . "'");
+ $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 . "'");
if ($product = tep_db_fetch_array($product_query)) {
$prid = $product['products_id'];
$products_tax = tep_get_tax_rate($product['products_tax_class_id']);
@@ -211,7 +281,7 @@ function calculate() {
$products_price = $specials['specials_new_products_price'];
}
- $this->total += tep_add_tax($products_price, $products_tax) * $qty;
+ $this->total += $currencies->calculate_price($products_price, $products_tax, $qty);
$this->weight += ($qty * $products_weight);
}
@@ -222,9 +292,9 @@ function calculate() {
$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 . "'");
$attribute_price = tep_db_fetch_array($attribute_price_query);
if ($attribute_price['price_prefix'] == '+') {
- $this->total += $qty * tep_add_tax($attribute_price['options_values_price'], $products_tax);
+ $this->total += $currencies->calculate_price($attribute_price['options_values_price'], $products_tax, $qty);
} else {
- $this->total -= $qty * tep_add_tax($attribute_price['options_values_price'], $products_tax);
+ $this->total -= $currencies->calculate_price($attribute_price['options_values_price'], $products_tax, $qty);
}
}
}
@@ -253,11 +323,12 @@ function attributes_price($products_id) {
function get_products() {
global $languages_id;
- if (!is_array($this->contents)) return 0;
+ if (!is_array($this->contents)) return false;
+
$products_array = array();
reset($this->contents);
while (list($products_id, ) = each($this->contents)) {
- $products_query = tep_db_query("select p.products_id, pd.products_name, p.products_model, p.products_price, p.products_weight, p.products_tax_class_id from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd where p.products_id='" . (int)tep_get_prid($products_id) . "' and pd.products_id = p.products_id and pd.language_id = '" . (int)$languages_id . "'");
+ $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 . "'");
if ($products = tep_db_fetch_array($products_query)) {
$prid = $products['products_id'];
$products_price = $products['products_price'];
@@ -271,6 +342,7 @@ function get_products() {
$products_array[] = array('id' => $products_id,
'name' => $products['products_name'],
'model' => $products['products_model'],
+ 'image' => $products['products_image'],
'price' => $products_price,
'quantity' => $this->contents[$products_id]['qty'],
'weight' => $products['products_weight'],
@@ -279,6 +351,7 @@ function get_products() {
'attributes' => (isset($this->contents[$products_id]['attributes']) ? $this->contents[$products_id]['attributes'] : ''));
}
}
+
return $products_array;
}
@@ -294,6 +367,66 @@ function show_weight() {
return $this->weight;
}
+ function generate_cart_id($length = 5) {
+ return tep_create_random_value($length, 'digits');
+ }
+
+ function get_content_type() {
+ $this->content_type = false;
+
+ if ( (DOWNLOAD_ENABLED == 'true') && ($this->count_contents() > 0) ) {
+ reset($this->contents);
+ while (list($products_id, ) = each($this->contents)) {
+ if (isset($this->contents[$products_id]['attributes'])) {
+ reset($this->contents[$products_id]['attributes']);
+ while (list(, $value) = each($this->contents[$products_id]['attributes'])) {
+ $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");
+ $virtual_check = tep_db_fetch_array($virtual_check_query);
+
+ if ($virtual_check['total'] > 0) {
+ switch ($this->content_type) {
+ case 'physical':
+ $this->content_type = 'mixed';
+
+ return $this->content_type;
+ break;
+ default:
+ $this->content_type = 'virtual';
+ break;
+ }
+ } else {
+ switch ($this->content_type) {
+ case 'virtual':
+ $this->content_type = 'mixed';
+
+ return $this->content_type;
+ break;
+ default:
+ $this->content_type = 'physical';
+ break;
+ }
+ }
+ }
+ } else {
+ switch ($this->content_type) {
+ case 'virtual':
+ $this->content_type = 'mixed';
+
+ return $this->content_type;
+ break;
+ default:
+ $this->content_type = 'physical';
+ break;
+ }
+ }
+ }
+ } else {
+ $this->content_type = 'physical';
+ }
+
+ return $this->content_type;
+ }
+
function unserialize($broken) {
for(reset($broken);$kv=each($broken);) {
$key=$kv['key'];
Please sign in to comment.
Something went wrong with that request. Please try again.