Skip to content

Commit

Permalink
Variation in cart and checkout
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobias committed Jan 29, 2024
1 parent 6937031 commit efdfa6d
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 57 deletions.
24 changes: 22 additions & 2 deletions app/backend/auth/add-order.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,28 @@
$subtotal = $product->price * $quantity;
$total += $subtotal;

$body .= "<tr><td>" . $product->name . "</td><td>" . $product->price . "</td><td>" . $quantity . "</td><td>" . $subtotal . "</td></tr>";
$altBody .= "Name: " . $product->name . "\nPrice: " . $product->price . "\nQuantity: " . $quantity . "\nSubtotal: " . $subtotal . "\n-------------------\n";
$body .= "<tr><td>" . $product->name . "</td><td>" . Product::getCurrentPrice($product->product_id) . "</td><td>" . $quantity . "</td><td>" . $subtotal . "</td></tr>";
$altBody .= "Name: " . $product->name . "\nPrice: " . Product::getCurrentPrice($product->product_id) . "\nQuantity: " . $quantity . "\nSubtotal: " . $subtotal . "\n-------------------\n";
}
}

foreach ($products as $product) {
$product = explode(",", $product);
$product_id = $product[1];
$quantity = $product[0];

$product = Product::getProductById($product_id);

if ($product !== null) {
$variation_id = $product->variation_id;
// Decrease the stock of the product by the quantity ordered
Product::decreaseStock($variation_id, $quantity);

$subtotal = $product->price * $quantity;
$total += $subtotal;

$body .= "<tr><td>" . $product->name . "</td><td>" . Product::getCurrentPrice($product->product_id) . "</td><td>" . $quantity . "</td><td>" . $subtotal . "</td></tr>";
$altBody .= "Name: " . $product->name . "\nPrice: " . Product::getCurrentPrice($product->product_id) . "\nQuantity: " . $quantity . "\nSubtotal: " . $subtotal . "\n-------------------\n";
}
}

Expand Down
29 changes: 29 additions & 0 deletions app/backend/classes/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,5 +227,34 @@ public static function addVariation($product_id, $variation_name)
}
}

public static function getVariationByVariationId($variation_id)
{
$variation = Database::getInstance()->get('product_variations', array('variation_id', '=', $variation_id))->first();
if ($variation !== null) {
return $variation; // Return the variation, not the product
}
}

public static function decreaseStock($variation_id, $quantity)
{
$db = Database::getInstance();

$variation = $db->get('product_variations', array('variation_id', '=', $variation_id))->first();

if ($variation !== null) {
$stock = $variation->stock;

$stock -= $quantity;

if ($stock < 0) {
$stock = 0;
}

if (!$db->update('product_variations', 'variation_id', $variation_id, array('stock' => $stock))) {
throw new Exception('There was a problem updating the stock.');
}
}
}

// This file creates the products and gets all the products in a channel and the products by id.
}
142 changes: 89 additions & 53 deletions app/frontend/pages/cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,73 @@
$_SESSION['cart'] = [];
}

// Add product to cart, increase or decrease quantity
// Add product to cart
if (isset($_POST['add_to_cart'])) {
$product_id = $_POST['product_id'];
$quantity = isset($_POST['quantity']) ? $_POST['quantity'] : 1; // Default to 1 if 'quantity' is not set

// Check if the product already exists in the cart
$product_exists_in_cart = false;
foreach ($_SESSION['cart'] as $key => &$item) {
if ($item['product_id'] == $product_id) {
// If the product exists, increase or decrease its quantity
$item['quantity'] += $quantity;
$product_exists_in_cart = true;
// If quantity is 0 or less, remove the product from the cart
if ($item['quantity'] <= 0) {
unset($_SESSION['cart'][$key]);
}
break;
$variation_id = $_POST['variation_id'];

// Use a combination of the product ID and the variation ID as the key
$key = $product_id . '-' . $variation_id;

$_SESSION['cart'][$key] = array(
'product_id' => $product_id,
'variation_id' => $variation_id,
'quantity' => 1
);

// Redirect back to the cart page
header('Location: cart.php');
exit;
}

// Remove product from cart
if (isset($_POST['remove_from_cart'])) {
$product_id = $_POST['product_id'];
$variation_id = $_POST['variation_id'];

// Use the same key to remove the item from the cart
$key = $product_id . '-' . $variation_id;

if (isset($_SESSION['cart'][$key])) {
unset($_SESSION['cart'][$key]);
}

// Redirect back to the cart page
header('Location: cart.php');
exit;
}
// Increment quantity of product in cart
if (isset($_POST['increment_quantity'])) {
$product_id = $_POST['product_id'];
$variation_id = $_POST['variation_id'];

// Use the same key to access the item in the cart
$key = $product_id . '-' . $variation_id;

if (isset($_SESSION['cart'][$key])) {
$current_stock = Product::getStockByVariationId($variation_id);
if ($_SESSION['cart'][$key]['quantity'] < $current_stock) {
$_SESSION['cart'][$key]['quantity'] += 1;
}
}

// If the product doesn't exist in the cart, add it
if (!$product_exists_in_cart && $quantity > 0) {
$_SESSION['cart'][] = ['product_id' => $product_id, 'quantity' => $quantity];
// Redirect back to the cart page
header('Location: cart.php');
exit;
}

// Decrement quantity of product in cart
if (isset($_POST['decrement_quantity'])) {
$product_id = $_POST['product_id'];
$variation_id = $_POST['variation_id'];

// Use a combination of the product ID and the variation ID as the key
$key = $product_id . '-' . $variation_id;

if (isset($_SESSION['cart'][$key]) && $_SESSION['cart'][$key]['quantity'] > 1) {
$_SESSION['cart'][$key]['quantity'] -= 1;
} else if (isset($_SESSION['cart'][$key]) && $_SESSION['cart'][$key]['quantity'] == 1) {
unset($_SESSION['cart'][$key]);
}

// Redirect back to the cart page
Expand All @@ -44,27 +88,17 @@
$_SESSION['cart'] = [];
}

// Remove product from cart
if (isset($_POST['remove_from_cart'])) {
if (isset($_POST['remove_from_cart'])) {
$product_id = $_POST['product_id'];
foreach ($_SESSION['cart'] as $key => $item) {
if ($item['product_id'] == $product_id) {
unset($_SESSION['cart'][$key]);
break;
}
}
}
}

// Display the product in the cart
$total_price = 0;
if (!empty($_SESSION['cart'])) {
foreach ($_SESSION['cart'] as $item) {
foreach ($_SESSION['cart'] as $key => $item) {
$product_id = $item['product_id'];
$variation_id = $item['variation_id'];
$quantity = $item['quantity'];

$product = Product::getProductById($product_id);
$variation = Product::getVariationByVariationId($variation_id);

// Add product price to total price
$total_price += Product::getCurrentPrice($product->product_id) * $quantity;

Expand All @@ -73,27 +107,29 @@
echo "<p>" . $product->description . "</p>";
echo "<p>Price: $" . Product::getCurrentPrice($product->product_id) . "</p>";
echo "<p>Quantity: " . $quantity . "</p>";

// Button to remove product from cart
echo "<form action='cart.php' method='post'>";
echo "<input type='hidden' name='product_id' value='$product_id'>";
echo "<input type='submit' name='remove_from_cart' value='Remove from Cart'>";
echo "</form>";


// Add more of the same product
echo "<form action='cart.php' method='post'>";
echo "<input type='hidden' name='product_id' value='$product_id'>";
echo "<input type='hidden' name='quantity' value='1'>";
echo "<input type='submit' name='add_to_cart' value='+1'>";
echo "</form>";

// Remove one of the same product
echo "<form action='cart.php' method='post'>";
echo "<input type='hidden' name='product_id' value='$product_id'>";
echo "<input type='hidden' name='quantity' value='-1'>";
echo "<input type='submit' name='add_to_cart' value='-1'>";
echo "</form>";
echo "<p>Variation: " . $variation->name . "</p>";
?>
<!-- Remove from Cart -->
<form method="post" action="cart.php">
<input type="hidden" name="product_id" value="<?php echo $product_id; ?>">
<input type="hidden" name="variation_id" value="<?php echo $variation_id; ?>">
<input type="submit" name="remove_from_cart" value="Remove from Cart">
</form>

<!-- Add More of the Same Product -->
<form method="post" action="cart.php">
<input type="hidden" name="product_id" value="<?php echo $product_id; ?>">
<input type="hidden" name="variation_id" value="<?php echo $variation_id; ?>">
<input type="submit" name="increment_quantity" value="+1">
</form>

<!-- Remove One of the Same Product -->
<form method="post" action="cart.php">
<input type="hidden" name="product_id" value="<?php echo $product_id; ?>">
<input type="hidden" name="variation_id" value="<?php echo $variation_id; ?>">
<input type="submit" name="decrement_quantity" value="-1">
</form>
<?php
}

$tax_cut = $total_price * 0.2;
Expand Down
3 changes: 3 additions & 0 deletions app/frontend/pages/checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@
foreach ($products as $item) {
$product_id = $item['product_id'];
$quantity = $item['quantity'];
$variation_id = $item['variation_id']; // Define $variation_id here
$product = Product::getProductById($product_id);
$variation = Product::getVariationByVariationId($variation_id); // Define $variation here
echo "<h3>" . $product->name . "</h3>";
echo "<p>" . $product->description . "</p>";
echo "<p>Price: $" . Product::getCurrentPrice($product->product_id) . "</p>";
echo "<p>Quantity: " . $quantity . "</p>";
echo "<p> Variation: " . $variation->name . "</p>"; // Now $variation is defined
$total_price += Product::getCurrentPrice($product->product_id) * $quantity;
}
echo "<p>Total price: $$total_price</p>";
Expand Down
3 changes: 2 additions & 1 deletion app/frontend/pages/order-details.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@

// Fetch the product details from the database
$product = Product::getProductById($id);
$variation = Product::getVariationByVariationId($variation_id);

echo "Product Name: " . $product->name . "<br>";
echo "Product Description: " . $product->description . "<br>";
echo "Product Quantity: " . $quantity . "<br>";
echo "Product Variation: " . "<br>";
echo "Product Variation: " . $variation->name . "<br>";
echo "Product Price: " . "$" . $price_for_the_product . "<br>";
// Fetch the images for the product
$images = Product::getImagesByProductId($id);
Expand Down
10 changes: 9 additions & 1 deletion app/frontend/pages/product.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,31 @@
<p id="stock"></p>
<script>
document.getElementById('productVariations').addEventListener('change', function() {
var addToCartButton = document.querySelector('input[name="add_to_cart"]');
if (this.value === "") {
document.getElementById('stock').textContent = "";
return;
}

document.getElementById('selectedVariation').value = this.value;

fetch('get-stock.php?variation_id=' + this.value)
.then(response => response.text())
.then(data => {
var stockText = "Stock: " + data;
document.getElementById('stock').textContent = stockText;

// If the stock is 0, disable the "Add to Cart" button. Otherwise, enable it.
document.querySelector('input[name="add_to_cart"]').disabled = (data == 0);
})
.catch(error => console.error('Error:', error));
});
</script>

<form action="cart.php" method="post">
<form method="post" action="cart.php">
<input type="hidden" name="product_id" value="<?php echo $product->product_id ?>">
<input type="hidden" id="selectedVariation" name="variation_id">
<input type="hidden" name="quantity" value="1">
<input type="submit" name="add_to_cart" value="Add to Cart">
</form>
</div>
Expand Down

0 comments on commit efdfa6d

Please sign in to comment.