Shopping Cart Code Php Free Download

Posted on by

The free and/or open source PHP shopping cart scripts, also known as e-commerce software, listed here allow you to set up an ecommerce shop on your website,where visitors can select products and add them to a shopping cart. The software usually handles things like interfacing with a payment processor, calculation of shipping and taxes, display thumbnailpictures of your products, etc. Some of them may also allow your customers to create printable copies of receipts and the like. Note that a number of thesoftware require you to have a MySQL database on your web server, in addition to PHP facilities.

  1. Shopping Cart Code Php Free Download Pc

You may also be interested in the Free Perl Shopping Carts Scriptspage.

Related Pages

Free / Open Source PHP Shopping Carts and E-Commerce Scripts

May 25, 2017  In this tutorial, we are demonstrating how to make a simple PHP shopping cart tutorial step by step. This shopping cart application wrote as very minimal and simple for learning purpose. One can take this as an easy shopping cart for any website, but this is purely an idea to create a shopping cart.

OpenCart Open Source Shopping Cart Solution

OpenCart lets you create unlimited categories for your products, support multiple currencies, allow product reviews and ratings,which can be useful if you're selling products from different manufacturers (thinkAmazon), sell downloadable goods (likeebooks and software), set multipletax rates, display related products, issue discount coupons, give your customers printable invoices, calculate shipping, etc.It is available in many languages and support a wide variety of payment options. The program is open source andis released under the GNU General Public License. It uses a MySQL database.

PrestaShop Free Open Source e-Commerce Software

PrestaShop features special deals (where you give a price reduction or gift voucher), cross-selling (eg, to sell accessories),unlimited payment methods, payment by bank wire, the ability to order out-of-stock items, product tagging, return of merchandise,shopping cart recall, sale of services and virtual goods, pick up in store option, product search, anaffiliate program,built-in support for PayPal, real-time currency exchange rates, unlimited categories, subcategories,product attribute combinations and specifications,unlimited tax settings, currencies, carriers and destinations, tax by state, country, both state and country, zones or no taxes at all,quantity discounts (digressive pricing), search engine friendly URLs (web addresses), SSL encryption, etc (lots of features). Thesoftware is released under the Open Software License (OSL) and requires PHP 5 or later and MySQL 5 or later. (Read their system requirementsfor more details.) Note: if you are using this software, you may also want to read the articleHow to Install PrestaShopShopping Cart on Your Website for a step-by-step guide.

Zen Cart - Free Open Source Shopping Cart Software

Zen Cart is an open source shopping cart software that uses an XHTML template system so that you can customize it easily, supports unlimited extrapages, has multiple shipping options, payment options, tax support, different currency support,and ad-banner controller. It also allows you to set discount coupons, provide gift certificates,give quantity discounts and special prices, etc. The software is released under the GNU General Public License. This is a PHP script.

osCommerce - Open Source Secure Online E-Commerce Solution

osCommerce is an open source online shop solution that uses PHP and MySQL to manage your shopping cart. It allows you to have an unlimited numberof products and categories, provides an automatic setup of a variety of e-commerce payment systems, supports secure transactions with SSL, many shipping optionsincluding weight, price and destination, taxes (including compounded taxes, etc), database backup and restore, physical products as well as downloads,multi-currency support, etc.

  • How to Register Your Own Domain Name - how to get your own domain name

It will appear on your page as:


Shopping Cart PHP Tutorial: : Add to Cart in PHP Demo

Simple Add to Cart PHP Code

The AJAX PHP MySQL Database Structure

Simple Shopping Cart PHP Code for Listing Products

PHP Shopping Cart Using Session Variables

jQuery Shopping Cart Code

Shopping List PHP Code

Add to Cart in PHP Code with AJAX

Simple Add to Cart PHP Code

The Configuration File

The PHP Cart Class

Download the Simple Shopping Cart PHP Code

Shopping Cart PHP Tutorial: Add to Cart in PHP Demo

Shopping carts are a must for every site that needs to sell products online. This tutorial will show you how to make yourself one using PHP and AJAX.

You will learn how to list your products in a table, add a product to a shopping cart, remove an article from it, empty a shopping cart and how to checkout.

PHP Shopping Cart Code

In this part of the tutorial it is presented code for a complete PHP shopping cart application including the PHP scripts, JavaScript for sending AJAX requests using jQuery to process the shopping cart operations and database structure scripts to store the cart data in a MySQL database.

The AJAX PHP MySQL Database Structure

You do not need a database for managing the shopping cart itself, but because we want to list products for selling, we will need to retrieve the products from some place that it is flexible to update, like for instance a database. We are going to insert some initial product data in there for demonstration purposes.

Let us create one MySQL table named products. It has an ID, product name and a price. You can expand this table as you want. I am keeping it simple as this is not a tutorial about listing and creating products.

I am inserting four products, named them Product 1 to Product 4, the prices are randomly set. Note that the price field is float, prices can be float values.

Simple Shopping Cart PHP Code for Listing Products

Now we will create a page which will list all the products with their prices, as well a link to add the product to the cart.

Let us create index.php script. It will contain a table dynamically generated using a loop through an array, that array will have the products list from the database. I will use a PHP class and will put in it a method which will query the products list and return them in the array.

PHP Shopping Cart Using Session Variables

Shopping carts need to be stored in some place to keep track of the products that the user picks. You can use any type of data storage. In this case I am using PHP sessions. They are easy to access, you can store arrays in them, and differently from database systems, you do not need to connect to them every time you need them, and they do not require the user of any query language. They are simply fast and easy to manipulate.

Create a file named index.php, and add this code in it:

Here we could use a template engine to place the values of products in the page output to separate the script logic from the presentation logic. However, to simplify this tutorial we are using PHP statements in the middle of the HTML code just so you can easily see where everything goes.

In the first couple of lines before the HTML we activate the PHP sessions with the function session_start(), then included two files, config.php for configuration values, and cart.php which will be our class.

Next in the page head section we will need to include the jQuery library and main.js, a Javascript file that we will discuss later. Then before the table I am creating an instance to the cart class (we will discuss that later too) and calling getProducts method to get the products array back. For now we will discuss its details, just know that this method will return us an array with products ID, name and price.

Next with a foreach loop we are creating a table row for every product, listing the name, then price and a span having the product ID in data-id attribute. The span will be used to trigger a JavaScript function which will send the product ID by AJAX for the cart to add the product in the session.

Shopping Cart Code Php Free Download Pc

Finally we show a link to the show-cart.php script where we will list the cart elements.

jQuery Shopping Cart Code

In both front end files we will be using two JavaScript files. One is the jQuery library which we are retrieving from a Content Delivery Network site ( We are using jQuery as it is easy and comfortable. It has extensive AJAX support that will be used in main.js file.


Next let us create a simple JavaScript file named as main.js. It will have this code in it:

In this file we are just registering one event handler, add to cart. It is used in the first front end file I already explained.

Note that we should register the event handlers after the document is fully loaded. This is important for the code to work correctly. We are attaching the event handler to an HTML element. If it is not loaded it will not work.

In index.php there is a span element in every row in the list of products with a class addToCart. We are attaching a click handler to every one, and assigning a function which will be called. The function will first get the ID of the product we clicked on, then we call an AJAX sending to it the ID and action add. The AJAX file will add the product to our cart, on finish we will trigger an alert to let us know that the product is in our cart.

By this JavaScript file we can add products to the cart interface.

Shopping List PHP Code

After adding products in the cart, you need to have a way to view them and show what is in the cart. We will use a script named show-cart.php which is very similar to the one before.

As in index.php I am starting the session so we can access it. Then we are including the config file and the cart class.

In this script we will show the cart entries in a table, every row will have one product with its name. Also we will show the number of items of that product, as it can be bought more than one item of a product.

Of course one more information is required, it is the total price which is the price of a single item of a product multiplied with the number of items in the cart.

Add to Cart in PHP Code with AJAX

In the JavaScript file we used an AJAX request in the event handler. Actually we call one script named ajax.php, every time we gave it an action. Create a file named ajax.php and insert this code in it:

Simple Add to Cart PHP Code

This is a PHP script that helps to interact with the cart class by AJAX, so I am starting session to be able to access the cart in the session. Then including the config file and the cart class. Now we create an instance of the class to access the methods we need.

With every call to this file we sent an action, so we get that action value and handle it in a switch case statement. In this case by providing add we call the addToCart method. It will add the sent product in the cart. Later we will discuss how to remove items and empty the cart.

The Configuration File

Before going on with the cart class, just create a script called config.php and add this code in it:

In this file we are defining the credentials of access to the database required by the database server so we can connect to it and pass authentication. These constants are used in the class. We are separating them into a single script to have easier and safer access to them.

The PHP Cart Class

Finally we are creating the cart class, here we have all the mothods we need for the cart to function. Create a file named as cart.php and copy this code in it.

To simplify this tutorial we have omitted error handling of eventual failures to connect to the database or execute queries, but robust applications should always implement error handling to provide good user experience and help the developer to debug errors when they happen.

First let me explain the connection issue, as we will connect to the database more than once we are using a private member variable which will hold the connection. The connection will be opened on class creation by a constructor and closed by a destructor. I am so using the built-in __construct and __destruct methods to do that.

Shopping cart code php free download for windows 10

Next method is getProducts. We call that method in index.php file. We get the connection object, and make a query to the products table. The data is sorted in an array with a while loop using an empty generic class. The result is returned as an array.

In ajax.php we call addToCart method using the add action. We get the product ID sent via AJAX. If it is valid we test if the cart variable in session is empty. If it is empty we create a new entry, the product ID, count one and add it in session.

If the session variable is not empty, we loop through the cart array. If we find the product, we just increment the count variable. If it is not there we add it as a new element with count one. The changed array is returned back to the session.

By this we will make sure we do not have duplicates in the session, if product already exists we increase the count number by one, if not we add it there.

In show-cart.php we call the method getCart. It simply lists all cart products in an array. The problem we have here is in the cart we have only ID's and count of products, we do not have prices nor product names.

Here the getProductData private method gets handy. For every product in the cart we call it and get all the data needed to list in the array. getProductData method receives a product ID and returns its name and price.

Download the Simple Shopping Cart PHP Code

You can always use some of the e-commerce systems on-line, but they are too complicated and usually are not easy to customized. With this simple class you can customize your shopping cart and keep it simple and low weight. Mix design code book free download.

The class package code is available right here for you to download the ZIP archive or install it using the Composer tool with instructions also on this page.

You can enhance it in many ways to make the class even better to address your needs like adding support for VAT and specify price currency.

In the next part we will how to do other stuff with the shopping cart like remove products or empty the cart.

If you liked this article or you have questions or suggestions, just post comment here. If you have great suggestions to improve the package I may implement your ideas in the class in the future. Just let me know.