PHP (Session) Cart to Checkout question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
koolsamule
Forum Contributor
Posts: 130
Joined: Fri Sep 25, 2009 10:03 am

PHP (Session) Cart to Checkout question

Post by koolsamule »

Hi Chaps,

I have a really basic shopping cart, which uses Sessions to add/update/delete/write products.

My question is, what's the best way to transfer the cart/session information to a checkout stage where a user would sign in/sign up, then complete the payment?

I'm guessing that when the user gets to the checkout, an 'order/order item' is created, which would need a customer id.

I'm not sure how to link the session cart to a customer/order??

Sorry if this is a bit of a daft question, but I guess it's beeter to be sure before doing something stupid!

Cheers
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: PHP (Session) Cart to Checkout question

Post by John Cartwright »

Are you refering to a third party checkout process, where it is hosted on another system? If so, a database is typically used to store intermediate customer information, which an identifier is passed (and returned) to the third party checkout system to link the customer to the order.

You've been quite general so I'm not sure if this is what you are after.
koolsamule
Forum Contributor
Posts: 130
Joined: Fri Sep 25, 2009 10:03 am

Re: PHP (Session) Cart to Checkout question

Post by koolsamule »

Hi, thanks for the reply.

Process:
1. User browses products
2. Builds a cart of items, quantities, price, using sessions
3. User sign's up/in
4. Checkout, where (MySQL) user_id + (Session) cart contents = (MySQL) order
5. Once there is an order, I'll be (trying) to link the order info with an integrated payment system, such as PayPal.

I guess I'm after a bit of guidence to get session data into a database really.

Cheers
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: PHP (Session) Cart to Checkout question

Post by John Cartwright »

I guess I'm after a bit of guidence to get session data into a database really.
I still don't really understand what you are after exactly. To insert session information into your database, you simply need to connect to your database, and perform the query with session values. You haven't given me any specifics, but I would do something along the lines of (simplified)

Code: Select all

/**
 * Generate an order id, which stores relationship information about the user and specific items in the cart
*/
$sql = "
   INSERT INTO `cart_orders`
   SET `user_id` = ". (int)$_SESSION['userid'] .",
         `status` = 'unprocessed'
         `created` = NOW() 
";
mysql_query($sql) or die(mysql_error());
   
/** 
 * Order id will be used to link third party processor to user
*/
$orderid = mysql_insert_id();

/**
 * Loop through individual items and link them to our specific order id
*/
foreach ($_SESSION['items'] as $item) 
{
   $sql = "
      INSERT INTO `cart_orders_contents`
      SET `order_id` = ". (int)$orderid ."
            `product_id` = ". (int)$item['product_id'] .",
            `quantitiy` = ". (int)$item['quantity'] .",
            `created` = NOW()       
   ";
   mysql_query($sql) or die(mysql_error());
}

//paypal stuff
$paypal = new paypal();
$paypal->add_field('custom', $orderid);
$paypal->submit();


Now, by passing the $orderid field to paypal, during the post notification process you can relate the order id to the specific order contents to mark the order as fulfilled and perform the specific inventory changes.

Might be a little much to swallow at first, but it truly is a simple process.
Post Reply