PHP E-Commerce Shopping Cart Application step by step

PHP E-Commerce Shopping Cart Application step by step

In this Post We Will Explain About is PHP E-Commerce Shopping Cart Application step by step With Example and Demo.Welcome on Pakainfo.com – Examples, The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to Create Simple Shopping Cart with PHP and MysqlExample

In this post we will show you Best way to implement PHP Shopping Cart Tutorials : simple shopping cart in php source code, hear for PHP E-Commerce Shopping Cart Scriptswith Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.

Simple Shopping Cart with PHP

There are the following step by step Simple shopping Cart Application using PHP
1. Import simple database “mysql_import.sql” in your MySql login to PhpMyAdmin to create “item” table.
2. Change simple settings in database connection “config.php” for your database.
3. Navigate to simple default index page of your simple shopping cart and Good Luck.

PHP Shopping Cart Sample files Structure

images
style
online_cart_update.php
config.php
index.php
mysql_import.sql
online_view_cart.php

mysql_import.sql

CREATE TABLE IF NOT EXISTS `items` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `Item_Code` varchar(60) NOT NULL,
  `item_name` varchar(60) NOT NULL,
  `item_desc` tinytext NOT NULL,
  `item_img_name` varchar(60) NOT NULL,
  `cart_price` decimal(10,2) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `Item_Code` (`Item_Code`)
) AUTO_INCREMENT=1 ;

--
-- Dumping data for table `items`
--

INSERT INTO `items` (`id`, `Item_Code`, `item_name`, `item_desc`, `item_img_name`, `cart_price`) VALUES
(1, 'PD1001', 'red-MI Phone FX1', 'Di sertakan Pakainfo.com rambang live24u lansung tidak munasabah. Jika dalana ingin Pakainfo.com fake website free Download, itfiddle perlu itfiddle bahwa tiada apa live24u', 'android-phone.jpg', 200.50),
(2, 'PD1002', 'Pakainfo.com DXT', 'Ia Pakainfo.com kamus live24u mengandungi itfiddle 800 ayat Pakainfo.com, bersama model dan itfiddle ayat Latin, more Pakainfo.com fake website free Download live24u munasabah.', 'live24ucom-tv.jpg', 500.85),
(3, 'PD1003', 'more Hard Disk', 'Ada Pakainfo.com simple dari mukasurat-mukasurat fake website free Download live24u sedia ada, tetapi ashnbgiakam telah diubahsuai, natak jenaka karma, atau ayat ayat live24u', 'more-hard-disk.jpg', 100.00),
(4, 'PD1004', 'lovely ghadiyar GE2', 'jiten akan terselit didalam di tengah tengah nests text. gana injin fake website free Download didalam step by step hanya new cretae  text, dodiya menjadikan injin demao sebagai live24u terunggul dan bhatyat sekali di step by step.', 'lovely-watch.jpg', 400.30);

config.php

 12, 
                            'Service Tax' => 5
                            );						
//simple connect to MySql Pakainfo.com						
$mysqli = new mysqli($db_host, $db_username, $db_password,$db_name);						
if ($mysqli->connect_error) {
    die('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error);
}
?>

index.php






Shopping Cart




Items

0) { echo '
'; echo '

Your Shopping Cart

'; echo '
'; echo ''; echo ''; $all_total =0; $b = 0; foreach ($_SESSION["cart_items"] as $online_cart_item) { $item_name = $online_cart_item["item_name"]; $item_qty = $online_cart_item["item_qty"]; $item_cart_price = $online_cart_item["item_cart_price"]; $Item_Code = $online_cart_item["Item_Code"]; $item_color = $online_cart_item["item_color"]; $bk_color = ($b++%2==1) ? 'odd' : 'even'; //zebra stripe echo ''; echo ''; echo ''; echo ''; echo ''; $suball_total = ($item_cart_price * $item_qty); $all_total = ($all_total + $suball_total); } echo ''; echo ''; echo '
Qty '.$item_name.' Remove
'; echo 'Checkout'; echo '
'; $cl_url = urlencode($url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']); echo ''; echo '
'; echo '
'; } ?> query("SELECT Item_Code, item_name, item_desc, item_img_name, cart_price FROM items ORDER BY id ASC"); if($data_res){ $items_item = '
    '; //fetch data_res set as object and output HTML while($obj = $data_res->fetch_object()) { $items_item .= <<<EOT
  • {$obj->item_name}

    item_img_name}">
    {$obj->item_desc}
    Online Price {$currency}{$obj->cart_price}
    Item_Code}" />
  • EOT; } $items_item .= '
'; echo $items_item; } ?>

online_view_cart.php






View shopping cart


View Cart

<?php if(isset($_SESSION["cart_items"])) //check session var { $all_total = 0; //set initial all_total value $b = 0; //var for zebra stripe table foreach ($_SESSION["cart_items"] as $online_cart_item) { //set variables to use in content below $item_name = $online_cart_item["item_name"]; $item_qty = $online_cart_item["item_qty"]; $item_cart_price = $online_cart_item["item_cart_price"]; $Item_Code = $online_cart_item["Item_Code"]; $item_color = $online_cart_item["item_color"]; $suball_total = ($item_cart_price * $item_qty); //calculate Online Price x Qty $bk_color = ($b++%2==1) ? 'odd' : 'even'; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; $all_total = ($all_total + $suball_total); //add suball_total to all_total var } $grand_all_total = $all_total + $online_s_cost; //grand all_total including shipping cost foreach($online_all_taxes as $key => $value){ //list and calculate all online_all_taxes in array $all_txt_inc_amount = round($all_total * ($value / 100)); $tax_item[$key] = $all_txt_inc_amount; $grand_all_total = $grand_all_total + $all_txt_inc_amount; //add tax val to grand all_total } $list_tax = ''; foreach($tax_item as $key => $value){ //List simple all online_all_taxes $list_tax .= $key. ' : '. $currency. sprintf("%01.2f", $value).'
'; } $online_s_cost = ($online_s_cost)?'Shipping Cost : '.$currency. sprintf("%01.2f", $online_s_cost).'
':''; } ?>
QuantityNameOnline PriceTotalRemove
'.$item_name.''.$currency.$item_cart_price.''.$currency.$suball_total.'
Amount Payable :
Add More Items
<input type="hidden" name="live_rurl" value="" />

online_cart_update.php

0)
{
	foreach($_POST as $key => $value){ //add all post vars to cnew_prod array
		$cnew_prod[$key] = filter_var($value, FILTER_SANITIZE_STRING);
    }
	//remove unecessary vars
	unset($cnew_prod['type']);
	unset($cnew_prod['live_rurl']); 
	
 	//we need to get item name and cart_price from database.
    $myquery = $mysqli->prepare("SELECT item_name, cart_price FROM items WHERE Item_Code=? LIMIT 1");
    $myquery->bind_param('s', $cnew_prod['Item_Code']);
    $myquery->execute();
    $myquery->bind_result($item_name, $cart_price);
	
	while($myquery->fetch()){
		
		//fetch item name, cart_price from db and add to cnew_prod array
        $cnew_prod["item_name"] = $item_name; 
        $cnew_prod["item_cart_price"] = $cart_price;
        
        if(isset($_SESSION["cart_items"])){  //if session var already exist
            if(isset($_SESSION["cart_items"][$cnew_prod['Item_Code']])) //check item exist in items array
            {
                unset($_SESSION["cart_items"][$cnew_prod['Item_Code']]); //unset old array item
            }           
        }
        $_SESSION["cart_items"][$cnew_prod['Item_Code']] = $cnew_prod; //update or create item session with new item  
    } 
}
//update or remove items 
if(isset($_POST["item_qty"]) || isset($_POST["delete_code"]))
{
	//update item quantity in item session
	if(isset($_POST["item_qty"]) && is_array($_POST["item_qty"])){
		foreach($_POST["item_qty"] as $key => $value){
			if(is_numeric($value)){
				$_SESSION["cart_items"][$key]["item_qty"] = $value;
			}
		}
	}
	//remove an item from item session
	if(isset($_POST["delete_code"]) && is_array($_POST["delete_code"])){
		foreach($_POST["delete_code"] as $key){
			unset($_SESSION["cart_items"][$key]);
		}	
	}
}
$live_rurl = (isset($_POST["live_rurl"]))?urldecode($_POST["live_rurl"]):''; //return url
header('Location:'.$live_rurl);

?>

Example

I hope you have Got What is Create Simple Shopping Cart with PHP and Mysql And how it works.I would Like to have FeadBack From My Blog(Pakainfo.com) readers.Your Valuable FeadBack,Any Question,or any Comments abaout This Article(Pakainfo.com) Are Most Always Welcome.

Leave a Comment