Jump to content


store cart help!


19 replies to this topic

#1 tiki

    Young Padawan

  • Members
  • Pip
  • 259 posts
  • Gender:Male
  • Location:California

Posted 29 December 2005 - 04:21 AM

Ok I have a script that adds an item based on its product id, and adds 1 quantity to the cart.

I want it to add the shirt size as well, so how would I do this, heres my code:

Heres my image/link to add the item:

<a href="store.php?page=add&id='. $prod->id .'&size=Medium" title="Add to Cart"><img src="/images/icon-cart-add.gif" alt="Add" /></a>

Heres my add page:

// If no product id is specified, go back to the main page.
$_SESSION["cart"]->add($id, $size, 1);
$_SESSION["cart"]->cleanup();
$_SESSION["cart"]->recalc_total();

if (! empty($HTTP_REFERER)) {
	header("Location: store.php?page=cart");
} else {
	header("Location: store.php?page=cart");
}


My add function:
Now how would I make that insert the shirt size? and maybe color?
I dont understand the function since I used a tutorial.

function add(&$productid, $shirtsize, $qty) {
	// Add an item to the shopping cart and update the total price.
		if (isset($productid)) {
			setdefault($this->items[$productid], 0);
			$this->items[$productid] += $qty;
		}
	}

Can someone please help:

Heres my full class function:

class Cart {
	var $items;		// Array of items
	var $total;		// Cart total

	function Cart() {
		$this->init();
	}

	function init() {
	// This function is called to initialize (and reset) a shopping cart.
		$this->items = array();
		$this->total = 0;
	}

	function add(&$productid, &$shirtsize, $qty) {
	// Add an item to the shopping cart and update the total price.
		if (isset($productid)) {
			setdefault($this->items[$productid], 0);
			$this->items[$productid] += $qty;
			$this->items[$shirtsize] += $shirtsize;
		}
	}

	function remove(&$productid) {
	// This function will remove a given product from the cart.
		if (isset($productid)) {
			unset($this->items[$productid]);
		}
	}

	function cleanup() {
	// This function will clean up the cart, removing items with invalid product id's (non-numeric ones) and products with quantities less than 1.
		foreach ($this->items as $productid => $qty) {
			if ($qty < 1) {
				unset($this->items[$productid]);
			}
		}
	}

	function itemcount() {
	// Returns the number of individual items in the shopping cart (note, this takes into account the quantities of the items as well).
		$count = 0;
		foreach ($this->items as $productid => $qty) {
			$count += $qty;
		}

		return $count;
	}

	function get_productid_list() {
	// Return a comma delimited list of all the products in the cart, this will be used for queries, eg. SELECT id, price FROM products WHERE id IN ....
		$productid_list = "";

		foreach ($this->items as $productid => $qty) {
			$productid_list .= ",'" . $productid . "'";
		}

		// Need to strip off the leading comma.
		return substr($productid_list, 1);
	}

	function recalc_total() {
	// Recalculate the total for the shopping cart, we will also do some cleanup and remove invalid items from the cart.  
	// We have to query the database to get the prices, so instead of making one query for each product in the basket
	// We will gather up all the ID's we are interested in and run one query to get all the products we care about (using $in_clause).
		$this->total = 0;

		$in_clause = $this->get_productid_list();
		if (empty($in_clause)) {
			return;
		}

		$qid = db_query("SELECT id, price FROM products WHERE id IN ($in_clause)");
		while ($product = db_fetch_object($qid)) {
			$this->total += $this->items[$product->id] * $product->price;
		}
	}
}


function get_cart_items() {
// Return a $qid of all the items in the shopping cart.
	global $_SESSION;

	$in_clause = $_SESSION["cart"]->get_productid_list();
	if (empty($in_clause)) {
		return false;
	}

	return db_query("SELECT * FROM products WHERE id IN ($in_clause)");
}

And this is the code that echos whats in the cart,
how would I make it show the size they chose?

<? load_profile();
$qid = get_cart_items(); 

 while ($prod = db_fetch_object($qid)) {
						$qty = $_SESSION["cart"]->items[$prod->id];
						$total = $prod->price * $qty;
?>


#2 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 29 December 2005 - 04:23 PM

1. You'll need to look into what the setdefault() function does.
2. So many references are pointless, and i belive slow things down. You also don't need to make sure $productid isset(), seeing as PHP would give a fatal error if it wasn't.
3. I believe
$this->items[$shirtsize] += $shirtsize;
// Should be
$this->items[$shirtsize] = $shirtsize;
Seeing as $this->items[$shirtsize] should be empty before adding the size to it, the + is worthless.
4. Try asking the author of the tutorial for support, they know it better then anybody else.

Note: These are just my suggestions, i don't know if any of them will actually work.

#3 tiki

    Young Padawan

  • Members
  • Pip
  • 259 posts
  • Gender:Male
  • Location:California

Posted 29 December 2005 - 08:25 PM

Set default is very simple.

Ill try your suggestion then try asking the author.

function setdefault(&$var, $default="") {
// If $var is undefined, set it to $default. Otherwise leave it alone.
if (! isset($var)) {
$var = $default;
}
}

#4 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 29 December 2005 - 11:30 PM

Ok... well now that i know what that does, it makes a little more sense.
Make $this->items[$productid] a multi-dimensional array. So it would basically look similar to this
$this->items[$productid]['quantity'] += $qty;
$this->items[$productid]['size'] = $shirtsize;
You can remove the setdefault function, it's pointless. And you'll probably have to adjust more of your script, but this is a start.

#5 tiki

    Young Padawan

  • Members
  • Pip
  • 259 posts
  • Gender:Male
  • Location:California

Posted 30 December 2005 - 12:51 AM

Ok ill try that, just one thing would be, how would I show the size in the cart now?

while ($prod = db_fetch_object($qid)) { 
	$qty = $_SESSION["cart"]->items[$prod->id];
	$size = $_SESSION["cart"]-> ???;
	$total = $prod->price * $qty;


#6 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 30 December 2005 - 01:36 AM

Like i said in my last post...
$qty = $_SESSION["cart"]->items[$prod->id]['quantity'];
$size = $_SESSION["cart"]->items[$prod->id]['size'];
$total = $prod->price * $qty;


#7 tiki

    Young Padawan

  • Members
  • Pip
  • 259 posts
  • Gender:Male
  • Location:California

Posted 30 December 2005 - 02:09 AM

It doesnt work, it shows the item still but not the size.

#8 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 30 December 2005 - 01:41 PM

From what i can tell, it's because you never tell PHP to display the shirt size. I have no idea what the rest of your site looks like (that includes the code behind it).
Without more info, there's nothing i can do right now. And no, i don't expect you to post the source to every single file you have on your site, just thte parts dealing with the display of the shirtsize.

But i do have one question
if (! empty($HTTP_REFERER)) {
	header("Location: store.php?page=cart");
} else {
	header("Location: store.php?page=cart");
}
What's the point of the if-statement if it redirects to the same page no matter what? Also $HTTP_REFERER, should be $_SERVER['HTTP_REFERER']

#9 tiki

    Young Padawan

  • Members
  • Pip
  • 259 posts
  • Gender:Male
  • Location:California

Posted 30 December 2005 - 04:12 PM

It used to direct to two different links, but I just changed the links and left the code. lol.

Well heres my cart php file.

///////////////////////////////////////////////////////////////////////////////
//  Cart Panel
///////////////////////////////////////////////////////////////////////////////
	case "cart":

require_login();
// Cart sub functions
if (isset($func)) {
	switch ($func) {
		case "remove" :
			$_SESSION["cart"]->remove($id);
			header ("Location: store.php?page=cart");
		break;
	
		case "empty" :
			$_SESSION["cart"]->init();
			header ("Location: store.php?page=cart");
		break;
	
		case "recalc" :
			$_SESSION["cart"]->recalc_total();
			$_SESSION["cart"]->cleanup();
			update_qty($HTTP_POST_VARS);
			header ("Location: store.php?page=cart");
		break;
	}
	
	$_SESSION["cart"]->recalc_total();
	$_SESSION["cart"]->cleanup();
}

load_profile();
$qid = get_cart_items();
	
// Check to see if any items in cart
if ($_SESSION["cart"]->itemcount() == 0) {
?>

			<!-- Cart Home -->
			<h1>Your Cart</h1>
				
			<fieldset>
			<legend><img src="/images/icon-cart.gif" alt="" /> Cart Items</legend>
			
				<div id="cart-left">
					<p>Your shopping cart is currently empty, we suggest purchasing some items to add to this shopping cart of yours. </p>
					<p>Would you like to view the <a href="store.php" title="Store">store</a>?</p>
				</div>
					
				<div id="cart-right">
					<h4>Options</h4>
						
					<p>You can only pay fast, easily and secure using Paypal. If you do not have a paypal please 
					<a href="https://www.paypal.com/us/cgi-bin/webscr?cmd=_registration-run" title="Join Paypal!">signup</a> for one!</p>
					
					<p><a href="info.php?page=termsofservice" title="Terms of Service">Terms of Service</a> <br />
					<a href="info.php?page=privacypolicy" title="Privacy Policy">Privacy Policy</a> <br />
					<a href="info.php?page=shippingreturns" title="Shipping and Returns">Shipping and Returns</a></p>
				</div>
				
			</fieldset>
			<!-- // Cart Home -->

<?
	include("library/footer.php");
	return false;
}
?>

			<!-- Cart Home -->
			<h1>Your Cart</h1>
				
			<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
			<input type="hidden" name="cmd" value="_cart" />
			<input type="hidden" name="upload" value="1" />
			<input type="hidden" name="business" value="paypal@illicitfashion.com" />
			<input type="hidden" name="item_name" value="Illicit Fashion" />
			<input type="hidden" name="currency_code" value="USD" />
			<input type="hidden" name="amount" value="<? printf("%.2f", $_SESSION["cart"]->total); ?>" />
				
			<fieldset>
			<legend><img src="/images/icon-cart.gif" alt="" /> Cart Items</legend>
					
				<div id="cart-left">
					<div class="vc1 vch"><acronym title="Delete">X</acronym></div>
					<div class="vc2 vch">Product</div>
					<div class="vc3 vch">Color / Size</div>
					<div class="vc4 vch">Price</div>
					<div class="vc5 vch"><acronym title="Quantity">Q</acronym></div>
					<br clear="all" />
					
					<? while ($prod = db_fetch_object($qid)) { 
						$qty = $_SESSION["cart"]->items[$prod->id]["quantity"];
						$size = $_SESSION["cart"]->items[$prod->id]["size"];
						$total = $prod->price * $qty;
					?>
							
					<div class="vc1"><a href="<? echo htmlspecialchars("store.php?page=cart&func=remove&id="); ?><?=$prod->id?>" title="Remove Item"><img src="/images/icon-cart-delete.gif" alt="X" /></a></div>
					<div class="vc2"><a href="<? echo htmlspecialchars("store.php?page=product&id="); ?><?=$prod->id?>" title="<? pv($prod->name) ?>"><? pv($prod->name) ?></a></div>
					<div class="vc3"><? echo "$size"; ?></div>
					<div class="vc4"><?=$prod->price?></div>
					<div class="vc5"><?=$qty?></div>
					<br clear="all" />
					
					<input type="hidden" name="item_name_1" value="<? pv($prod->name) ?>" />
					<input type="hidden" name="item_number_1" value="<?=$prod->id?>" />
					<input type="hidden" name="amount_1" value="<?=$prod->price?>" />
					<input type="hidden" name="quantity_1" value="<?=$qty?>" />
					<input type="hidden" name="on0_1" value="<? pv($prod->color) ?>" />
					<input type="hidden" name="on1_1" value="<? pv($prod->size) ?>" />
					<input type="hidden" name="shipping_1" value="2.50" />
					<input type="hidden" name="shipping2_1" value=".50" />
					
					<? } ?>
					
					<p style="text-align: right; padding: 2px 2px;">Grand Total: <b>$<? printf("%.2f", $_SESSION["cart"]->total); ?></b></p>
					<p class="button"><input type="image" src="/images/button-checkout.gif" alt="Checkout with Paypal" name="submit" id="submit" value="TRUE" /></p>
					
				</div>
				
				<div id="cart-right">
					<h4>Options</h4>
					
					<p>From here you can either checkout and purchase your cart, recalculate your total, update your quantity or continue shopping.</p>
					<p>You can only pay fast, easily and secure using Paypal. If you do not have a paypal please 
					<a href="https://www.paypal.com/us/cgi-bin/webscr?cmd=_registration-run" title="Join Paypal!">signup</a> for one!</p>
					
					<p><a href="store.php" title="Store">Continue Shopping</a> <br />
					<a href="store.php?page=cart<? echo "&amp;" ?>func=recalc" title="Recalculate">Recalculate Cart</a> <br />
					<a href="store.php?page=cart<? echo "&amp;" ?>func=empty" title="Empty Cart">Empty the Cart</a></p>
					
					<p><a href="info.php?page=termsofservice" title="Terms of Service">Terms of Service</a> <br />
					<a href="info.php?page=privacypolicy" title="Privacy Policy">Privacy Policy</a> <br />
					<a href="info.php?page=shippingreturns" title="Shipping and Returns">Shipping and Returns</a></p>
				</div>
					
			</fieldset>
			</form>
			<!-- // Cart Home -->

Edited by tiki, 30 December 2005 - 04:14 PM.


#10 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 31 December 2005 - 01:50 PM

I don't know, it looks right. Change the while loop to this:
<? while ($prod = db_fetch_object($qid)) { 
						$qty = $_SESSION["cart"]->items[$prod->id]["quantity"];
						$size = $_SESSION["cart"]->items[$prod->id]["size"];
						$total = $prod->price * $qty;
die(highlight_string(print_r($_SESSION['cart'],1),1));
					?>
I added the die() function, so when you load up the page, it will print everything in your cart to the string. If the items size is in there, then theres something really wierd going on. If it's not, then it was never set.
If you have no idea what the random stuff that appears on the screen is, copy/paste it here and i'll take a look at it.

Edited by rc69, 31 December 2005 - 01:50 PM.


#11 tiki

    Young Padawan

  • Members
  • Pip
  • 259 posts
  • Gender:Male
  • Location:California

Posted 31 December 2005 - 04:38 PM

Error, I probably need to update the functions:

Notice: Undefined index: 1 in /home/illicitf/public_html/library/functions.php on line 27

Notice: Undefined index: quantity in /home/illicitf/public_html/library/functions.php on line 27

Fatal error: Unsupported operand types in /home/illicitf/public_html/library/functions.php on line 90

Fatal error: Unsupported operand types in /home/illicitf/public_html/library/functions.php on line 59

	function add(&$productid, &$shirtsize, $qty) {
	// Add an item to the shopping cart and update the total price.
		if (isset($productid)) {
			$this->items[$productid]["quantity"] += $qty;  ////// Line 27
			$this->items[$productid]["size"] = $shirtsize;
		}
	}

	function recalc_total() {
	// Recalculate the total for the shopping cart, we will also do some cleanup and remove invalid items from the cart.  
	// We have to query the database to get the prices, so instead of making one query for each product in the basket
	// We will gather up all the ID's we are interested in and run one query to get all the products we care about (using $in_clause).
		$this->total = 0;

		$in_clause = $this->get_productid_list();
		if (empty($in_clause)) {
			return;
		}

		$qid = db_query("SELECT id, price FROM products WHERE id IN ($in_clause)");
		while ($product = db_fetch_object($qid)) {
			$this->total += $this->items[$product->id] * $product->price; //// Line 90
		}
	}
}

	function itemcount() {
	// Returns the number of individual items in the shopping cart (note, this takes into account the quantities of the items as well).
		$count = 0;
		foreach ($this->items as $productid => $qty) {
			$count += $qty;  //// Line 59
		}

		return $count;
	}

Edited by tiki, 31 December 2005 - 04:39 PM.


#12 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 01 January 2006 - 01:10 AM

For the notices: http://php.net/types...pes.array.donts

The other error i've never seen before, but after a quick Google search, i found that changing += to =+ might (for some odd reason) fix the problem (even though it shouldn't).

Edited by rc69, 01 January 2006 - 01:10 AM.


#13 tiki

    Young Padawan

  • Members
  • Pip
  • 259 posts
  • Gender:Male
  • Location:California

Posted 01 January 2006 - 05:48 AM

Php is retarded sometimes, ill update this asap.

#14 tiki

    Young Padawan

  • Members
  • Pip
  • 259 posts
  • Gender:Male
  • Location:California

Posted 01 January 2006 - 06:18 PM

I got rid of the notices, but still have the fatal error:

Fatal error: Unsupported operand types in /home/illicitf/public_html/library/functions.php on line 90
recalc_total() is line 77-93.

class Cart {
	var $items;		// Array of items
	var $total;		// Cart total

	function Cart() {
		$this->init();
	}

	function init() {
	// This function is called to initialize (and reset) a shopping cart.
		$this->items = array();
		$this->total = 0;
	}

	function add(&$productid, &$shirtsize, $qty) {
	// Add an item to the shopping cart and update the total price.
		if (isset($productid)) {
			$this->items["$productid"]["quantity"] =+ $qty;
			$this->items["$productid"]["size"] = $shirtsize;
		}
	}

	function set(&$productid, $qty) {
	// Set the quantity of a product in the cart to a specified value.
		if (isset($productid)) {
			$this->items[$productid] = (int) $qty;
		}
	}

	function remove(&$productid) {
	// This function will remove a given product from the cart.
		if (isset($productid)) {
			unset($this->items[$productid]);
		}
	}

	function cleanup() {
	// This function will clean up the cart, removing items with invalid product id's (non-numeric ones) and products with quantities less than 1.
		foreach ($this->items as $productid => $qty) {
			if ($qty < 1) {
				unset($this->items[$productid]);
			}
		}
	}

	function itemcount() {
	// Returns the number of individual items in the shopping cart (note, this takes into account the quantities of the items as well).
		$count = 0;
		foreach ($this->items as $productid => $qty) {
			$count =+ $qty;
		}

		return $count;
	}

	function get_productid_list() {
	// Return a comma delimited list of all the products in the cart, this will be used for queries, eg. SELECT id, price FROM products WHERE id IN ....
		$productid_list = "";

		foreach ($this->items as $productid => $qty) {
			$productid_list .= ",'" . $productid . "'";
		}

		// Need to strip off the leading comma.
		return substr($productid_list, 1);
	}

	function recalc_total() {
	// Recalculate the total for the shopping cart, we will also do some cleanup and remove invalid items from the cart.  
	// We have to query the database to get the prices, so instead of making one query for each product in the basket
	// We will gather up all the ID's we are interested in and run one query to get all the products we care about (using $in_clause).
		$this->total = 0;

		$in_clause = $this->get_productid_list();
		if (empty($in_clause)) {
			return;
		}

		$qid = db_query("SELECT id, price FROM products WHERE id IN ($in_clause)");
		while ($product = db_fetch_object($qid)) {
			$this->total += $this->items["$product->id"] * $product->price;
		}
	}
}


#15 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 01 January 2006 - 10:35 PM

You fixed the one on 59 right? What'd you do to fix that one?

#16 tiki

    Young Padawan

  • Members
  • Pip
  • 259 posts
  • Gender:Male
  • Location:California

Posted 02 January 2006 - 03:32 AM

$this->items["$productid"]["quantity"] =+ $qty;
$this->items["$productid"]["size"] = $shirtsize;

Quoted.

#17 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 02 January 2006 - 04:08 PM

I think there's a misunderstanding some where.

Quote

Fatal error: Unsupported operand types in /home/illicitf/public_html/library/functions.php on line 90

Fatal error: Unsupported operand types in /home/illicitf/public_html/library/functions.php on line 59
Now you're still getting the one on 90, so what did you do to fix the one on 59?

#18 tiki

    Young Padawan

  • Members
  • Pip
  • 259 posts
  • Gender:Male
  • Location:California

Posted 02 January 2006 - 05:22 PM

On 59 I changed += to =+ or whatever.

Did the same on 90 tried =+ and += but still get the error.

Maybe Ill have to fix the functions to include the size and quantity.

edit:
Im going to search for an alternative session based cart system...
That does sizes...

So help me find any =P

edit2:
http://www.phpwebcommerce.com/

That looks useful, I could possibly try and get the sizes working somehow ;)

#19 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 03 January 2006 - 08:21 PM

First, please don't triple post, use the edit button.
Second, i know nothing about web commerce systems. All i know is, if you want something to work how you want it to, code it yourself. I'm not trying to be offensive, just voicing my opinion.

#20 tiki

    Young Padawan

  • Members
  • Pip
  • 259 posts
  • Gender:Male
  • Location:California

Posted 03 January 2006 - 10:43 PM

Well I dont know classes or oop, so it doesnt really help me.

Im using that site I posted as a basic guideline and figuring from there.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users