Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Mar 2009
    Posts
    55
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Paypal Add to Cart Customization

    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

  • #2
    New Coder
    Join Date
    Mar 2009
    Posts
    55
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Still looking for a way to do this if anyone has any suggestions.

  • #3
    New Coder
    Join Date
    Mar 2009
    Posts
    55
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Figured out how to do this.

    Code:
    <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
    Code:
    //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.

  • #4
    Senior Coder
    Join Date
    Aug 2010
    Location
    High Point, NC
    Posts
    3,338
    Thanks
    5
    Thanked 363 Times in 360 Posts
    Quote Originally Posted by Serihon View Post
    Figured out how to do this.

    Code:
    <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
    Code:
    //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.
    Teed

  • #5
    New Coder
    Join Date
    Mar 2009
    Posts
    55
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by teedoff View Post
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •