- How to Build a Persistent Shopping Cart in PHP
- HTML for the Product Gallery and the Shopping Cart
- PHP Code to Perform Add Remove Empty Cart Action
- Leave a Reply Cancel reply
- Popular Articles
- Simple PHP Shopping Cart
- PHP Shopping Cart Software Development Overview
- Create a Product Gallery for Shopping Cart
- Adding Products to Shopping Cart
- List Cart Items from the PHP Session
- Removing or Clearing Cart Item
- Database Product Table for Shopping Cart
- Simple PHP Shopping Cart Output
- Popular Articles
How to Build a Persistent Shopping Cart in PHP
A persistent shopping cart application will be used to preserve the user’s cart item until the user processes checkout or clears the cart over different sessions and also via multiple devices. In a previous tutorial, we have created a session-based shopping cart by storing cart info in user session using PHP SESSION array.
Shopping cart items in the session will be cleared at the time of logout or closing the window once the user closes his session. In this example, I stored the added cart item in a database table to provide persistence shopping cart.
This example contains a database table to store cart information. When the user clicks the ‘Add to Cart’ button, the selected product id, quantity and the logged in member id is stored in the cart table.
The member id is pre-defined at the start of the program where you can integrate your authentication module to get the logged in member id. This shopping cart example contains the functionalities to add items to the cart, remove a single item from the cart and empty the cart table.
The following screenshot shows the output of the persistent shopping cart example program.
HTML for the Product Gallery and the Shopping Cart
This HTML code is used to list the product in a gallery view. It shows the persistent cart items read from the database table until the user clears the cart. In the product gallery, each product tile has an Add to Cart button to insert cart item to the database table. The cart list contains options to remove individual or whole cart items.
Shopping Cart getMemberCartItem($member_id); if (! empty($cartItem)) < $item_total = 0; ?> Name Code Quantity Price Action " > ?> Total:
?>
PHP Code to Perform Add Remove Empty Cart Action
This PHP code contains a switch case to handle the Add, Remove and Empty cart actions. The ShoppingCart class contains PHP functions to perform these cart actions.
In this functions, I prepare query statements and the query parameters array which will be bind together to make the requested change for the shopping cart.
getProductByCode($_GET["code"]); $cartResult = $shoppingCart->getCartItemByProduct($productResult[0]["id"], $member_id); if (! empty($cartResult)) < // Update cart item quantity in database $newQuantity = $cartResult[0]["quantity"] + $_POST["quantity"]; $shoppingCart->updateCartQuantity($newQuantity, $cartResult[0]["id"]); > else < // Add to cart table $shoppingCart->addToCart($productResult[0]["id"], $_POST["quantity"], $member_id); > > break; case "remove": // Delete single entry from the cart $shoppingCart->deleteCartItem($_GET["id"]); break; case "empty": // Empty cart $shoppingCart->emptyCart($member_id); break; > > ?>
getDBResult($query); return $productResult; > function getMemberCartItem($member_id) < $query = "SELECT tbl_product.*, tbl_cart.id as cart_id,tbl_cart.quantity FROM tbl_product, tbl_cart WHERE tbl_product.id = tbl_cart.product_id AND tbl_cart.member_id = ?"; $params = array( array( "param_type" =>"i", "param_value" => $member_id ) ); $cartResult = $this->getDBResult($query, $params); return $cartResult; > function getProductByCode($product_code) < $query = "SELECT * FROM tbl_product WHERE code=?"; $params = array( array( "param_type" =>"s", "param_value" => $product_code ) ); $productResult = $this->getDBResult($query, $params); return $productResult; > function getCartItemByProduct($product_id, $member_id) < $query = "SELECT * FROM tbl_cart WHERE product_id = ? AND member_id = ?"; $params = array( array( "param_type" =>"i", "param_value" => $product_id ), array( "param_type" => "i", "param_value" => $member_id ) ); $cartResult = $this->getDBResult($query, $params); return $cartResult; > function addToCart($product_id, $quantity, $member_id) < $query = "INSERT INTO tbl_cart (product_id,quantity,member_id) VALUES (?, ?, ?)"; $params = array( array( "param_type" =>"i", "param_value" => $product_id ), array( "param_type" => "i", "param_value" => $quantity ), array( "param_type" => "i", "param_value" => $member_id ) ); $this->updateDB($query, $params); > function updateCartQuantity($quantity, $cart_id) < $query = "UPDATE tbl_cart SET quantity = ? WHERE $params = array( array( "param_type" =>"i", "param_value" => $quantity ), array( "param_type" => "i", "param_value" => $cart_id ) ); $this->updateDB($query, $params); > function deleteCartItem($cart_id) < $query = "DELETE FROM tbl_cart WHERE $params = array( array( "param_type" =>"i", "param_value" => $cart_id ) ); $this->updateDB($query, $params); > function emptyCart($member_id) < $query = "DELETE FROM tbl_cart WHERE member_id = ?"; $params = array( array( "param_type" =>"i", "param_value" => $member_id ) ); $this->updateDB($query, $params); > >
Leave a Reply Cancel reply
Popular Articles
Simple PHP Shopping Cart
Building a PHP shopping cart eCommerce software is simple and easy. In this tutorial, let’s create a simple PHP shopping cart software with MySQL.
The intention of this shopping cart software is that it should be simple and as minimal as possible. You can download this free and customize it for your needs within minutes.
PHP Shopping Cart Software Development Overview
PHP shopping cart example has the following functionality:
- Retrieve product information from the database.
- Create product gallery for the shopping cart.
- Manage cart items using the PHP session.
- Handle add, edit, remove and empty cart actions.
I have retrieved information like name, code, price, and photos from the database. The resultant information is in an array format.
I have iterated this resultant array to form the product gallery. Every product in the gallery will have an add-to-cart option.
I have used the PHP shopping cart session to store and manage the items in the cart.
Once the session expires, the cart items get cleared. This code has an option to clear the entire cart or to remove any particular item from the cart.
File Structure
The shopping cart software example has the following file structure. The below list has the file names and their responsibility.
You can build a gallery-based shopping cart software with these files in few minutes.
- dbcontroller.php – a generic database layer to help with DAO functions. It also manages the database connection.
- index.php – to display the product gallery for the shopping cart.
- style.css – to showcase products for the shopping cart. The styles are minimal and cross-browser compatible.
- tblproduct.sql – contains SQL script with the product table structure and the data.
- product-images – a folder that contains the product images. I have used these images to show the product gallery.
Create a Product Gallery for Shopping Cart
If you are familiar with online shopping, you might have across product galleries. It is an important gateway in a shopping cart application.
Users will use the product gallery to get a feel of the products available to buy. It is a critical component of every online store.
You should always provide a catalog of the available products to let them have a glance. It helps to promote your products to impulsive buyers.
In the following code, I have shown the PHP script to get the product results from the database. The result will be an array and iterated to create a product card in each iteration.
The DBController.php class will handle the DAO operation and fetch products result.
runQuery("SELECT * FROM tblproduct ORDER BY id ASC"); if (!empty($product_array)) < foreach($product_array as $key=>$value) < ?>