Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ErMandeep/b94743af4c50a4748a7793fcd1051bfc to your computer and use it in GitHub Desktop.
Save ErMandeep/b94743af4c50a4748a7793fcd1051bfc to your computer and use it in GitHub Desktop.
add to cart using session and sweet alert
<?php
if(isset($_GET['view'])){
$v_id = $_GET['view'];
}
$query = "SELECT * FROM products WHERE id = $v_id";
$select_image = mysqli_query($connection, $query);
while($row = mysqli_fetch_assoc($select_image)){
$id = $row['id'];
$product_name = $row['product_name'];
$price = $row['price'];
}
?>
// this is for sending data into seeion variable
<input type="hidden" name="hidden_name" id="name<?php echo $id; ?>" value="<?php echo $product_name; ?>" />
<input type="hidden" name="hidden_price" id="price<?php echo $id; ?>" value="<?php echo $price; ?>" />
<!-- <input type="text" name="hidden_price" id="price'.<?php echo $id; ?>'" value="price<?php echo $id; ?>" /> -->
<input type="hidden" name="quantity" id="quantity<?php echo $id; ?>" class="form-control" value="1" />
<!-- <input type="button" name="add_to_cart" class="flex-c-m stext-101 cl0 size-101 bg1 bor1 hov-btn1 p-lr-15 trans-04 js-addcart-detail" value="Add to cart <?php echo $id; ?>"> -->
<button class="flex-c-m stext-101 cl0 size-101 bg1 bor1 hov-btn1 p-lr-15 trans-04 js-addcart-detail add_to_cart" name="add_to_cart" id="<?php echo $id; ?>">
Add to cart <?php echo $id; ?>
</button>
//
ajax request for add to card into session variable
$(document).on('click', '.add_to_cart', function(){
var product_id = $(this).attr("id");
var product_name = $('#name'+product_id+'').val();
var product_price = $('#price'+product_id+'').val();
var product_quantity = $('#quantity'+product_id).val();
var action = "add";
if(product_quantity > 0)
{
$.ajax({
url:"action.php",
method:"POST",
data:{product_id:product_id, product_name:product_name, product_price:product_price, product_quantity:product_quantity, action:action},
success:function(data)
{
// load_cart_data();
// location.reload();
// alert("Item has been Added into Cart");
}
});
}
else
{
alert("lease Enter Number of Quantity");
}
});
//action.php (create this file)
<?php
//action.php
session_start();
if(isset($_POST["action"]))
{
if($_POST["action"] == "add")
{
if(isset($_SESSION["shopping_cart"]))
{
$is_available = 0;
foreach($_SESSION["shopping_cart"] as $keys => $values)
{
if($_SESSION["shopping_cart"][$keys]['product_id'] == $_POST["product_id"])
{
$is_available++;
$_SESSION["shopping_cart"][$keys]['product_quantity'] = $_SESSION["shopping_cart"][$keys]['product_quantity'] + $_POST["product_quantity"];
}
}
if($is_available == 0)
{
$item_array = array(
'product_id' => $_POST["product_id"],
'product_name' => $_POST["product_name"],
'product_price' => $_POST["product_price"],
'product_quantity' => $_POST["product_quantity"]
);
$_SESSION["shopping_cart"][] = $item_array;
}
}
else
{
$item_array = array(
'product_id' => $_POST["product_id"],
'product_name' => $_POST["product_name"],
'product_price' => $_POST["product_price"],
'product_quantity' => $_POST["product_quantity"]
);
$_SESSION["shopping_cart"][] = $item_array;
}
}
if($_POST["action"] == 'remove')
{
foreach($_SESSION["shopping_cart"] as $keys => $values)
{
if($values["product_id"] == $_POST["product_id"])
{
unset($_SESSION["shopping_cart"][$keys]);
}
}
}
if($_POST["action"] == 'empty')
{
unset($_SESSION["shopping_cart"]);
}
}
// echo $_SESSION["shopping_cart"];
foreach($_SESSION["shopping_cart"] as $result) {
echo "<pre>";
print_r($result);
// var_dump($result);
// echo $result["product_quantity"];
echo "</pre>";
// foreach($_SESSION["shopping_cart"] as $keys => $values)
// {
// echo "<pre>";
// echo $values["product_quantity"];
// echo "</pre>";
// $item = $values["product_quantity"];
// }
// echo $item;
}
$itemtotal = 0;
foreach($_SESSION["shopping_cart"] as $item) {
$itemtotal += $item['product_quantity'];
}
echo $itemtotal; // output 5
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment