Ok Harry…I’ve messing around and trying to learn. So I have 2 pages. 1 called shop.html and the other called cart.html
In the shop.html page I have this code below which is an addToCart snippet and an addToCart_Button chunk.
<div class="container">
<!-- Add to Cart Snippet
============================================= -->
[[!addToCart]]
<!-- Add to Cart Button Chunk
============================================= -->
[[$addToCart_Button]]
</div>
This is the addToCart snippet
<?php
// Error checker
error_reporting(E_ALL);
ini_set("display_errors", 1);
// Use secure and HttpOnly flags for session cookies
session_set_cookie_params([
'secure' => true,
'httponly' => true,
]);
// Start or resume the session
session_start();
// Regenerate the session ID to prevent session fixation
session_regenerate_id(true);
// Check if the cart array is not initialized in the session, initialize it
if (!isset($_SESSION["cart"])) {
$_SESSION["cart"] = array();
}
// Check if the request is to add an item to the cart
if (isset($_POST["add_to_cart"])) {
// Validate and sanitize input
$product_id = filter_input(INPUT_POST, "product_id", FILTER_SANITIZE_NUMBER_INT);
$quantity = filter_input(INPUT_POST, "quantity", FILTER_VALIDATE_INT, array("options" => array("min_range" => 1)));
if ($product_id !== false && $quantity !== false) {
// Check if the product is already in the cart
if (isset($_SESSION["cart"][$product_id])) {
// If it is, increment the quantity by the selected multiplier
$_SESSION["cart"][$product_id]["quantity"] += $quantity;
} else {
// If it's not, add it to the cart with the selected quantity
$_SESSION["cart"][$product_id] = array(
"quantity" => $quantity,
"product_name" => "ITEM SOLD" . $product_id,
"product_price" => 250.00,
);
}
} else {
// Handle invalid input (e.g., display an error message)
echo "Invalid input. Please check your input values.";
}
}
And this is the addToCart_Button chunk
<form class="cart" method="post">
<div class="quantity">
<input type="button" value="-" class="minus">
<input type="number" step="1" min="1" name="quantity" id="quantity" value="1" title="Qty" class="qty">
<input type="button" value="+" class="plus">
</div>
<button type="submit" name="add_to_cart" class="add-to-cart button">Add to Cart</button>
</form>
Then on the cart.html page I have a snippet called viewCart and a chunk called viewCart_Table
This is the viewCart snippet
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// Start or resume the session
session_start();
// Check if the cart array is not initialized in the session, initialize it
if (!isset($_SESSION["cart"])) {
$_SESSION["cart"] = array();
}
// Check if the "Clear Cart" button is clicked
if (isset($_POST["clear_cart"])) {
// Clear the cart
$_SESSION["cart"] = array();
}
?>
<?php foreach ($_SESSION["cart"] as $product): ?>
<?php
$quantity = $product["quantity"];
$discount = ($quantity == 1) ? 0 : ($quantity == 2 ? 0.10 : 0.20);
$fullPrice = $product["quantity"] * $product["product_price"];
$discountAmount = $fullPrice * $discount;
$discountedPrice = $fullPrice - $discountAmount;
?>
<?php endforeach; ?>
This is the viewCart_Table
<table class="table cart mb-5">
<thead>
<tr>
<th class="cart-product-name">Name</th>
<th class="cart-product-price">Price</th>
<th class="cart-product-quantity">Quantity</th>
<th class="cart-product-fullprice">Full Price</th>
<th class="cart-product-discount">Discount</th>
<th class="cart-product-subtotal">Discountws Price</th>
</tr>
</thead>
<tbody>
<tr class="cart_item">
<td class="cart-product-name">Product Name</td>
<td class="cart-product-price"><span class="amount">Price Here</span></td>
<td class="cart-product-quantity">
<div class="quantity">
<input type="button" value="-" class="minus">
<input type="text" name="quantity" value="Quantity Vakue Here" class="qty">
<input type="button" value="+" class="plus">
</div>
</td>
<td class="cart-product-fullprice"><span class="amount">Full Price Here</span></td>
<td class="cart-product-discount"><span class="amount">Discount Percentage Here</span></td>
<td class="cart-product-subtotal"><span class="amount color lead fw-medium">Discounted Price Here</span></td>
</tr>
</tbody>
</table>
My question is how do I take the data from the shop.html page that I’ve saved and add it to the table on the cart.html page? I know you mentioned placeholders but I’m not 100% sure how you do that.