📘 Lesson · Lesson 43
Shopping Cart Logic
Shopping Cart Logic
About
A shopping cart stores selected products in the session so they stay while the user browses. Here is the core logic.
Add to Cart
session_start();
$id = $_GET["id"];
if (!isset($_SESSION["cart"][$id])) {
$_SESSION["cart"][$id] = 1; // quantity
} else {
$_SESSION["cart"][$id]++;
}
echo "Added to cart";
View Cart
foreach ($_SESSION["cart"] as $id => $qty) {
echo "Product $id x $qty<br>";
}
Remove Item
unset($_SESSION["cart"][$_GET["id"]]);
echo "Removed";
Summary
- Store cart items in
$_SESSION["cart"]as id => quantity. - Add increments quantity; unset removes an item.
परिचय
Shopping cart चुने products को session में रखता है ताकि user के browse करते समय वे बने रहें। यह रहा core logic।
Cart में Add करें
session_start();
$id = $_GET["id"];
if (!isset($_SESSION["cart"][$id])) {
$_SESSION["cart"][$id] = 1; // quantity
} else {
$_SESSION["cart"][$id]++;
}
echo "Added to cart";
Cart देखें
foreach ($_SESSION["cart"] as $id => $qty) {
echo "Product $id x $qty<br>";
}
Item हटाएं
unset($_SESSION["cart"][$_GET["id"]]);
echo "Removed";
सारांश
- Cart items को
$_SESSION["cart"]में id => quantity के रूप में रखें। - Add quantity बढ़ाता है; unset item हटाता है।