#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    6
    Rep Power
    0

    Question Intergrating Paypal into my shopping cart!


    First of all I would like to thank the users of dev shed forums for all the help so far thankyou.

    Secondly another shopping cart question.

    Im struggling intergrating the paypal checkout api into my existing shopping cart. Ive tried the HTML stuff but this is very limited and wont let me pass individual items to PayPal such as quantity. Ive managed to pass the total cart value to Paypal but not the quantity etc. Does anyone have an idea how I can do this with my cart?

    Code below

    PHP Code:
    <?php 

    //Start the session
    session_start();

    function 
    checkCartForItem($addItem$cartItems) {
         if (
    is_array($cartItems)){
              foreach(
    $cartItems as $key => $item) {
                  if(
    $item['name'] === $addItem)
                      return 
    $key;
              }
         }
         return 
    false;
    }

    //Create cart if it doesn't already exist
    if (!isset($_SESSION['SHOPPING_CART'])){ $_SESSION['SHOPPING_CART'] = array(); }

    //Add an item only if we have the required pieces of information: name, price, qty
    if (isset($_GET['add']) && isset($_GET['price']) && isset($_GET['qty'])){
                    
        
    //Store it in a Array
        
    $ITEM = array(
            
    //Item name        
            
    'name' => $_GET['add'], 
            
    //Item Price
            
    'price' => $_GET['price'], 
            
    //Qty of item
            
    'qty' => $_GET['qty']    
                
            );
            
            
    // If item exists increment quantity value by 1
            
    $addItem $_GET['add'];

    $itemExists checkCartForItem($addItem$_SESSION['SHOPPING_CART']);

     
    $itemExists checkCartForItem($addItem$_SESSION['SHOPPING_CART']); 

    if (
    $itemExists !== false){ 
        
    // item exists - increment quantity value by 1 
        
    $_SESSION['SHOPPING_CART'][$itemExists]['qty']++; 
    } else { 
        
    //Add this item to the shopping cart 
        
    $_SESSION['SHOPPING_CART'][] =  $ITEM
    }              
        
    header('Location: ' $_SERVER['PHP_SELF']);    
    }

    else if (isset(
    $_GET['remove'])){
        
    //Remove the item from the cart
        
    unset($_SESSION['SHOPPING_CART'][$_GET['remove']]);
        
    //Re-organize the cart
        //array_unshift ($_SESSION['SHOPPING_CART'], array_shift ($_SESSION['SHOPPING_CART']));
        //Clear the URL variables
        
    header('Location: ' $_SERVER['PHP_SELF']);

    }

    else if (isset(
    $_GET['empty'])){
        
    //Clear Cart by destroying all the data in the session
        
    session_destroy();
        
    //Clear the URL variables
        
    header('Location: ' $_SERVER['PHP_SELF']);

    }

    else if (isset(
    $_POST['update'])) {
        
        
    //Updates Qty for all items
        
    foreach ($_POST['items_qty'] as $itemID => $qty) {
            
            
    //If the Qty is "0" remove it from the cart
            
    if ($qty == 0) {
                
    //Remove it from the cart
                
    unset($_SESSION['SHOPPING_CART'][$itemID]);
                
            }
        
            else if(
    $qty >= 1) {
                
    //Update to the new Qty
                
    $_SESSION['SHOPPING_CART'][$itemID]['qty'] = $qty;
                
                
    //Update quantity if item exists
                
    $addItem $_GET['add'];
                        
            }
        }    
            
        
    //Clear the POST variables
        
    header('Location: ' $_SERVER['PHP_SELF']);

    ?>
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Dec 2004
    Posts
    3,031
    Rep Power
    377
    what does the paypal documentation have to say about this?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    6
    Rep Power
    0

    Wink


    Originally Posted by paulh1983
    what does the paypal documentation have to say about this?
    Ive looked over the paypal documentation and I managed to pass the cart total but there are no options for quantity etc. The documentation isnt very clear thats why im asking if anyone has accomplished this and whether its possible to accomplish with the code above!
  6. #4
  7. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6352
    I believe paypal only accepts a total amount to charge, it's not a shopping cart processor.

    Though I could be wrong. I certainly only use it for fixed amounts.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

IMN logo majestic logo threadwatch logo seochat tools logo