I am making an ecommerce system natively with PHP, everything works perfectly.
Except that I can't duplicate the products in the shopping cart when a customer adds the same product but with a different color or size than the one added to the cart.
If the customer chooses the same product again with a different size or color, what he does is update the previous size and its color and this is not ideal, the ideal is to duplicate that product.
Example: (What I want to achieve when adding the same product but with a different size or color)
My code
cart.php
<?php
session_start();
require "config.ini.php";
$itemCount = isset($_SESSION['cart']) ? count($_SESSION['cart']) : 0;
if (isset($_SESSION['qty'])) {
$meQty = 0;
foreach ($_SESSION['qty'] as $meItem){
$meQty = $meQty + $meItem;
}
} else {
$meQty = 0;
}
if (isset($_SESSION['cart']) and $itemCount > 0){
$itemIds = "";
foreach ($_SESSION['cart'] as $itemId){
$itemIds = $itemIds . $itemId . ",";
}
$inputItems = rtrim($itemIds, ",");
$meSql = "SELECT * FROM products WHERE id in ({$inputItems})";
$meQuery = mysqli_query($kcon, $meSql);
$meCount = mysqli_num_rows($meQuery);
} else {
$meCount = 0;
}
if ($meCount == 0){
echo "<div class=\"alert alert-warning\">No hay artículos en la cesta</div>";
}else{
?>
<form action="updatecart.php" method="post" name="fromupdate">
<table class="table">
<tr>
<td>Producto</td>
<td>Precio</td>
<td>Cantidad</td>
<td>Total</td>
</tr>
<?php
$total_price = 0;
$num = 0;
$iva = 12;
$shipping = 50;
while ($meResult = mysqli_fetch_assoc($meQuery)){
$key = array_search($meResult['id'], $_SESSION['cart']);
$total_price = $total_price + ($meResult['price'] * $_SESSION['qty'][$key]);
$intemId=$meResult['id'];
$color = $_SESSION['colors'][$intemId];
$size = $_SESSION['size'][$intemId];
if ($total_price > 50){
$shipping = 0;
}
?>
<tr>
<td class="cart-image">
<img src="<?php echo $meResult['image']; ?>" />
<div class="cart-items">
<h3><?php echo $meResult['product']; ?><?php echo $color; ?><?php echo $size?></h3>
</div>
</td>
<td class="cart-price"><?php echo $meResult['price']; ?></td>
<td>
<input type="text" name="qtyupdate[<?php echo $num; ?>]" value="<?php echo $_SESSION['qty'][$key]; ?>" class="form-control" style="width: 60px;text-align: center;" autocomplete="off" data-validation="number" data-validation-allowing="float">
<input type="hidden" name="arr_key_<?php echo $num; ?>" value="<?php echo $key; ?>">
<a class="btn btn-danger btn-lg" href="removecart.php?itemId=<?php echo $meResult['id']; ?>" role="button">
<span class="glyphicon glyphicon-trash"></span>Eliminar</a>
</td>
<td>$<?php echo number_format(($meResult['price'] * $_SESSION['qty'][$key]),2); ?></td>
<?php
$num++;
}
}
?>
updatecart.php
<?php
session_start();
$itemId = isset($_GET['itemId']) ? $_GET['itemId'] : "";
$_SESSION['colors'][$itemId]=$_POST['colors'];
$_SESSION['size'][$itemId]=$_POST['size'];
if ($_SERVER['REQUEST_METHOD'] == 'POST' and isset($_POST['qtyupdate'])) {
for ($i = 0; $i < count($_POST['qtyupdate']); $i++) {
$key = $_POST['arr_key_' . $i];
$_SESSION['qty'][$key] = $_POST['qtyupdate'][$i];
}
} else {
$qty = isset($_POST['qty']) ? $_POST['qty'] : 1;
if (!isset($_SESSION['cart'])) {
$_SESSION['cart'] = array();
$_SESSION['qty'][] = array();
}
if (in_array($itemId, $_SESSION['cart'])) {
$key = array_search($itemId, $_SESSION['cart']);
$_SESSION['qty'][$key] = $_SESSION['qty'][$key] + $qty;
} else {
array_push($_SESSION['cart'], $itemId);
$key = array_search($itemId, $_SESSION['cart']);
$_SESSION['qty'][$key] = $qty;
}
}
header('location:cart.php');
?>
In the product detail I have the select
one where the product size is added:
<select name="size">
<option value="XL">XL</option>
<option value="M">M</option>
</select>
And the color of the product:
<div id="cont-colors">
<input id="id-7" type="radio" name="colors" class="selector-colors" value="Verde">
<label for="id-7" title="Verde" id="colors" style="background:#008000;"></label>
<input id="id-8" type="radio" name="colors" class="selector-colors" value="Azulado">
<label for="id-8" title="Azulado" id="colors" style="background:#008080;"></label>
</div>
And sent:
<form method="post" action="updatecart.php?itemId=3">
With the code you currently handle there is no way to Duplicate an item in your session variables, the only way to add this scope in your application would be to rethink the structure of how you handle items, I'm not saying stop using sessions.
I would recommend that for each item you generate an array with an additional index
and you can associate the button to this $tempid and what the id would be like so you can create a structure:
this way if you add an Item, you can test if an item with its characteristics exists:
You will have to adapt this answer to your needs, if the solution does not provide an answer to your question, please do not throw negative, just ignore it.
As I see the code, the reason why the quantity and color are modified is because you
updatecart.php
are overwriting their value.As the same articles have the same id even if they have a different color or size, the last color and size assigned will be the general value of the article.
To solve this problem you must create more dimensions to your array one for Item, Color, and Size:
This will need to change the logic of the application specifically how you save the shopping cart.
You could do it like this:
cart.php
updatecart.php
I hope this clarifies my point.