John Davidson

php - Add quantity of goods in cart

0 comments
Message:


I want to add possibility to change quantity of goods in cart but I don't have idea how to do this. I tried to write function but it just deleted current session.
I want to do this without javascript, only php. Quantity should be saving in current session..


<?php

require_once "db.php";

$quantity = 1;
if ( isset($_GET['delete_id']) && isset($_SESSION['cart_list']) ) {
$_SESSION['cart_list'] = array_filter($_SESSION['cart_list'], function($v) {
return $v['id'] != $_GET['delete_id'];
});
}

if ( isset($_GET['single_id']) && !empty($_GET['single_id']) ) {
$current_added_good = get_single_by_id($_GET['single_id']);
if ( !empty($current_added_good) ) {
if ( !isset($_SESSION['cart_list'])) {
$_SESSION['cart_list'][] = $current_added_good;
}
$single_check = false;
if ( isset($_SESSION['cart_list']) ) {
foreach ($_SESSION['cart_list'] as $value) {
if ( $value['id'] == $current_added_good['id'] ) {
$single_check = true;
}
}
}
if ( !$single_check ) {
$_SESSION['cart_list'][] = $current_added_good;
}
} else {
}
}
?>
<?php if ( isset($_SESSION['cart_list']) && count($_SESSION['cart_list']) !=0 ) : ?>
<br>
<?php $s = 0; ?>
<?php foreach( $_SESSION['cart_list'] as $single ) : ?>
<h4 class='font-weight-bold blue-text'>
<strong><?php echo $single['price'];?>$</strong>
</h4>
<a class="plus">
<i class="fas fa-plus" style='color: Blue'></i>
</a>
<br>
<div class="cart_quantity"><?php echo $quantity;?></div>
<a class="">
<i class="fas fa-minus" style='color: Blue'></i>
</a>
<br>
<hr>
<a href="cart.php?delete_id=<?php echo $single['id'];?>">
<i class='fas fa-times' style='color: Red'></i>
</a>
<hr>
<section class='text-center mb-4 font-weight-bold display-4'>
<label class="count" id="summ"><?php echo $s; ?>$</label>
</section>


My Answer:
You can achieve this by creating a form with input fields for each item in the cart where users can input the desired quantity. When the form is submitted, you can update the quantity in the session for each item.

Here's an example of how you can implement this:

1. Create a form in your cart page to allow users to update the quantity of each item:

html

$item) : ?>







2. Create a PHP script (update_cart.php) to handle the form submission and update the quantity in the session:

php
session_start();

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
foreach ($_POST['item_id'] as $key => $item_id) {
$_SESSION['cart'][$item_id]['quantity'] = $_POST['quantity'][$key];
}
}

header('Location: cart.php');


3. Make sure to start the session at the beginning of your cart page and populate the cart session with some sample data:

php
session_start();

if (!isset($_SESSION['cart'])) {
$_SESSION['cart'] = [
1 => ['name' => 'Product 1', 'quantity' => 1],
2 => ['name' => 'Product 2', 'quantity' => 1],
3 => ['name' => 'Product 3', 'quantity' => 1],
];
}


With this setup, users can update the quantity of each item in the cart using the form, and the updated quantities will be saved in the session.

Rate this post

3 of 5 based on 5310 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us