I'm designing an online shop - http://growinggifts.co.uk/shop.php . It has a functioning shopping basket coded with PHP, using a mysql db. However I'd like to create an "add to Paypal cart button", which passes the entire basket contents (item names, prices, quantity) to paypals cart, with one click. The shoppingcart.php file (shown further down) returns an indexed array of all the contents in the basket using the function "get_contents". It is then displayed in basket.php using a table. I'd like to have this "add to paypal cart" in basket.php, below the table of basket contents.
Each item to be added to paypals shopping cart requires 4 lines of form code within the "add to cart" button.
eg.
<input type='hidden' name='item_name_1' value='Medium Ceramic Pot' />
<input type='hidden' name='amount_1' value='18.00' />
<input type='hidden' name='currency_code' value='GBP' />
<input type='hidden' name='quantity_2' value = '2' />
The item_name_? and amount_? must be ascending 1, 2, 3, 4 etc.. The quantity_? and its value is generated from the current basket contents.
Below is all the relevant code. Can anyone show me the code to do this?
First of all ...this is basically what I'm trying to achieve...
HTML Code:<form target="paypal" action="https://www.paypal.com/uk/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='email' /> //need to automatically generate these (example) lines, getting the values from the current basket contents //item_name_? and amount_? must be ascending 1, 2, 3, 4 etc.. //quantity_? and its value is generated from the current basket contents //******************************************************************* <input type='hidden' name='item_name_1' value='Medium Ceramic Pot' /> <input type='hidden' name='amount_1' value='18.00' /> <input type='hidden' name='currency_code' value='GBP' /> <input type='hidden' name='quantity_2' value = '2' /> //******************************************************************* <input type='hidden' name='item_name_2' value='Small Ceramic Pot' /> <input type='hidden' name='amount_2' value='14.00' /> <input type='hidden' name='currency_code' value='GBP' /> <input type='hidden' name='quantity_1' value = '1' /> //******************************************************************* <input type='hidden' name='item_name_3' value='Small Hanging Basket' /> <input type='hidden' name='amount_3' value='15.00' /> <input type='hidden' name='currency_code' value='GBP' /> <input type='hidden' name='quantity_1' value = '1' /> //******************************************************************* etc...PHP Code:basket.php
*******************************************************************
<?
// Cart Cookie Creator
if(!isset($HTTP_COOKIE_VARS['cart_id'])) {
$cart_id = md5(uniqid(rand()));
setcookie("cart_id", $cart_id, time() + 14400);
} else {
$cart_id = $HTTP_COOKIE_VARS['cart_id'];
}
// Make the Cart Object
require_once("shoppingcart.php");
$cart = new cart($cart_id);
// Select Action
switch($_GET['a']){
case "add":
if(isset($_POST['prodId']) and isset($_POST['quantity'])) $cart->add_item($_POST['prodId'],$_POST['quantity']);
break;
case "update":
$cart->modify_quantity($_POST["prodId"],$_POST["quantity"]);
break;
case "delete":
$cart->delete_item($_POST['prodId']);
break;
case "clear":
$cart->clear_cart();
break;
}
// Begin Cart Display
$cartContents = $cart->get_contents();
$cart_tbl = "
<!-- Begin Cart Display -->
<table summary='Your shopping cart' id='cart'>
<tr>
<th>Item</th>
<th>Product Name</th>
<th>Quantity</th>
<th>Price</th>
<th> </th>
</tr>";
if(count($cartContents) <= 0){ //nothing in cart
$cart_tbl .= "
<tr>
<td class='items' colspan='5'>Your cart is empty</td>
</tr>";
}else{ //stuff in the cart so show it
for($i=0; $i < count($cartContents); $i++){
$cart_tbl .= "
<tr>
<td class='items'>" . $cartContents[$i][0] . "</td>
<td class='items'>" . $cartContents[$i][1] . "</td>
<td class='items'>
<form action=\"basket.php?a=update\" method=\"POST\">
<input type='hidden' name='prodId' value='". $cartContents[$i][0] ."' />
<input type='text' name='quantity' size='3' value='" . $cartContents[$i][2] . "' />
<input type='submit' value='update' />
</form>
</td>
<td class='items'>£". $cartContents[$i][3] ."</td>
<td class='items'>
<form action=\"basket.php?a=delete\" method=\"POST\">
<input type='hidden' name='prodId' value='". $cartContents[$i][0] ."' />
</>
<input type='submit' value='remove item' />
</form>
</td>
</tr>";}}
$cart_tbl .= "
<tr>
<td colspan='3' class='empty'> </td><td class='total'>". $cart->cart_total() ."</td>
<td class='items'><form action=\"basket.php?a=clear\" method=\"post\"><input type=\"submit\" value=\"clear cart\" /></form></td>
</tr>";
$cart_tbl .= "
</tr>
</table>
<!-- End Cart Display -->\n\n\n";
//add all basket contents to paypal cart (the bit I'm stuck on)
<form target="paypal" action="https://www.paypal.com/uk/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='email' />
//need to automatically generate these (example) lines, getting the values from the current basket contents
//item_name_? and amount_? must be ascending 1, 2, 3, 4 etc..
//quantity_? and its value is generated from the current basket contents
//*******************************************************************
<input type='hidden' name='item_name_1' value='Medium Ceramic Pot' />
<input type='hidden' name='amount_1' value='18.00' />
<input type='hidden' name='currency_code' value='GBP' />
<input type='hidden' name='quantity_2' value = '2' />
//*******************************************************************
<input type='hidden' name='item_name_2' value='Small Ceramic Pot' />
<input type='hidden' name='amount_2' value='14.00' />
<input type='hidden' name='currency_code' value='GBP' />
<input type='hidden' name='quantity_1' value = '1' />
//*******************************************************************
<input type='hidden' name='item_name_3' value='Small Hanging Basket' />
<input type='hidden' name='amount_3' value='15.00' />
<input type='hidden' name='currency_code' value='GBP' />
<input type='hidden' name='quantity_1' value = '1' />
//*******************************************************************
etc...
<input type='submit' name='submit' value='Pay via Paypal' /></form>
?>PHP Code:relevant functions to this problem in shoppingcart.php
*******************************************************************
// Adapted from John Coggeshall's tutorial at:
// [url]http://www.zend.com/zend/spotlight/php-shopping-cart1.php[/url]
function get_contents() {
//get stuff in the cart and return in indexed array
$q1 = "SELECT * FROM ". $this->cart_table. " WHERE session='".$this->cart_id."'";
$incart_r = mysql_query($q1,$this->dblink) or die(mysql_error());
$contents = array();
while($incart = mysql_fetch_array($incart_r)){
//get the item's price first
$q2 = "SELECT price, prodName FROM ". $this->inv_table. " WHERE prodId='".$incart["product"]."'";
$prodResult = mysql_query($q2) or die(mysql_error());
$name = mysql_result($prodResult,0,"prodName");
$price = mysql_result($prodResult,0,"price");
//build array of info
$item = array($incart['product'], $name, $incart['quantity'],$price);
array_push($contents,$item);
}
return $contents;
}
function quant_items() {
$contents = $this->get_contents();
$q = 0;
for($i=0; $i < count($contents); $i++){
$q = $q + $contents[$i][2];
}
return $q;
}




Bookmarks