Created
April 27, 2012 14:28
-
-
Save mgirouard/2509711 to your computer and use it in GitHub Desktop.
ZenCart Shopping Cart Class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Method to handle cart Action - add product | |
* | |
* @param string forward destination | |
* @param url parameters | |
*/ | |
function actionAddProduct($goto, $parameters) { | |
global $messageStack, $db; | |
if (isset($_POST['products_id']) && is_numeric($_POST['products_id'])) { | |
// verify attributes and quantity first | |
$the_list = ''; | |
$adjust_max= 'false'; | |
if (isset($_POST['id'])) { | |
foreach ($_POST['id'] as $key => $value) { | |
$check = zen_get_attributes_valid($_POST['products_id'], $key, $value); | |
if ($check == false) { | |
$the_list .= TEXT_ERROR_OPTION_FOR . '<span class="alertBlack">' . zen_options_name($key) . '</span>' . TEXT_INVALID_SELECTION . '<span class="alertBlack">' . (zen_values_name($value) == 'TEXT' ? TEXT_INVALID_USER_INPUT : zen_values_name($value)) . '</span>' . '<br />'; | |
} | |
} | |
} | |
// verify qty to add | |
// $real_ids = $_POST['id']; | |
//die('I see Add to Cart: ' . $_POST['products_id'] . 'real id ' . zen_get_uprid($_POST['products_id'], $real_ids) . ' add qty: ' . $add_max . ' - cart qty: ' . $cart_qty . ' - newqty: ' . $new_qty); | |
$add_max = zen_get_products_quantity_order_max($_POST['products_id']); | |
$cart_qty = $this->in_cart_mixed($_POST['products_id']); | |
$new_qty = $_POST['cart_quantity']; | |
//echo 'I SEE actionAddProduct: ' . $_POST['products_id'] . '<br>'; | |
$new_qty = $this->adjust_quantity($new_qty, $_POST['products_id'], 'shopping_cart'); | |
if (($add_max == 1 and $cart_qty == 1)) { | |
// do not add | |
$new_qty = 0; | |
$adjust_max= 'true'; | |
} else { | |
// adjust quantity if needed | |
if (($new_qty + $cart_qty > $add_max) and $add_max != 0) { | |
$adjust_max= 'true'; | |
$new_qty = $add_max - $cart_qty; | |
} | |
} | |
if ((zen_get_products_quantity_order_max($_POST['products_id']) == 1 and $this->in_cart_mixed($_POST['products_id']) == 1)) { | |
// do not add | |
} else { | |
// process normally | |
// bof: set error message | |
if ($the_list != '') { | |
$messageStack->add('product_info', ERROR_CORRECTIONS_HEADING . $the_list, 'caution'); | |
// $messageStack->add('header', 'REMOVE ME IN SHOPPING CART CLASS BEFORE RELEASE<br/><BR />' . ERROR_CORRECTIONS_HEADING . $the_list, 'error'); | |
} else { | |
// process normally | |
// iii 030813 added: File uploading: save uploaded files with unique file names | |
$real_ids = isset($_POST['id']) ? $_POST['id'] : ""; | |
if (isset($_GET['number_of_uploads']) && $_GET['number_of_uploads'] > 0) { | |
/** | |
* Need the upload class for attribute type that allows user uploads. | |
* | |
*/ | |
include(DIR_WS_CLASSES . 'upload.php'); | |
for ($i = 1, $n = $_GET['number_of_uploads']; $i <= $n; $i++) { | |
if (zen_not_null($_FILES['id']['tmp_name'][TEXT_PREFIX . $_POST[UPLOAD_PREFIX . $i]]) and ($_FILES['id']['tmp_name'][TEXT_PREFIX . $_POST[UPLOAD_PREFIX . $i]] != 'none')) { | |
$products_options_file = new upload('id'); | |
$products_options_file->set_destination(DIR_FS_UPLOADS); | |
$products_options_file->set_output_messages('session'); | |
if ($products_options_file->parse(TEXT_PREFIX . $_POST[UPLOAD_PREFIX . $i])) { | |
$products_image_extension = substr($products_options_file->filename, strrpos($products_options_file->filename, '.')); | |
if ($_SESSION['customer_id']) { | |
$db->Execute("insert into " . TABLE_FILES_UPLOADED . " (sesskey, customers_id, files_uploaded_name) values('" . zen_session_id() . "', '" . $_SESSION['customer_id'] . "', '" . zen_db_input($products_options_file->filename) . "')"); | |
} else { | |
$db->Execute("insert into " . TABLE_FILES_UPLOADED . " (sesskey, files_uploaded_name) values('" . zen_session_id() . "', '" . zen_db_input($products_options_file->filename) . "')"); | |
} | |
$insert_id = $db->Insert_ID(); | |
$real_ids[TEXT_PREFIX . $_POST[UPLOAD_PREFIX . $i]] = $insert_id . ". " . $products_options_file->filename; | |
$products_options_file->set_filename("$insert_id" . $products_image_extension); | |
if (!($products_options_file->save())) { | |
break; | |
} | |
} else { | |
break; | |
} | |
} else { // No file uploaded -- use previous value | |
$real_ids[TEXT_PREFIX . $_POST[UPLOAD_PREFIX . $i]] = $_POST[TEXT_PREFIX . UPLOAD_PREFIX . $i]; | |
} | |
} | |
} | |
$this->add_cart($_POST['products_id'], $this->get_quantity(zen_get_uprid($_POST['products_id'], $real_ids))+($new_qty), $real_ids); | |
// iii 030813 end of changes. | |
} // eof: set error message | |
} // eof: quantity maximum = 1 | |
if ($adjust_max == 'true') { | |
// $messageStack->add_session('shopping_cart', ERROR_MAXIMUM_QTY . ' B: - ' . zen_get_products_name($_POST['products_id']), 'caution'); | |
$messageStack->add_session('shopping_cart', ERROR_MAXIMUM_QTY . zen_get_products_name($_POST['products_id']), 'caution'); | |
} | |
} | |
if ($the_list == '') { | |
// no errors | |
// display message if all is good and not on shopping_cart page | |
if (DISPLAY_CART == 'false' && $_GET['main_page'] != FILENAME_SHOPPING_CART) { | |
$messageStack->add_session('header', SUCCESS_ADDED_TO_CART_PRODUCT, 'success'); | |
} | |
zen_redirect(zen_href_link($goto, zen_get_all_get_params($parameters))); | |
} else { | |
// errors - display popup message | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Method to handle cart Action - multiple add products | |
* | |
* @param string forward destination | |
* @param url parameters | |
* @todo change while loop to a foreach | |
*/ | |
function actionMultipleAddProduct($goto, $parameters) { | |
global $messageStack; | |
$addCount = 0; | |
header('content-type: text/plain');print_r($_POST);exit; | |
if (is_array($_POST['products_id']) && sizeof($_POST['products_id']) > 0) { | |
while ( list( $key, $val ) = each($_POST['products_id']) ) { | |
if ($val > 0) { | |
$adjust_max = false; | |
$prodId = preg_replace('/[^0-9a-f:.]/', '', $key); | |
$qty = $val; | |
$add_max = zen_get_products_quantity_order_max($prodId); | |
$cart_qty = $this->in_cart_mixed($prodId); | |
// $new_qty = $qty; | |
//echo 'I SEE actionMultipleAddProduct: ' . $prodId . '<br>'; | |
$new_qty = $this->adjust_quantity($qty, $prodId, 'shopping_cart'); | |
if (($add_max == 1 and $cart_qty == 1)) { | |
// do not add | |
$adjust_max= 'true'; | |
} else { | |
// adjust quantity if needed | |
if (($new_qty + $cart_qty > $add_max) and $add_max != 0) { | |
$adjust_max= 'true'; | |
$new_qty = $add_max - $cart_qty; | |
} | |
$this->add_cart($prodId, $this->get_quantity($prodId)+($new_qty)); | |
$addCount++; | |
} | |
if ($adjust_max == 'true') { | |
// $messageStack->add_session('shopping_cart', ERROR_MAXIMUM_QTY . ' C: - ' . zen_get_products_name($prodId), 'caution'); | |
$messageStack->add_session('shopping_cart', ERROR_MAXIMUM_QTY . zen_get_products_name($prodId), 'caution'); | |
} | |
} | |
} | |
// display message if all is good and not on shopping_cart page | |
if ($addCount && DISPLAY_CART == 'false' && $_GET['main_page'] != FILENAME_SHOPPING_CART) { | |
$messageStack->add_session('header', SUCCESS_ADDED_TO_CART_PRODUCTS, 'success'); | |
} | |
zen_redirect(zen_href_link($goto, zen_get_all_get_params($parameters))); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* 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; | |
} | |
$qty = $this->adjust_quantity($qty, $products_id, 'shopping_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' => (float)$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) . "')"); | |
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'. (int)$val . "', '" . (int)$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 . "', '" . (int)$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'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment