Files
Commerce/cart.php
“VeLiTi” 580f835fff 2nd update
2025-05-26 15:07:22 +02:00

219 lines
9.2 KiB
PHP

<?php
// Prevent direct access to file
defined(security_key) or exit;
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
// Remove product from cart, check for the URL param "remove", this is the product id, make sure it's a number and check if it's in the cart
if (isset($_GET['remove']) && is_numeric($_GET['remove']) && isset($_SESSION['cart']) && isset($_SESSION['cart'][$_GET['remove']])) {
// Remove the product from the shopping cart
array_splice($_SESSION['cart'], $_GET['remove'], 1);
header('Location: ' . url('index.php?page=cart'));
exit;
}
// Empty the cart
if (isset($_POST['emptycart']) && isset($_SESSION['cart'])) {
// Remove all products from the shopping cart
unset($_SESSION['cart']);
header('Location: ' . url('index.php?page=cart'));
exit;
}
// Update product quantities in cart if the user clicks the "Update" button on the shopping cart page
if ((isset($_POST['update']) || isset($_POST['checkout'])) && isset($_SESSION['cart'])) {
// Iterate the post data and update quantities for every product in cart
foreach ($_POST as $k => $v) {
if (strpos($k, 'quantity') !== false && is_numeric($v)) {
$id = str_replace('quantity-', '', $k);
// abs() function will prevent minus quantity and (int) will ensure the value is an integer (number)
$quantity = abs((int)$v);
// Always do checks and validation
if (is_numeric($id) && isset($_SESSION['cart'][$id]) && $quantity > 0) {
// Update new quantity
$_SESSION['cart'][$id]['quantity'] = $quantity;
}
}
}
// Send the user to the place order page if they click the Place Order button, also the cart should not be empty
if (isset($_POST['checkout']) && !empty($_SESSION['cart'])) {
header('Location: ' . url('index.php?page=checkout'));
exit;
}
header('Location: ' . url('index.php?page=cart'));
exit;
}
// Check if accessoiries are added
if (isset($_POST['product'])) {
//VALIDATE THE INPUT FOR THE SHOPPING CART
$payload = json_encode($_POST['product'], JSON_UNESCAPED_UNICODE);
$product_to_cart = ioAPIv2('/v2/shopping_cart/',$payload,$clientsecret);
$product_to_cart = json_decode($product_to_cart,true);
// Check if the product exists (array is not empty)
if ($product_to_cart['quantity'] > 0) {
// Product exists in database, now we can create/update the session variable for the cart
if (!isset($_SESSION['cart'])) {
// Shopping cart session variable doesnt exist, create it
$_SESSION['cart'] = [];
}
$cart_product = &get_cart_product($product_to_cart['id'], $product_to_cart['options']);
if ($cart_product) {
// Product exists in cart, update the quanity
$cart_product['quantity'] += $quantity;
} else {
// Product is not in cart, add it
$_SESSION['cart'][] = $product_to_cart;
}
}
// Prevent form resubmission...
header('Location: ' . url('index.php?page=cart'));
exit;
}
// Check if samples are added
if (isset($_POST['samples'])){
$options = $h2_cart_sample_product;
$quantity = 1;
$cart_product = &get_cart_product($_POST['add_product_id'], $options);
if ($cart_product) {
// Do no nothing
} else {
//remove existing product from CART
foreach ($_SESSION['cart'] as $num => $product){
if ($product['options'] == $h2_cart_sample_product && !empty(category_id_checkout_samples)){
array_splice($_SESSION['cart'], $num, 1);
}
}
//ADD Product to the chart
$_SESSION['cart'][] = [
'id' => $_POST['add_product_id'],
'quantity' => $quantity,
'options' => $options,
'options_price' => $_POST['add_product_price'],
'options_weight' => $_POST['add_product_weight'],
'shipping_price' => 0.00
];
}
}
// Check the session variable for products in cart
$products_in_cart = isset($_SESSION['cart']) ? $_SESSION['cart'] : [];
$subtotal = 0.00;
foreach ($products_in_cart as $num => $product) {
// Calculate the subtotal
$subtotal += (float)$product['options_price'] * (int)$product['quantity'];
}
$view = template_header(($shopping_cart_header ?? 'Shopping Cart'),'');
$view .= '
<div class="cart content-wrapper">
<div class="cart-header">
<h1>'.$h1_cart_name.'</h1>
<h4>
<a href="'.url(link_to_collection).'">
'.$navigation_back_to_store.'
</a>
</h4>
</div>
<form id="cart-form" action="" method="post">
<table>
<thead>
<tr>
<td colspan="2">'.$tr_product.'</td>
<td class="rhide"></td>
<td class="rhide">'.$tr_price.'</td>
<td>'.$tr_quantity.'</td>
<td>'.$tr_total.'</td>
</tr>
</thead>
<tbody>';
if (empty($products_in_cart)){
$view .= '
<tr>
<td colspan="6" style="text-align:center;">'.$cart_message_empty.'</td>
</tr>';
} else {
foreach ($products_in_cart as $num => $product){
// Ensure product price is a numeric value
$product['options_price'] = isset($product['options_price']) && $product['options_price'] > 0 ? floatval($product['options_price']) : 0.00;
if (isset($product['options']) && $product['options'] !=''){
$prod_options = '';
foreach ($product['options'] as $prod_opt){
$prod_options .= (${$prod_opt} ?? $prod_opt).', ';
}
}
$view .= '
<tr>
<td class="img">';
if (!empty($product['meta']['img'])){
$view .= ' <a href="'.url('index.php?page=product&id=' . $product['id']).'">
<img src="'.img_url.$product['meta']['img'].'" width="50" height="50" alt="'.$product['meta']['name'].'">
</a>';
}
$view .= '</td>
<td>
<a href="'.url('index.php?page=product&id=' . $product['id']).'">'.(${$product['meta']['name']} ?? $product['meta']['name']).'</a>
<br>
<a href="'.url('index.php?page=cart&remove=' . $num).'" class="remove">Remove</a>
</td>
<td class="options rhide">
'.htmlspecialchars(substr($prod_options, 0,-2), ENT_QUOTES).'
<input type="hidden" name="options" value="['.implode(',',$product['options']).']">
</td>
<td class="cart_price rhide">'.currency_code.''.number_format($product['options_price'],2).'</td>';
if ($product['options'] == $h2_cart_sample_product && !empty(category_id_checkout_samples)){
$view .= '
<td class="cart_quantity">
<input type="number" class="ajax-update" name="quantity-'.$num.'" value="1" min="1" max="1" placeholder="Quantity" readonly>
</td>';
} else {
$view .= '
<td class="cart_quantity">
<input type="number" class="ajax-update" name="quantity-'.$num.'" value="'.$product['quantity'].'" min="1" placeholder="Quantity" required>
</td>';
}
$view .= ' <td class="cart_price product-total">'.currency_code.''.number_format($product['options_price'] * $product['quantity'],2).'</td>
</tr>';
}
}
$view .= '</tbody>
</table>
</form>';
//SUGGESTIONS
if (!empty($products_in_cart) && !empty(category_id_checkout_suggestions)){
$view .= getAccessoiries($clientsecret,category_id_checkout_suggestions);
}
/* SAMPLES
if (!empty($products_in_cart) && !empty(category_id_checkout_samples)){
$view .= getSamples($clientsecret,category_id_checkout_samples);
}*/
$view .= '
<div class="total">
<span class="text">'.$total_subtotal.'</span>
<span class="price">'.currency_code.''.number_format($subtotal,2).'</span>
<span class="note">'.$total_note.'</span>
</div>
<div class="buttons">
<input type="submit" form ="cart-form" value="'.$btn_emptycart.'" name="emptycart" class="btn" title="Remove cart" style="font-size:10px;background:none;">
<input type="submit" form ="cart-form" value="'.$btn_update.'" name="update" class="btn" title="Refresh cart">
<input type="submit" form ="cart-form" value="'.$btn_checkout.'" name="checkout" class="btn" style="background-color:green;">
</div>
</div>
';
$view .= template_footer();
//OUTPUT
echo $view;
?>