AngularJS Shopping Cart With PHP MySQL

Using AngularJS here we going to create online shopping cart application with PHP MySQL. Now with the AngularJS, we going to fetch and display the product details then you can add the products under shopping bag, also you can remove the products on the same page without page refreshing. For the backend, we used PHP this will help to fetch the product name, price, and image to display on the front view with the help of AngualrJS. Using PHP SESSION we store the shopping bag data and this will be used anywhere on the web pages. Once the user closed the window or refreshed the page, PHP SESSION will help to store the shopping bag details and will avoid the losing data issues.
This online shopping cart application help to buy products that products all are added on the same page without refreshing. If the user likes to buy a product means they can add the products and there is shopping bag in that only added lists show with the product name, quantity, product price and total amount of your shopping bag, from there you need to integrate any payment gateway for checkout.
Database
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
-- -- Database: `tut` -- -- -------------------------------------------------------- -- -- Table structure for table `sa_product` -- CREATE TABLE `sa_product` ( `id` int(11) NOT NULL, `pro_name` varchar(255) NOT NULL, `pro_price` varchar(255) NOT NULL, `pro_image` varchar(255) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1; -- -- Dumping data for table `sa_product` -- INSERT INTO `sa_product` (`id`, `pro_name`, `pro_price`, `pro_image`) VALUES (1, 'Apple iPhone XS', '729', 'Apple-iPhone-XS.png'), (2, 'Apple Macbook Pro', '1299', 'apple-macbook-pro.jpg'), (3, 'Apple Watch Series 4', '399', 'apple-watch-series-4.png'), (4, 'Apple AirPods', '199', 'apple-airpods.png'); -- -- Indexes for dumped tables -- -- -- Indexes for table `sa_product` -- ALTER TABLE `sa_product` ADD PRIMARY KEY (`id`); -- -- AUTO_INCREMENT for dumped tables -- -- -- AUTO_INCREMENT for table `sa_product` -- ALTER TABLE `sa_product` MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=5; COMMIT; |
conn.php
1 2 3 4 5 6 7 8 |
<?php $host = "localhost"; $db_user = "root"; $db_pass = ""; $dbname = "tut"; $connect = new PDO("mysql:host=$host; dbname=$dbname", $db_user, $db_pass); ?> |
fetch.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?php // DB Connection include('conn.php'); $query = "SELECT * FROM sa_product"; $statement = $connect->prepare($query); $statement->execute(); while($row = $statement->fetch(PDO::FETCH_ASSOC)) { $data[] = $row; } echo json_encode($data); ?> |
fetch_bag.php
1 2 3 4 5 6 7 8 |
<?php session_start(); if(isset($_SESSION["shopping_bag"])) { echo json_encode($_SESSION["shopping_bag"]); } ?> |
add_product.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
<?php session_start(); $proInfo = json_decode(file_get_contents("php://input")); $p_id = $proInfo->id; $product_name = $proInfo->pro_name; $product_price = $proInfo->pro_price; if(isset($_SESSION["shopping_bag"])) { $is_available = 0; foreach($_SESSION["shopping_bag"] as $keys => $values) { if($_SESSION["shopping_bag"][$keys]['p_id'] == $p_id) { $is_available++; $_SESSION["shopping_bag"][$keys]['product_qty'] = $_SESSION["shopping_bag"][$keys]['product_qty'] + 1; } } if($is_available == 0) { $item_array = array( 'p_id' => $p_id, 'product_name' => $product_name, 'product_price' => $product_price, 'product_qty' => 1 ); $_SESSION["shopping_bag"][] = $item_array; } } else { $item_array = array( 'p_id' => $p_id, 'product_name' => $product_name, 'product_price' => $product_price, 'product_qty' => 1 ); $_SESSION["shopping_bag"][] = $item_array; } ?> |
delete_product.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?php session_start(); $proInfo = json_decode(file_get_contents("php://input")); $p_id = $proInfo; foreach($_SESSION["shopping_bag"] as $keys => $values) { if($values["p_id"] == $p_id) { unset($_SESSION["shopping_bag"][$keys]); } } ?> |
Mraj
Creative Designer & Developer specialist by the spirit and a loving blogger by thoughts. If you have any questions let me drop an email with the article name to the following email id: [email protected]