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





<title>Shopping Cart</title>




<h1 align="center">Items </h1>

<!-- Pakainfo.com	 View Cart Box Start -->
0)
{
	echo '<div class="cart-view-table-front" id="view-cart">';
	echo '<h3>Your Shopping Cart</h3>';
	echo '<form method="post" action="online_cart_update.php">';
	echo '<table width="100%" cellpadding="6" cellspacing="0">';
	echo '<tbody>';

	$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 '<tr class="'.$bk_color.'">';
		echo '<td>Qty </td>';
		echo '<td>'.$item_name.'</td>';
		echo '<td> Remove</td>';
		echo '</tr>';
		$suball_total = ($item_cart_price * $item_qty);
		$all_total = ($all_total + $suball_total);
	}
	echo '<td colspan="4">';
	echo '<button type="submit">Update</button><a href="online_view_cart.php" class="button">Checkout</a>';
	echo '</td>';
	echo '</tbody>';
	echo '</table>';
	
	$cl_url = urlencode($url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
	echo '';
	echo '</form>';
	echo '</div>';

}
?>
<!--Pakainfo.com	 View Cart Box End -->
<!-- Pakainfo.com	Items List Start -->
query("SELECT Item_Code, item_name, item_desc, item_img_name, cart_price FROM items ORDER BY id ASC");
if($data_res){ 
$items_item = '<ul class="items">';
//fetch data_res set as object and output HTML
while($obj = $data_res->fetch_object())
{
$items_item .= <<<EOT
	<li class="item">
	<form method="post" action="online_cart_update.php">
	<div class="item-content"><h3>{$obj->item_name}</h3>
	<div class="item-thumb"><img>item_img_name}"></div>
	<div class="item-desc">{$obj->item_desc}</div>
	<div class="item-info">
	Online Price {$currency}{$obj->cart_price} 
	
	<fieldset>
	
	<label>
		<span>Color</span>
		
		Black
		Silver
		
	</label>
	
	<label>
		<span>Quantity</span>
		
	</label>
	
	</fieldset>
	Item_Code}" />
	
	
	<div align="center"><button type="submit" class="add_to_cart">Add</button></div>
	</div></div>
	</form>
	</li>
EOT;
}
$items_item .= '</ul>';
echo $items_item;
}
?>    
<!-- Pakainfo.com Items List End -->


online_view_cart.php





<title>View shopping cart</title>


<h1 align="center">View Cart</h1>
<div class="cart-view-table-back">
<form method="post" action="online_cart_update.php">
<table width="100%" cellpadding="6" cellspacing="0"><thead><tr><th>Quantity</th><th>Name</th><th>Online Price</th><th>Total</th><th>Remove</th></tr></thead>
  <tbody>
 	<?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 '<tr class="'.$bk_color.'">';
			echo '<td></td>';
			echo '<td>'.$item_name.'</td>';
			echo '<td>'.$currency.$item_cart_price.'</td>';
			echo '<td>'.$currency.$suball_total.'</td>';
			echo '<td></td>';
            echo '</tr>';
			$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).'<br />';
		}
		$online_s_cost = ($online_s_cost)?'Shipping Cost : '.$currency. sprintf("%01.2f", $online_s_cost).'<br />':'';
	}
    ?>
    <tr><td colspan="5"><span style="float:right;text-align: right">Amount Payable : </span></td></tr>
    <tr><td colspan="5"><a href="index.php" class="button">Add More Items</a><button type="submit">Update</button></td></tr>
  </tbody>
</table>
<input type="hidden" name="live_rurl" value="" />
</form>
</div>


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

Also Read :  Simple Ecommerce Shopping Cart PHP mysql

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 Reply

Your email address will not be published. Required fields are marked *