...

View Full Version : Paypal Add to Cart Customization



Serihon
03-05-2012, 06:12 PM
Hey everyone,

I wasn't sure where to go to ask this and thought someone here might be able to help. I currently am using Paypal's "Add to Cart" buttons to allow potential customers the ability to purchase items from my website.

What I would like to do is be able to offer on a specific page multiple items and services for people to purchase using one "Add to Cart" button. Basically it would be something like this.
Qty:[#] Coffee Mug $##.##
Qty:[#] Beach Towel $##.##
Qty:[#] Monthly Service $##.##
Add to Cart

If they input 2 coffee mugs, 0 beach towels, and 6 months of service and hit the add to cart button they would then be taken to the Paypal page where it would have those items they filled in quantities for ready for them to pay for.

Would this be possible and if so, how?

Thanks,

Serihon

Serihon
03-07-2012, 01:36 PM
Still looking for a way to do this if anyone has any suggestions.

Serihon
03-07-2012, 08:44 PM
Figured out how to do this.



<form id="form" action="shopProcess.php" method="post">
<input type="hidden" name="item_name_1" value="Towel" />
<input type="hidden" name="amount_1" value="15.00" />
<label for="quantity_1">Quantity:</label>
<input type="text" name="quantity_1" id="quantity_1" maxlength="6" width="50px" />
<input type="hidden" name="item_name_2" value="Cup" />
<input type="hidden" name="amount_2" value="5.00" />
<label for="quantity_2">Quantity:</label>
<input type="text" name="quantity_2" id="quantity_2" maxlength="6" width="50px" />
<input type="image" src="https://www.paypal.com/en_US/i/btn/btn_cart_LG.gif" border="0" name="submit" alt="" align="middle" width="120" height="26">
</form>


And the PHP


//Variables
$count=0;
$count2=1;
$items=array();

//Function to sanitize values received from the form. Prevents SQL injection
function clean($str) {
$str = @trim($str);
if(get_magic_quotes_gpc()) {
$str = stripslashes($str);
}
return mysql_real_escape_string($str);
}

//Pull information where quantity is not 0
for($i=1; $i<=10; $i++){
if(isset($_POST['quantity_'.$i]) && $_POST['quantity_'.$i] != "0"){
$count++;
$items[$count]['item_name']=clean($_POST['item_name_'.$i]);
$items[$count]['amount']=clean($_POST['amount_'.$i]);
$items[$count]['quantity']=clean($_POST['quantity_'.$i]);
}
}?>

<form name="form" id="form" target="paypal" action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="upload" value="1" />
<input type="hidden" name="cmd" value="_cart" />
<input type="hidden" name="business" value="business@biz.biz" />
<input type="hidden" name="currency_code" value="USD">

<?php foreach($items as $item){ //fill info into form ?>
<?php if($item['item_name']=="Cam Hosting"){ ?>
<input type="hidden" name="item_name_<?php echo $count2; ?>" value="<?php echo $item['item_name']; ?>" />
<input type="hidden" name="amount_<?php echo $count2; ?>" value="<?php echo $item['quantity']; ?>" />
<input type="hidden" name="quantity_<?php echo $count2; ?>" value="<?php echo $item['amount']; ?>" />
<?php }else{ ?>
<input type="hidden" name="item_name_<?php echo $count2; ?>" value="<?php echo $item['item_name']; ?>" />
<input type="hidden" name="amount_<?php echo $count2; ?>" value="<?php echo $item['amount']; ?>" />
<input type="hidden" name="quantity_<?php echo $count2; ?>" value="<?php echo $item['quantity']; ?>" />
<?php }
$count2++; ?>
<?php } ?>

<input type="hidden" name="currency_code" value="USD">&nbsp;
</form>


Of course I cannot figure out how to add an input for a subscription payment in there. I assume that since it is ran using a different cmd through Paypal that it isn't possible to process a one time payment and a subscription payment at the same time.

teedoff
03-08-2012, 06:08 PM
Figured out how to do this.



<form id="form" action="shopProcess.php" method="post">
<input type="hidden" name="item_name_1" value="Towel" />
<input type="hidden" name="amount_1" value="15.00" />
<label for="quantity_1">Quantity:</label>
<input type="text" name="quantity_1" id="quantity_1" maxlength="6" width="50px" />
<input type="hidden" name="item_name_2" value="Cup" />
<input type="hidden" name="amount_2" value="5.00" />
<label for="quantity_2">Quantity:</label>
<input type="text" name="quantity_2" id="quantity_2" maxlength="6" width="50px" />
<input type="image" src="https://www.paypal.com/en_US/i/btn/btn_cart_LG.gif" border="0" name="submit" alt="" align="middle" width="120" height="26">
</form>


And the PHP


//Variables
$count=0;
$count2=1;
$items=array();

//Function to sanitize values received from the form. Prevents SQL injection
function clean($str) {
$str = @trim($str);
if(get_magic_quotes_gpc()) {
$str = stripslashes($str);
}
return mysql_real_escape_string($str);
}

//Pull information where quantity is not 0
for($i=1; $i<=10; $i++){
if(isset($_POST['quantity_'.$i]) && $_POST['quantity_'.$i] != "0"){
$count++;
$items[$count]['item_name']=clean($_POST['item_name_'.$i]);
$items[$count]['amount']=clean($_POST['amount_'.$i]);
$items[$count]['quantity']=clean($_POST['quantity_'.$i]);
}
}?>

<form name="form" id="form" target="paypal" action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="upload" value="1" />
<input type="hidden" name="cmd" value="_cart" />
<input type="hidden" name="business" value="business@biz.biz" />
<input type="hidden" name="currency_code" value="USD">

<?php foreach($items as $item){ //fill info into form ?>
<?php if($item['item_name']=="Cam Hosting"){ ?>
<input type="hidden" name="item_name_<?php echo $count2; ?>" value="<?php echo $item['item_name']; ?>" />
<input type="hidden" name="amount_<?php echo $count2; ?>" value="<?php echo $item['quantity']; ?>" />
<input type="hidden" name="quantity_<?php echo $count2; ?>" value="<?php echo $item['amount']; ?>" />
<?php }else{ ?>
<input type="hidden" name="item_name_<?php echo $count2; ?>" value="<?php echo $item['item_name']; ?>" />
<input type="hidden" name="amount_<?php echo $count2; ?>" value="<?php echo $item['amount']; ?>" />
<input type="hidden" name="quantity_<?php echo $count2; ?>" value="<?php echo $item['quantity']; ?>" />
<?php }
$count2++; ?>
<?php } ?>

<input type="hidden" name="currency_code" value="USD">&nbsp;
</form>


Of course I cannot figure out how to add an input for a subscription payment in there. I assume that since it is ran using a different cmd through Paypal that it isn't possible to process a one time payment and a subscription payment at the same time.

That would be what "php condition statements" are for. I'm not a php guy, but essentially you could have a two or more checkboxes, say

1: One Time Payment
2: Yearly Subscription

Then use if statements to run different code dependant on which one the user selects.

Serihon
03-08-2012, 07:18 PM
That would be what "php condition statements" are for. I'm not a php guy, but essentially you could have a two or more checkboxes, say

1: One Time Payment
2: Yearly Subscription

Then use if statements to run different code dependant on which one the user selects.

Thanks for the reply. However the variables passed to a _cart versus a _subscription payment are different and it seems impossible to pass them both at the same time. I already have the means to figure out which the user wants to process and process them separately.

What I am trying to find out is whether a subscription can be processed at the same time single purchase items are processed to save the user time.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum