Super Simple Shopping Cart for PHP


PHP Super Simple Shopping Cart System



About

This is a very simple, yet fully functional shopping cart system programmed in PHP. Usage and integration is simple, no database required. At the moment, their is no payment module. This is only a shopping cart solution. Come back to Unibia.net for payment modules as I may add them in. Orders along with payment information is emailed.

Requirements

  • Tested on PHP 5.2.x
  • PHP must have SESSION support enabled

How it Works

The data structure is a multidimensional array. All the items are stored in an array (the shopping cart) which is stored in the $_SESSION variable. Each item is it's own array that keeps information such as the internal item ID, Quantity, Description, and Price. The end user never sees the internal item ID.

The shopping cart is managed by passing URI parameters as GET or POST requests. A POST request is used only to manipulate an existing item's quantity. The GET requests allow you to add an item, empty the cart, or remove a single item. Most of the time, the only method you will need to manual create is the item add URI. The other methods have GUI controls built in.

How to use (Example)

At minimum you must adjust lines 99 and 100 so you and your costumers can receive email notification about your orders.

<?php$adminEmail '<a href="mailto:info@example.com">info@example.com</a>';$companyName 'Unibia.net';?>

Next Upload the script to your PHP enabled web server. The file name does not matter just as long as your other pages link to the correct file.

Add products to the shopping cart with a simple HTTP "GET" request with the following URI parameters:

  • add={product name as string}
  • price={decimal or integer}
  • qty={integer}

A request to add an item would look like this:

http(s)://www.{website}.com/{script file name}.php?add=ChopSticks&price=3.00&qty=1

*Spaces are allowed in the products name.

Where to get it

Try it out at: http://www.unibia.com/unibia/dev/examples/SuperSimpleShoppingCart/
Download it at: http://www.unibia.com/unibia/dev/downloads/SuperSimpleShoppingCart.tar

Comments

Sun, 01/29/2012 - 17:03

I put the cute little script up for a test,

http://urform.dk/urshop.htm

but I do not receive any mail either as customer or as an administrator even though I have added my email address in line 99 of the script as directed. Is an SMTP thing. email blocked by spam filters?
Can you figur out what is wrong??

Mikkel

Mon, 11/18/2013 - 10:46

Your ISP may have disabled the PHP "mail()" function. This function is required in order for PHP to send out email notifications.

Hi we have added a piece of code to ensure that the total appears below the list of articles, so that the mail sent to the user can be used as an order confirmation message. Please send a mail to newmind@mindnew.com if somebody is interested in this add-on .

Aother item: while the "remove" hypertext is handy when the cart is filled in, we would like to get rid of it in the mail to the client and to the seller .
Does somebody have an idea?
Alzir

Mon, 11/18/2013 - 10:54

When I originally wrote this I was very lazy, but the following "hack" should get rid of the "remove" links in the email.

Change line 271 from:

sendEmail($_SESSION['SHOPPING_CART_HTML']);

To:

sendEmail(preg_replace(array('/<td>.*href="\?remove.*<\/td>/i', '/<th.*&nbsp;<\/th>/i'), '', $_SESSION['SHOPPING_CART_HTML']));

Greetings all,

Great little script here, lots you can do with it. I have one last thing to work out and that is making sure the cart is empty at the start a new session.

I'm running Joomla 2.5. I have the user login and they are directed to a page that is displaying the cart items with the appropriate tags attached for the cart. I have these pages being generated with a module that runs some sql queries, very cool, works quite well. I've been able to bring in a couple of the joomla user variables and use them in the sql queries to setup customer specific pricing which is a feature our company really wanted.

My issue is that unless the cart is emptied with the "Empty" button or the sale is completed, I get items hanging in the cart. When a new user logs in he has those items sitting in his cart too. Awkward.

Any help would be greatly appreciated.

glennt@findlayfoods.com