Jump to content

Shopping Cart


Archadian28

Recommended Posts

Can anyone help on this issue? I have tried everything. If anyone has the PHP Shopping Cart project files just give an example off of that and i'll recode it to fit mine. I just need to see an example of updating and removing. If anyone could point me in the right direction it will be greatly appreciated. Thanks.

Link to comment
Share on other sites

I don't really have the time at the moment to actually write this code for you, but I can point you in the right direction.

 

-- For removing items from the shopping cart, you'd need to edit the render_shopping_cart_row() function within templates.php. Next to each item, you'd need to add a "remove" text link (or link surrounding an image that indicated that the product would be removed from the cart). That would link to a page called "remove.php", and you would pass along the id of the product you wanted to remove in the URL like this: remove.php?id=ITEM_ID.

 

Within remove.php, you would use $_GET[] to get the value of "id" from the URL. You probably need to add a function within ShoppingCart.php that would then take that id, access the $items array within the Shopping Cart object, and remove any items from that array with the specified id (probably with the unset() function: http://php.net/manual/en/function.unset.php). Once that is completed and the item removed, you could redirect the user back to the shopping cart page.

 

-- For updating the number of items that a user is purchasing of a specific product, you would probably need to update the render_shopping_cart_row() function. Rather than simply displaying the number of items from a specific product that the person is purchasing, you would need to put that value inside an input box, allowing the user to edit it. Something like this:

 

<td><input name='numitems_$productid' type="text" value='$quantity' /></td>

Then you'd need to add a submit button (and obviously make sure that all this is contained within <form></form> tags) so the user has something to submit the changes. Once the form submits, you would go through each of the inputs, grabbing the number in each form element and updating the $items array in the shopping cart with the correct values. Assuming the value isn't 0 (in which case you'd want to remove the item from the cart) you would need to probably create a new function ("setNumItems($product_id, $num)"?) within ShoppingCart.php that update the $items array -- something like this:

 

public function setNumItems($product_id, $num)
{
  if ($num != 0)
  {
     $this->items[$product_id] = $num;
  }
  else
  {
     $this->remove($product_id); // I'm assuming you've created a remove() function that accepts the product_id that you want to remove.
  }
}

Once that is all completed, redirect them back to the cart page.

 

Hope that helps get you started?

Link to comment
Share on other sites

The remove works perfectly and i had to use javascript to get the new quantity entered. You can't put a form inside of a form so i had to use the onchange="" inside the input field. Pass the form.id value to a javascript function then window.location to a php file. The price doesn't show up correctly when you put in a new quantity and it seems like only the first item updates. Why would that happen?

Link to comment
Share on other sites

Here is the code:

 


<input id='qnty' name='$row->token' type='text' size='3' onchange='sendVal(this.id, this.name)' value='$qnty' /> //text field to change the quantity

------------

function sendVal(id, name) { // gets the value and token from the row for the quantity

  var getQnty = parseInt(document.getElementById(id).value);
  var token = document.getElementById(id).name;

  window.location = "index.php?page=addtocart&qnty=" + getQnty + "&token=" + token;

 } 

-------------------

public function updQnty($pid, $qnty) { // In the shopping cart class 

if ($qnty != 0) {

	$this->scart[$pid] = $qnty;

}
else {

	$this->remove($pid);

}

}

----------------

$shopcart = getCart(); 

if (isset($_GET['qnty'])) { // in addtocart file that the javascript function for the input field redirects to, this code should update

$num = $_GET['qnty'];
   $token = $_GET['token'];

   unset($_GET['qnty']);
   unset($_GET['token']);

   if (item_exists($token) && ($num <= 10) && is_numeric($num)) {

   	$shopcart->updQnty($token, $num);
	setCart($shopcart);

       ?>

   	<script type="text/javascript">

           alert('Quantity updated successfully.');

           window.location = "javascript: history.go(-1)";

       </script>

   <?php

   }
   else {
   ?>
   	<script type="text/javascript">

           alert('That item does not exist or the quantity was too high.');

           window.location = "javascript: history.go(-1)";

       </script>

   <?php
   }


}

-----------------------

This is the full code for my templates for the Shopping Cart. Maybe its something I am over looking:

function shoppingCart_Row(ShoppingCart $shopcart , $pid, $counter) {

global $db;

$db->query = "SELECT title, body, price, token FROM products WHERE token='$pid'";
$result = $db->sql_query($db->query);
$row = $db->fetch_object($result);

$qnty = $shopcart->getItemQnty($row->token);
$price = $row->price;
$total = number_format(($qnty * $price), 2);

return "

	<tr bgcolor='#2C2C2C'>
		<td align='left' width='65%'>
			$row->title
			<input type='hidden' name='item_name_$counter' value='$row->title' />
		</td>
		<td width='10%'><div class='remove'><a href='index.php?page=addtocart&action=remove&id=$row->token'>Remove</a></div></td>
		<td>
			<input id='qnty' name='$row->token' type='text' size='3' onchange='sendVal(this.id, this.name)' value='$qnty' />
			<input type='hidden' name='quantity_$counter' value='$qnty' />
		</td>
		<td>
			$$total
			<input type='hidden' name='amount_$counter' value='$price' />
		</td>
	</tr>

";

}

function showTotal(ShoppingCart $shopcart) {

global $db;

foreach ($shopcart->GetItems() as $key) {

       $db->query = "SELECT price, token FROM products WHERE token='$key'";
       $result = $db->sql_query($db->query);
       $row = $db->fetch_object($result);

       $qnty = $shopcart->getItemQnty($row->token);
       $total = number_format(($qnty * $row->price), 2);
       $grdTotal += $total;

}

return '<tr>

			<td colspan="2"></td>

			<td><b>Total:</b></td>

			<td><b>$' . number_format($grdTotal, 2) . '</b>

			</td>

		</tr>';

}

function exec_shopcart() {

$shopcart = getCart();

$output = '

	<tr>

		<th colspan="2" width="75%"><b>Items</b></th>

		<th width="10%"><b>Quantity</b></th>

		<th width="15%"><b>Amount</b></th>

	</tr>

';

$counter = 1;

foreach ($shopcart->GetItems() as $pid) {

	$output .= shoppingCart_Row($shopcart , $pid, $counter);

	$counter++;
}

$output .= showTotal($shopcart);

$output .= "</table>";

return $output;

}

 

 

the token is stored in the database and its a md5() conversion that references the title, description, image and price of each product. Thanks for the help!

Link to comment
Share on other sites

ok i fixed the total for now anyways heres the code....but only the first item in the cart is able to be updated the others i try to update the quantity and it doesn't work but it says its been updated.

 

displaying correct total fixed:

 


function showTotal(ShoppingCart $shopcart) {

$total = $shopcart->getTotal();

return '<tr>

			<td colspan="2"></td>

			<td><b>Total:</b></td>

			<td><b>$' . $total . '</b>

			</td>

		</tr>';

}

------------------------------

public function getTotal() {

	global $db;

	foreach ($this->GetItems() as $key) {

		$db->query = "SELECT price FROM products WHERE token='$key'";
		$result = $db->sql_query($db->query);
		$row = $db->fetch_object($result);

		$total += ($row->price * $this->getItemQnty($key));

	}

	return number_format($total, 2);

}

Link to comment
Share on other sites

I tested the link you PMed me, and I'm not having any issues when the first item is added to cart. However, when I have added two different items to the cart, I can't seem to update the quantity on the second item. As far as I can tell, I'm not having any issues with bad math so far.

 

Have you gone through and tested that all the variables are what you are expecting? Either echoing out the values or using an alert() for the Javascript variables?

 

One question... In the "remove.php" file (or whatever you called it) I notice that you are checking to see if the token exists:

 

item_exists($token)

 

does that actually check to see if the item exists in the database, or check to see if it is currently in the cart? If it is only checking against the database, then perhaps the id is wrong and you are getting a "success" message when that particular item isn't actually in the cart?

 

As a random sidenote, it would be really helpful to have some sort of feedback when adding an item to the cart -- either an alert() or a message that appears within the page. At the moment, there seems to be no way of checking if the item actually was added to the cart successfully without visiting the cart page.

Link to comment
Share on other sites

yeah its still in the testing stages. a confirmation will be seen when they add items...having gotten to that yet lol. trying to figure out why only the first item in the shopping cart is able to have its quantity updated but not the rest. As far as item_exists here is the code:

 


function item_exists($token) {

global $db;

if (strlen($token) == 32) {

	$db->query = "SELECT token FROM products";
	$result = $db->sql_query($db->query);

	while($row = $db->fetch_object($result)) {

		if ($row->token == htmlentities(addslashes($token))) {

			return true;

		}

	}

	return false;

}
else {

	send_error_loc("TOKEN ERROR", "The token and/or the product doesn't exist.");

}

}

 

Ok im checking both now. Still can't update the quantity on the 2nd row and down....only the first one.

Link to comment
Share on other sites

Ok, im using javascript (scroll up to code) to send the id='qnty' and name='$row->token' to the javascript function sendVal() in the input field. When i add the first item to the shopping cart it sends that quantity and token ($row->token) to the variables in the javascript function and saves them there. So no matter what i add afterwards, the var getQnty and var token have the info from the first item in the shopping cart...thats why all the others aren't updating the quantity. It shows the Quantity of the first item on all the others...same for the token. Any solutions to this? Thanks.

Link to comment
Share on other sites

Ben you are a genius lol. I wrote this at 3am this morning. I should have caught that.

 

I changed this:

 

<input id='qnty' name='$row->token' type='text' size='3' onchange='sendVal(this.id, this.name)' value='$qnty' />

 

to this:

 

<input id='$row->token' name='qnty' type='text' size='3' onchange='sendVal(this.id)' value='$qnty' />

 

and i changed this:

 


function sendVal(id, name) {

         var getQnty = parseInt(document.getElementById(id).value);
         var token = document.getElementById(id).name;

         window.location = "index.php?page=addtocart&qnty=" + getQnty + "&token=" + token;

 } 

 

to this:

 


function sendVal(id) {

  var getQnty = document.getElementById(id).value;
  var token = document.getElementById(id).id;

  window.location = "index.php?page=cart&qnty=" + getQnty + "&token=" + token;

 }  

 

It works perfectly. Thanks for your help Ben. :)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...