Jump to content

Paypal Checkout help


Samza
Go to solution Solved by mac_gyver,

Recommended Posts

Hey guys,

 

I have created a shopping cart system and I am now up to the point of building the checkout script to allow the customer to checkout using paypal.

 

I have the bqasic paypal script form however I now need to create an array which pulls the data out of the customers shopping cart (which is stored in a DB) and for each product, output in the form of a hidden input form with the fields; item_number_x, item_id_x, item_price_x, item_quantity_x. Where X is the value of each product in the shopping cart.

 

I am really suck on where to start and could do with some advice.

 

This is the code that I originally tried however Im not have any luck as I am fairly new to using arrays;

function Paypal_items() {
	//Get the items to submit for paypal
	$cart = new ShoppingCart;
	$cart_id = $cart -> get_cart_id();
	
	$num = 0;
	$cart_sql = mysql_query("SELECT product_id, product_title, product_price FROM shopping_cart WHERE cart_identifier='".mysql_real_escape_string($cart_id)."'");
	$cart_result = mysql_fetch_assoc($cart_sql);
	
	foreach ($cart_result as $name => $value) {
		if ($value != 0) {
			if($name){
				$product_id = $name;
				echo "<br />name: " . $product_id . " : value". $value;
			}
			$sql = mysql_query("SELECT product_id, product_title, product_price FROM shopping_cart WHERE cart_identifier='$cart_id'");
			while ($sql_result = mysql_fetch_assoc($sql)) {
				$num++;
				echo $num;
				echo "<input type=\"hidden\" name=\"item_number_\"".$num." value=\"".$product_id."\"";
			}
		} else {
			echo "!= is false<br />";
		}
	}
}

Thanks!

Link to comment
Share on other sites

the only thing your code needs to do is run 1 (one) query that gets the content of the cart, then loop over the rows that the query retrieved and produce the output that you want.

 

your foreach(){} loop makes no sense, it is looping over the first row in the cart and your query inside of the loop makes no sense because you have already queried for the contents of the cart.

Link to comment
Share on other sites

the only thing your code needs to do is run 1 (one) query that gets the content of the cart, then loop over the rows that the query retrieved and produce the output that you want.

 

your foreach(){} loop makes no sense, it is looping over the first row in the cart and your query inside of the loop makes no sense because you have already queried for the contents of the cart.

 

Thanks for this help, I was following a tutorial previously but it clearly was a bit messed up and didnt need to be that completed for my cart.

 

Anyway here's what I've got;

function Paypal_items() {
	//Get the items to submit for paypal
	$cart = new ShoppingCart;
	$cart_id = $cart -> get_cart_id();
	// run 1 query to get the customers cart details
	// create a loop over the cart rows to output the fields
	
	$num = 0;
	$sql = mysql_query("SELECT * FROM shopping_cart WHERE cart_identifier='".mysql_real_escape_string($cart_id)."'");
	$result = mysql_fetch_array($sql,0);
	$count = mysql_num_rows($sql);
	$i = 0;
	while($i < $count) {
		$num++;
		$i = $i+1;
		echo "<input type=\"hidden\" name=\"item_number_".$num."\" value=\"".$result[product_id]."\" />";
		echo "<input type=\"hidden\" name=\"item_name_".$num."\" value=\"".$result[product_title]."\" />";
		echo "<input type=\"hidden\" name=\"amount_".$num."\" value=\"".$result[product_price]."\" />";
	}
	
}

However, one problem I am having this that if there is more than 1 item in the cart, it is repeating the same data from the 1st cart record rather than for each cart item

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.