betty88 Posted February 22, 2007 Posted February 22, 2007 I know a couple of contributions out there which can remove the quantity box in cart and also limit the same product with same attribute selection to be only added once to the cart. But what I want to do is to have these same products to appear multiple times in the cart instead of one product with its quantity box. I want: Cart Contents Box: Product 1 -- Attribute 1 Product 2 -- Attribute 1 Product 3 -- Attribute 1 Product 1 -- Attribute 1 Product 1 -- Attribute 1 Instead of Cart Contents Box: Product 1 -- Attribute 1 quantity 3 Product 2 -- Attribute 1 quantity 1 Product 3 -- Attribute 1 quantity 1 I changed back and forth the codes in the /class/shopping_cart.php and couldn't figure things out. Can anyone give me some help on this? Thank you.
Velveeta Posted February 22, 2007 Posted February 22, 2007 I know a couple of contributions out there which can remove the quantity box in cart and also limit the same product with same attribute selection to be only added once to the cart. But what I want to do is to have these same products to appear multiple times in the cart instead of one product with its quantity box. I want: Cart Contents Box: Product 1 -- Attribute 1 Product 2 -- Attribute 1 Product 3 -- Attribute 1 Product 1 -- Attribute 1 Product 1 -- Attribute 1 Instead of Cart Contents Box: Product 1 -- Attribute 1 quantity 3 Product 2 -- Attribute 1 quantity 1 Product 3 -- Attribute 1 quantity 1 I changed back and forth the codes in the /class/shopping_cart.php and couldn't figure things out. Can anyone give me some help on this? Thank you. You'll have to do some rewriting of the way the shopping_cart class stores the products themselves... Right now, it sort of serializes the attributes so that say, product_id 37 with attributes 2 and 3 having values 5 and 7 respectively, become this string: 37{2}5{3}7 And then it declares an array index of that value to store the quantity, like this: array('37{2}5{3}7' => '3'); Then, when another product is added to the cart, if it's identical, it's issued the same string, and when the cart sees that that array index already exists, it adds the selected quantity to the quantity already in the cart, so if you add 2 more, it just resets that array to this: array('37{2}5{3}7' => '5'); If you wanted to have each item in its own slot, you'd have to do rewrite the shopping_cart class to store things maybe like this: array(array('37{2}5{3}8' => '5'), array('37{2}5{3}8' => '5'), array('37{2}5{3}8' => '5'), array('37{2}5{3}8' => '5'), array('37{2}5{3}8' => '5')); And then loop through the elements a little different to pull your products for listing... Richard. Richard Lindsey
betty88 Posted February 23, 2007 Author Posted February 23, 2007 Hi Richard, Thank you very much for your detailed instruction. I understand what you mean but I am having trouble locating the right functions in the shopping_cart class file that I should change. Would it be possible for you to show me which part of the codes I should do the modification on? Thanks a lot. Betty
Velveeta Posted February 23, 2007 Posted February 23, 2007 Hi Richard, Thank you very much for your detailed instruction. I understand what you mean but I am having trouble locating the right functions in the shopping_cart class file that I should change. Would it be possible for you to show me which part of the codes I should do the modification on? Thanks a lot. Betty Actually, you know what? Scratch my previous instructions, and try something like this in the shopping_cart class file instead... Find your get_products function, and change it from this: function get_products() { global $languages_id; 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_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']; $specials_query = tep_db_query("select specials_new_products_price from " . TABLE_SPECIALS . " where products_id = '" . (int)$prid . "' and status = '1'"); if (tep_db_num_rows($specials_query)) { $specials = tep_db_fetch_array($specials_query); $products_price = $specials['specials_new_products_price']; } $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'], 'final_price' => ($products_price + $this->attributes_price($products_id)), 'tax_class_id' => $products['products_tax_class_id'], 'attributes' => (isset($this->contents[$products_id]['attributes']) ? $this->contents[$products_id]['attributes'] : '')); } } return $products_array; } To this: function get_products() { global $languages_id; 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_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']; $specials_query = tep_db_query("select specials_new_products_price from " . TABLE_SPECIALS . " where products_id = '" . (int)$prid . "' and status = '1'"); if (tep_db_num_rows($specials_query)) { $specials = tep_db_fetch_array($specials_query); $products_price = $specials['specials_new_products_price']; } for ($x = 0; $x < $this->contents[$products_id]['qty']; $x++) { $products_array[] = array('id' => $products_id, 'name' => $products['products_name'], 'model' => $products['products_model'], 'image' => $products['products_image'], 'price' => $products_price, 'quantity' => 1, 'weight' => $products['products_weight'], 'final_price' => ($products_price + $this->attributes_price($products_id)), 'tax_class_id' => $products['products_tax_class_id'], 'attributes' => (isset($this->contents[$products_id]['attributes']) ? $this->contents[$products_id]['attributes'] : '')); } } } return $products_array; } Just wrap that for loop around the $products_array statement and see if that works for you :) Richard. Richard Lindsey
betty88 Posted February 24, 2007 Author Posted February 24, 2007 Hi Richard, It works like a charm! Thanks a million! :thumbsup: Betty
Velveeta Posted February 24, 2007 Posted February 24, 2007 Hi Richard, It works like a charm! Thanks a million! :thumbsup: Betty Great :) Glad to hear it works :D It's always nice when they're quick additions like that :D Richard. Richard Lindsey
betty88 Posted February 25, 2007 Author Posted February 25, 2007 Hi Richard, I just found that when an item was added to the cart it was not added to the end of the cart but immediately followed the first same item (if existed in the cart already). Is there any way to list all the items in the order they are added to the cart? Also, when removing one of the duplicated items from the cart, it also removed all of the same items in the cart. I was able to modify the remove function to remove only one item from the cart at a time, but still it needs to be further modified to remove the exactly the one the remove button pointing to (same issue as the above). Here is the codes I modified for the remove function: function remove($products_id) { global $customer_id; if ($this->contents[$products_id]['qty'] > 1) { $qty = $this->contents[$products_id]['qty']- 1; $this->contents[$products_id] = array('qty' => $this->contents[$products_id]['qty']- 1); // update database if (tep_session_is_registered('customer_id')) tep_db_query("update " . TABLE_CUSTOMERS_BASKET . " set customers_basket_quantity = '" . $qty . "' where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($products_id_string) . "'"); } else { unset($this->contents[$products_id]); // remove from database 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) . "'"); } I wonder if a new array need be added to add_cart, get_product and remove function, so that it first gets all product ids no matter they are duplicated in the cart or not; function add_cart($products_id, $qty = '1', $attributes = '', $notify = true) { global $new_products_id_in_cart, $customer_id; [u] $x=array(); $x[]=$products_id;[/u] $products_id_string = tep_get_uprid($products_id, $attributes); $products_id = tep_get_prid($products_id_string); $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; } } } then display all of these items in the order they are added in the cart; function get_products() { global $languages_id; if (!is_array($this->contents)) return false; $products_array = array(); reset($this->contents); [b]for ( $u =0; $u< count($x); $u++){[/b] $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']; $specials_query = tep_db_query("select specials_new_products_price from " . TABLE_SPECIALS . " where products_id = '" . (int)$prid . "' and status = '1'"); if (tep_db_num_rows($specials_query)) { $specials = tep_db_fetch_array($specials_query); $products_price = $specials['specials_new_products_price']; } [b] $products_array[] = array('id' => $x[$u],[/b] 'name' => $products['products_name'], 'model' => $products['products_model'], 'image' => $products['products_image'], 'price' => $products_price, 'quantity' => 1, 'weight' => $products['products_weight'], // 'final_price' => ($products_price + $this->attributes_price($products_id)), 'tax_class_id' => $products['products_tax_class_id'], 'attributes' => (isset($this->contents[$products_id]['attributes']) ? $this->contents[$products_id]['attributes'] : '')); } } finally when removing products, the remove function calculates the index of the $x for the particular product to be removed. However, I do not know how. Could you please give me some hints on that? Thank you. Betty
Recommended Posts
Archived
This topic is now archived and is closed to further replies.