Jump to content

Updating entire shopping cart, rather than just one item


Dakkadakka

Recommended Posts

I made a fully working shopping cart, but the problem is that in my line of business, customers may order a hundered items, and having to adjust quantities line by line could be a big hassle. My cart includes suggestions based on customer history due to the tremendous amount of things people typically buy. while($row=mysql_fetch_array($result)){//We grab the product id as well as everything else we need.$product_id= $row['product_id'];$sales_info = $row['sales_info'];$sku= $row['sku'];$qty= $row['qty'];$final_price = $row['final_price']; $price = $row['price'];$purchase_cost = $row['purchase_cost'];$link = $row['image_path']; //Get the price and final price in the true format.if ($price == 0){$dollar_price = "Listing Price:<br>".number_format($purchase_cost *= (1+$no_avg_markup/100),2);$dollar_final_price = "Contact your Sales Rep";}else{$dollar_price = "$".number_format($price, 2);$dollar_final_price = "$".number_format($final_price,2);} echo '<tr> <td height="110"><table width="159" border="0"> <tr> <td width="153">Sku#:'.$sku.'</td> </tr> <tr> <td height="84"> '; if (!is_null($link)){echo'<img src="/'.$link.'" alt="" width="35%" height="45%" />';}else{echo'<img src="/imagen/imagecomingsoon.png" alt="" width="35%" height="45%" />';}echo ' </td> </tr> </table></td> <td>'.strtoupper($sales_info).'</td> <td>'.$dollar_final_price.'</td> <td><form id="update'.$product_id.'" name="update'.$product_id.'" method="post" action="updateitem.php?item_id='.$product_id.'&cart_id='.$cart_id.'&customer_id ='.$customer_id.'"> <input name="qty" type="text" id="textfield" value="'.$qty.'"size="4" /> <input name="button" type="submit" id="button" value="Update" /></form></td>'; if ($price == 0){echo '<td>Contact Sales Rep</td>';}else{echo '<td>'.$dollar_price.'</td>';} echo'<td><a href="deleteitem.php?item_id='.$product_id.'&cart_id='.$cart_id.'&customer_id ='.$customer_id.'"><img src="/imagen/icon/deletered.png" width="42" height="47" /></a></td> </tr>'; $sub_total = $sub_total + $final_price; } ?> In this, each row generates an individual form for that one item. At the end of each row is a button to add that one item to the cart. I can't seem to find any tutorials on designing an Update All button. How do I make the entire cart, and an Update All button, that will work in this fashion?

Link to comment
Share on other sites

Everytime your form is submitted your entire page refreshes. Just code your update into updateitem.php.

Link to comment
Share on other sites

Everytime your form is submitted your entire page refreshes. Just code your update into updateitem.php.
I don't know how to make the form do that. If there are a variable number of items in the cart, how do I make one form for all of them, and how do I make the PHP check them all? I can't seem to find examples anywhere. I know carts have done it before.
Link to comment
Share on other sites

Talk to the person that wrote updateitem.php or learn how to do it yourself. Learning how to do it yourself is what the w3shool forums are all about. How much php do you know?

Edited by niche
Link to comment
Share on other sites

Talk to the person that wrote updateitem.php or learn how to do it yourself. Learning how to do it yourself is what the w3shool forums are all about. How much php do you know?
I wrote update cart.php. It's a very simple script that checks the quantity of the row item and it updates the respective row on the database. I've been improving my PHP all summer at my paid internship but am stuck on this problem. //Data has been retrieved at this point. // database connectionmysql_connect("localhost", $dbuser, $dbpass) or die(mysql_error());mysql_select_db($dbname) or die("Unable to select database"); $query = "SELECT * FROM cart_line WHERE cart_id = '".$cart_id."' AND product_id = '".$product_id."'";//echo $query."<br>";$search = mysql_query($query) or die(mysql_error()); while($row=mysql_fetch_array($search)){$price = $row['price'];//echo "Found a unit price of ".$price."<br>";$final_price = $row['final_price'];}$final_price = $price * $qty;//echo "New Final Price is ".$price." * ".$qty." = ".$final_price."<br>";$update_query = "UPDATE cart_line SET qty='".$qty."', final_price = '".$final_price."' WHERE cart_id = '".$cart_id."' AND product_id = '".$product_id."'";//echo $update_query."<br>";$update= mysql_query($update_query) or die(mysql_error()); //return to the cart.header ("Location: index.php");?> EDIT - I finally found a good example of what I'm trying to do. I'm going to start applying it unless there are better suggestions.http://www.theblog.ca/update-multiple-rows-mysql Edited by Dakkadakka
Link to comment
Share on other sites

That's good news. The solution to your problem could be as simple as adding a query to updateitem.php that makes the needed updates. (You might have to put it in a while loop.)

Link to comment
Share on other sites

take the form defination outside of the loop. generate the item list with radio button with items data inside while loop. so people can use one form for any arbitrary number of list. you can also set a button to loop through each radio button and mark them selected using JS DOM operation which will act like "select all" button

Edited by birbal
Link to comment
Share on other sites

take the form defination outside of the loop. generate the item list with radio button with items data inside while loop. so people can use one form for any arbitrary number of list. you can also set a button to loop through each radio button and mark them selected using JS DOM operation which will act like "select all" button
This, and the tutorial, worked like a charm. Thanks. I didn't know i could do that. I was hesitant to try it and it worked on,like, the third try. This was the key foreach($_POST['item_id'] as $key=>$item_id){ $qty = $_POST['qty'][$key];$product_id = $_POST['product_id'][$key];$customer_id = $_POST['customer_id'][$key]; //Carry out the individual item query like usual.}
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...