rfwoolf Posted May 25, 2011 Share Posted May 25, 2011 Does anybody have any idea how I would go about making this happen? In application_top.php it will try to UPDATE a product when you add it to your cart the second time - but if I get it to INSERT the product, would it work? Any advice? Thanks! :) Link to comment Share on other sites More sharing options...
rfwoolf Posted May 25, 2011 Author Share Posted May 25, 2011 Listening [Actually I posted this reply by mistake, then edited to so I don't look like a complete moron...] Link to comment Share on other sites More sharing options...
rfwoolf Posted May 26, 2011 Author Share Posted May 26, 2011 I have been fighting with this for several hours now and I think I understand the problem - but don't know the solution... The $cart class stores the shopping cart in an array called $contents : $cart->contents When you add a product to the cart, *IF* it's already in the cart, *THEN it *updates* the existing item's quantity, *ELSE* it adds a new product to the $contents So you just comment out the 'update' part, right? That would force it to keep on adding *new* items to the cart -- exactly what we want. BUT there's a problem! In php arrays, you cannot have duplicate keys, this means if you INSERT into $contents a new array key of your product "123" and then fill that array with the products data, and then you try and INSERT again, it will *OVERWRITE* the existing entry!!!! Effectively, inserting a product twice, over-writes the existing product :( Link to comment Share on other sites More sharing options...
rfwoolf Posted May 26, 2011 Author Share Posted May 26, 2011 Okay I found a solution - just needs some proper and thorough testing. As per my previous post, you need to make every product added to $cart->content unique, so that it doesn't *update* the product every time. So in the add_cart function, I've done this: CHANGE THIS: $products_id_string = tep_get_uprid($products_id, $attributes); TO THIS: $products_id_string = tep_get_uprid($products_id, $attributes) . '[' . (string)count($this->contents) .']'; This takes the product ID e.g."28{8}17{7}15{9}19", and adds a COUNT of the existing items in your cart, so for example: 28{8}17{7}15{9}19[0] (if your cart was empty) 28{8}17{7}15{9}19[1] (after adding again) 28{8}17{7}15{9}19[2] (after adding again) 11[3] (a new product was added to the cart!) 28{8}17{7}15{9}19[4] (after adding again) This way, every item in your cart is uniquely identified, and can still be 'traced' in your catalogue because if you say int($products_id_string) it takes the int at the beginning, i.e. 28. Similarly, elsewhere in the code it will need to do something with the product ID, and int($product_id) fetches only the first part :D Okay - I'm off to do some testing... Link to comment Share on other sites More sharing options...
rfwoolf Posted May 26, 2011 Author Share Posted May 26, 2011 Well there are some minor problems... First: by appending '[X]' to the end of our product names to make it unique (where X is a count of cart items), if the customer is signed in, this new productID is also stored into the tables in add_cart and remove: TABLE_CUSTOMERS_BASKET and TABLE_CUSTOMERS_BASKET_ATTRIBUTES. I will have to test if this gives any problems later in the process, like at checkout. Second, there's a problem on the shopping_cart.php page if you make changes to the quantity and click "Update". However, *removing* items seems to work just fine. The update button calls the action "update_product" which is in application_top.php update_product then calls cart_add (which is in classes/shopping_cart.php), but with the NEW PRODUCT ID (with the extra '[X]', and that's the problem: cart_add performs a bunch of checks on the product_ID (like where it is active and in stock etc) and tries to explode the products attributes (which now have an extra [X] at the end. Link to comment Share on other sites More sharing options...
rfwoolf Posted May 26, 2011 Author Share Posted May 26, 2011 Another problem with my solution: Appending '[' . (string)count($this->contents) .']'; to the end of $products_id_string puts the count of the number of items in the cart. But it should really be a unique number - so count($this->contents) should really be something like FetchNextID Link to comment Share on other sites More sharing options...
rfwoolf Posted June 5, 2011 Author Share Posted June 5, 2011 Just a note: in the end I abandoned this idea, although someone that was better at PHP would probably have been able to finish. The whole idea shouldn't be necessary. I thought it was necessary for me because I was not using the Qty like a normal site - my Qty was my weight, e.g. 0.1, so if I add 0.1 and 0.5 I want it to list 0.1 and 0.5, not 0.6. So I found a fix that allows you to use weight as an ATTRIBUTE, allowing you to keep the Qty. So now if I add 1 x 0.1 and 1 x 0.5 it lists them separately anyway, because items with two different attributes get listed separately. So I guess another workaround for this problem is to simply add an attribute for the Qty that the user adds. Example: If they put 0.5 as their quantity, it will somehow add an attribute with value '0.5' (which you can hide from the user or show and just ignore it). Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.