eTiMaGo Posted April 12, 2008 Posted April 12, 2008 Hello people, I hope someone can help me because I am STUMPED :'( I'm working on a site for dedicated/colocation server rental. The customer must have the option of choosing how many months he will rent the server(s) from, and how many of each type. So, I essentially need 2 different product quantities to be used for each item in the shopping cart and order. I realize I could do this using attributes, but some items use them for options, so calculations would become incorrect. (unless there's a way to set one type of attribute as a multiplier?) Anyway what I am trying to do is duplicate all the the functions that relate to the standard customers_basket_quantity, by following the code starting from clicking "add to cart" in the product_info page. Most of these functions are handled in application_top with references to the shopping_cart and currency classes, all of which have been modified. I have also of course created a customers_basket_quantity2 field in the customers_basket database ;) So far, everything seems to work OK, the correct values are added to the database then show up in the shopping cart. I can add several items this way. But, after that, if I try to change quantities from the shopping cart, everything gets messed up! Since a picture is worth a thousand words... The site's in Thai, so ignore that :P Here is the "before" pic. I manually entered all those values. After pressing "update": As you can see, it seems like some problem with the data that is POST'ed from the shopping cart page here's the part of application_top that does the updating: case 'update_product' : for ($i=0, $n=sizeof($HTTP_POST_VARS['products_id']); $i<$n; $i++) { if (in_array($HTTP_POST_VARS['products_id'][$i], (is_array($HTTP_POST_VARS['cart_delete']) ? $HTTP_POST_VARS['cart_delete'] : array()))) { $cart->remove($HTTP_POST_VARS['products_id'][$i]); } else { if (PHP_VERSION < 4) { // if PHP3, make correction for lack of multidimensional array. reset($HTTP_POST_VARS); while (list($key, $value) = each($HTTP_POST_VARS)) { if (is_array($value)) { while (list($key2, $value2) = each($value)) { if (ereg ("(.*)\]\[(.*)", $key2, $var)) { $id2[$var[1]][$var[2]] = $value2; } } } } $attributes = ($id2[$HTTP_POST_VARS['products_id'][$i]]) ? $id2[$HTTP_POST_VARS['products_id'][$i]] : ''; } else { $attributes = ($HTTP_POST_VARS['id'][$HTTP_POST_VARS['products_id'][$i]]) ? $HTTP_POST_VARS['id'][$HTTP_POST_VARS['products_id'][$i]] : ''; } $cart->add_cart($HTTP_POST_VARS['products_id'][$i], $HTTP_POST_VARS['cart_quantity'][$i], $HTTP_POST_VARS['cart_quantity2'][$i], $attributes, false); } } tep_redirect(tep_href_link($goto, tep_get_all_get_params($parameters))); break; I modified the shopping cart class function add_cart and update_quantity to accept the second quantity variable (add_cart seems to work fine, there's no problem when adding an item from the product_info page): function add_cart($products_id, $qty = '1', $qty2 = '1', $attributes = '', $notify = true) { global $new_products_id_in_cart, $customer_id; // BOF qpbpp $product_info_query = tep_db_query("select products_qty_blocks from " . TABLE_PRODUCTS . " where products_id = '" . (int)$products_id . "'"); $product_info = tep_db_fetch_array($product_info_query); $pf = new PriceFormatter; $qty = $pf->adjustQty($qty, $product_info['products_qty_blocks']); // EOF qpbpp $products_id_string = tep_get_uprid($products_id, $attributes); $products_id = tep_get_prid($products_id_string); 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)) { reset($attributes); while (list($option, $value) = each($attributes)) { if (!is_numeric($option) || !is_numeric($value)) { $attributes_pass_check = false; break; } } } 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'); } if ($this->in_cart($products_id_string)) { $this->update_quantity($products_id_string, $qty, $qty2, $attributes); } else { $this->contents[$products_id_string] = array('qty' => (int)$qty, 'qty2' => (int)$qty2); // insert into database if (tep_session_is_registered('customer_id')) tep_db_query("insert into " . TABLE_CUSTOMERS_BASKET . " (customers_id, products_id, customers_basket_quantity, customers_basket_quantity2, customers_basket_date_added) values ('" . (int)$customer_id . "', '" . tep_db_input($products_id_string) . "', '" . (int)$qty . "', '" . (int)$qty2 . "', '" . date('Ymd') . "')"); if (is_array($attributes)) { reset($attributes); while (list($option, $value) = each($attributes)) { $this->contents[$products_id_string]['attributes'][$option] = $value; // insert into database 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(); // assign a temporary unique ID to the order contents to prevent hack attempts during the checkout procedure $this->cartID = $this->generate_cart_id(); } } } function update_quantity($products_id, $quantity = '', $quantity2 = '', $attributes = '') { global $customer_id; $products_id_string = tep_get_uprid($products_id, $attributes); $products_id = tep_get_prid($products_id_string); 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)) { 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->contents[$products_id_string] = array('qty' => (int)$quantity, 'qty2' => (int)$quantity2); // update database if (tep_session_is_registered('customer_id')) tep_db_query("update " . TABLE_CUSTOMERS_BASKET . " set customers_basket_quantity = '" . (int)$quantity . "', customers_basket_quantity2 = '" . (int)$quantity2 . "' 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 . "'"); } } } } I'm quite sure the problem is either from that part of application_top, or the part of the add_cart function that passes on the variables to the update_quantity function, but I've been over the code over and over again and can't seem to find any glaring problems! (that, and I suck whhen it comes to arrays :D) So, if anyone can see anything that could be changed, please please let me know!!!
Guest Posted April 12, 2008 Posted April 12, 2008 I am sure it can be done but there are software packages specifically designed for hosting. They take care of all that for you and set up the server for you. Have you looked at those packages. They do cost $$$ but since they are designed for the job you want to do they will fulfill all your needs in a much easier way. They handle billing, options (like you want to do), reminder notices, terminating contracts, and setting up customer accounts all without touching a button for the hosting company. They would suit your needs much better than osC. I don't know if they can do all that on dedicated servers also but I know they work for shared and possibly VPS as well. I know that wasn't the answer you wanted but wanted to make sure you had looked at ALL your options before finishing your site then finding out about alternatives. Have you looked at Customer Computer Creator or similar type contributions as they allow you to have various types of options but I am not sure it will do what you need it to but in one of the option/attribute contributions there has to be something already done that might be similar. I haven't really needed one so haven't looked at them to closely. I would give another look through the contributions. Sorry I don't have the answer you wanted.
eTiMaGo Posted April 13, 2008 Author Posted April 13, 2008 Thanks Peter for the advice The thing is, the site is pretty much complete except for this function, so I really don't wanna start aaaaaall over again. But anyway, it does not matter, because I fixed it :D Turns out, when I duplicated the text entry box in the shopping cart, i also left in place the part that POSTs the product ID. So, the HTTP_POST_VARS array had twice as many product_ids as quantities, as a result only one out of two would get processed properly. Doh! So it's all good now, yeay! On to the next problem! :D
Recommended Posts
Archived
This topic is now archived and is closed to further replies.