Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

shopping cart in php

Status
Not open for further replies.
There are a lot of PHP carts available. Most of the ones I looked at were over complicated and would have been more work to incorporate into my websites than writing my own. I think it took me about 45 minutes to write one after I decided how I wanted the object model.
 
Ericbrunson,

could you post the code somewhere, i'd love to see how you have gone about it. I know most of the php books use carts as an example but still a bit simple.
I agree with you oscommerce and zencart seem very complex if you just wawnt to sell a couple of items.
 
I don't usually post code like this, but I want to make a point. This stuff ain't magic. What does a cart need to do? Keep track of some items, persist between page retrievals, do some calculations (like a total) and display itself. Come up with a simple object that knows how to do those things and the rest is just eye-candy.

Here's a pared down version, it has the basic functionality.

This code won't run by itself, it's more of a framework to subclass from, and I've deleted most of the fancy bells and whistles. But it shows a simple object design that can be extended to do whatever special things you need.

I can usually set up a functional customer shopping cart in about 15 minutes using this as a starting point.

Code:
class ShoppingCart {

  function ShoppingCart ( $populate='session' ) {
    // it can populate itself from a session or a request
    $this->count = 0;
    $this->items = array();

    if ( $populate == 'session' ) $this->restore();
    elseif ( $populate == 'request' ) $this->populate_from_request();

  }

  function add_item( $array, $qty = 1 ) {
    $this->count = $this->count + $qty;
    $this->items[ $this->count ] = new cart_item( $array );

  }

  function save() {
    $array =& $_SESSION[ 'cart' ];

    $count = 0;

    foreach ( $this->items as $item ) {
      $count = $count + 1;
      $array[ $count ] =& $item->info;

    }

  }

  function restore() {
    if ( ! isSet( $_SESSION[ 'cart' ] ) or ! $_SESSION[ 'cart' ] ) return;

    $array =& $_SESSION[ 'cart' ];

    foreach ( $array as $store ) {
      $this->add_item( $store );

    }

  }

  function populate_from_request() {
    if ( ! $_REQUEST[ 'prod' ] ) return;

    $proto = new cart_item();

    foreach ( $_REQUEST[ 'proud' ] as $index => $value ) {

      if ( $_REQUEST[ 'quantity' ][ $index ] ) {
        $store = array();

        foreach ( $proto->fields as $field ) {
          $store[ $field ] = $_REQUEST[ $field ][ $index ];

        }
        $this->add_item( $store );

      }

    }

  }

  function show_cart() {
    $count = 0;
    foreach ( $this->items as $item ) {
      $item->display_line_item( sprintf( '[%02s]', $count ) );
      $count++;

    }

  }

  function total() {
    $sum = 0;
    foreach ( $this->items as $item ) {
      $sum += $item->total();

    }

    return sprintf( "%.2f", $sum );

  }

  function print_html() {
    // do your display as customer html 
  }

}

It is also uses an item object that has to implement an interface that the cart expects:

Code:
class cart_item extends {

  var $fields = array(  'pname',
                        'number',
                        'quantity',
                        'pricing' );

  function cart_item( $store=false,
                      $product=false ) {

    $this->info = array();
    $this->product = $product;

    if ( $store ) {
      $this->info[ 'pname' ]      = $store[ 'pname' ];
      $this->info[ 'number' ]     = $store[ 'number' ];
      $this->info[ 'quantity' ]   = $store[ 'quantity' ];
      $this->info[ 'pricing' ]    = $store[ 'pricing' ];

    }

  }

  function total() {
    return sprintf( "%.2f", $this->info[ 'quantity' ] * $this->info[ 'pricing' ] + $this->info[ 'setupcost' ] );
  }

  function display_line_item( $suffix=' )
  {
    // print some crap
  }

}
 
Hi,

I have a question which probaly is a little dumb question. My question is during the shopping cart process at what point we update the inventory table (subtract items from the inventory).

1. Do we keep updating the table the moemnt someone enters a item in the shopping cart?, OR

2. do we update when check out process is complete and custonmer has actually bought the product and cedit card goes through?

if the answer is 2, then do we do the inventory check again (like we did before entering the items in the shopping cart) just before person is about to check out , to make sure the product is stil avaiable.

Thanks

 
Hi


You can remove products from inventory only when consumer has paid for it.

Before processing you can check things which he has in shopping cart or not.

I use version when i put products ID and quantity into shoppping cart and when client is going to pay - the shopping cart is revalidated again, product are extracted from store, their value is added.

Webdesign
 
>>1. Do we keep updating the table the moemnt someone >>enters a item in the shopping cart?, OR

Quantity will never be correct, ie quantity is 5, you have 5 buyers, and each has 1 item in their cart. So the 6th buyer cannot purchase your product. Now 3 buyers walk off, or leave your site. Your inventory is bougus and shows no items.


>>2. do we update when check out process is complete and >>custonmer has actually bought the product and cedit card >>goes through?


Inventory is 5, and 5 buyers each purchase 5 items. Buyer 1 completes the sale, and now buyer 2, and you have 0 inventory.

You have to have a way to lock, and keep track of pending items. Either keep a actual quantity and pending quantity of which total (actual - pending) is used for the buyer trying to add to his cart. Cleanup will be necessary for those you lose connection/ or abandon your End of Day process would update pending to actual.

I use the transaction file to get my pending, with a simple SQL query, to get quantity on hand. A simple task runs from time to time and moves those transactions to a abandoned table when they become so many minutes old, which is a tunable parm.

AT reconciliation, the transactions move to the invoicing and actually update the quantity on hand.
 
cdlvj i understand when you are talking about pending and actually invetory and then updating pending etc

What I am wondering

1. How do you find out that the customer has left your website or connection has got disconnected?

2. Senondly then how do you execute the code to update the pending table one connection has droped or customer has left your website?


This might be a simple thing for someone good in PHP, but I am a beginer in PHP so please bear with me.

Thanks

 
As far as your nr.2 here, I guess you can make a field "reserved", 0 or 1, default 0.

Then you have a reserved_date.

You then use the reserved date,to check if the customer did actually want his/her product. Maybe you give him/her 24 hrs to finish the order.. maybe less? I dont know what is good practice.

I know some web-shops show 1-10 items, 10-20 items, 20-100 items.

They do not tell you that they have 3 or 9 items! Only 1-10! I think this is due to some reserved function and they do not wish to confuse the "dumb" users.
 

>>>>1. How do you find out that the customer has left your >>>>website or connection has got disconnected?

>>>>2. Senondly then how do you execute the code to update >>>>the pending table one connection has droped or >>>>customer has left your website?

First, you store the connection id and date/time in your transaction.

You have to run a program in the background like every 5 minutes. Get all the pending transactions, and move them if they are so many hours old. For the newer transactions, you check the connections, to see if they are still active. There should be a function to do that, do not recall what it is.
 
It would be much easier if it is a PHP script. It can run independently, but that part has to be created when PHP is installed. There can be security issues for stand alone PHP. On UNIX, you use cron, and on Microsoft Scheduled tasks.
 
ask them if you can run crontabs..
if not, simply make your script delete WHERE expired.
 
You probably won't be able to run PHP stand-alone either.
I guess the script could be run each time the home page is accessed?
 
as DaButcher stated,
telnet into your host with your user/password

execute the following

crontab -l


should 1. see if you access
2. list out your crons

if that is ok, create a file for your crons, look at man crontab
and run
crontab <yourfile

on most systems, a user can do crons.
 
I noticed when you use PayPal it is difficult to do the inventory controll. In my shopping cart logic right before buying I see to make sure that products not been sitting in the cart for more than 1 hour. I do not allow to customers to hold the products in the cart for more than one hour. Any product for longer than one hour I took it out from the shopping cart so others can buy that. But in case of PayPal once they leave my site i don't have any control over that or anyway of knowing for how long they wil be sitiing at Paypal site...

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top