CMXX - API based checkout
This commit is contained in:
180
cart.php
180
cart.php
@@ -1,5 +1,5 @@
|
||||
<?php
|
||||
// Prevent direct access to file
|
||||
// Prevent direct access to file
|
||||
defined(security_key) or exit;
|
||||
// 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']])) {
|
||||
@@ -90,42 +90,23 @@ if (isset($_POST['samples'])){
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Check the session variable for products in cart
|
||||
$products_in_cart = isset($_SESSION['cart']) ? $_SESSION['cart'] : [];
|
||||
$subtotal = 0.00;
|
||||
// If there are products in cart
|
||||
if ($products_in_cart) {
|
||||
// There are products in the cart so we need to select those products from the database
|
||||
// Products in cart array to question mark string array, we need the SQL statement to include: IN (?,?,?,...etc)
|
||||
$array_to_question_marks = implode(',', array_fill(0, count($products_in_cart), '?'));
|
||||
// Prepare SQL statement
|
||||
// $stmt = $pdo->prepare('SELECT p.id, pc.category_id, p.* FROM products p LEFT JOIN products_categories pc ON p.id = pc.product_id LEFT JOIN categories c ON c.id = pc.category_id WHERE p.id IN (' . $array_to_question_marks . ') GROUP BY p.id');
|
||||
$stmt = $pdo->prepare('SELECT p.*, (SELECT m.full_path FROM products_media pm JOIN media m ON m.id = pm.media_id WHERE pm.product_id = p.id ORDER BY pm.position ASC LIMIT 1) AS img FROM products p WHERE p.id IN (' . $array_to_question_marks . ')');
|
||||
// Leverage the array_column function to retrieve only the id's of the products
|
||||
$stmt->execute(array_column($products_in_cart, 'id'));
|
||||
// Fetch the products from the database and return the result as an Array
|
||||
$products = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
// Iterate the products in cart and add the meta data (product name, desc, etc)
|
||||
foreach ($products_in_cart as &$cart_product) {
|
||||
foreach ($products as $product) {
|
||||
if ($cart_product['id'] == $product['id']) {
|
||||
$cart_product['meta'] = $product;
|
||||
// Calculate the subtotal
|
||||
$subtotal += (float)$cart_product['options_price'] * (int)$cart_product['quantity'];
|
||||
}
|
||||
}
|
||||
}
|
||||
foreach ($products_in_cart as $num => $product) {
|
||||
// Calculate the subtotal
|
||||
$subtotal += (float)$product['options_price'] * (int)$product['quantity'];
|
||||
}
|
||||
?>
|
||||
<?=template_header('Shopping Cart')?>
|
||||
|
||||
template_header('Shopping Cart');
|
||||
|
||||
$view = '
|
||||
<div class="cart content-wrapper">
|
||||
|
||||
<h1><?=$h1_cart_name?></h1>
|
||||
<h1>'.$h1_cart_name.'</h1>
|
||||
<h2 style="text-align: center;margin-top: -35px;">
|
||||
<a href="<?=url(link_to_collection)?>" style="text-decoration: none;color: #555555;padding: 10px 10px;font-size: 10px;">
|
||||
<?=$navigation_back_to_store?>
|
||||
<a href="'.url(link_to_collection).'" style="text-decoration: none;color: #555555;padding: 10px 10px;font-size: 10px;">
|
||||
'.$navigation_back_to_store.'
|
||||
</a>
|
||||
</h2>
|
||||
|
||||
@@ -133,88 +114,105 @@ if ($products_in_cart) {
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<td colspan="2"><?=$tr_product?></td>
|
||||
<td colspan="2">'.$tr_product.'</td>
|
||||
<td class="rhide"></td>
|
||||
<td class="rhide"><?=$tr_price?></td>
|
||||
<td><?=$tr_quantity?></td>
|
||||
<td><?=$tr_total?></td>
|
||||
<td class="rhide">'.$tr_price.'</td>
|
||||
<td>'.$tr_quantity.'</td>
|
||||
<td>'.$tr_total.'</td>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<?php if (empty($products_in_cart)): ?>
|
||||
<tbody>';
|
||||
if (empty($products_in_cart)){
|
||||
$view .= '
|
||||
<tr>
|
||||
<td colspan="6" style="text-align:center;"><?=$cart_message_empty?></td>
|
||||
</tr>
|
||||
<?php else: ?>
|
||||
<?php foreach ($products_in_cart as $num => $product): ?>
|
||||
<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">
|
||||
<?php if (!empty($product['meta']['img']) && file_exists($product['meta']['img'])): ?>
|
||||
<a href="<?=url('index.php?page=product&id=' . $product['id'])?>">
|
||||
<img src="<?=base_url?><?=$product['meta']['img']?>" width="50" height="50" alt="<?=$product['meta']['name']?>">
|
||||
</a>
|
||||
<?php endif; ?>
|
||||
</td>
|
||||
<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']?></a>
|
||||
<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>
|
||||
<a href="'.url('index.php?page=cart&remove=' . $num).'" class="remove">Remove</a>
|
||||
</td>
|
||||
<td class="options rhide">
|
||||
<?=htmlspecialchars(str_replace(',', ', ', $product['options']), ENT_QUOTES)?>
|
||||
<input type="hidden" name="options" value="<?=htmlspecialchars($product['options'], ENT_QUOTES)?>">
|
||||
'.htmlspecialchars(substr($prod_options, 0,-2), ENT_QUOTES).'
|
||||
<input type="hidden" name="options" value="['.implode(',',$product['options']).']">
|
||||
</td>
|
||||
<td class="price rhide"><?=currency_code?><?=number_format($product['options_price'],2)?></td>
|
||||
<?php if ($product['options'] == $h2_cart_sample_product && !empty(category_id_checkout_samples)) : ?>
|
||||
<td class="quantity">
|
||||
<input type="number" class="ajax-update" name="quantity-<?=$num?>" value="1" min="1" max="1" placeholder="Quantity" readonly>
|
||||
</td>
|
||||
<?php else: ?>
|
||||
<td class="quantity">
|
||||
<input type="number" class="ajax-update" name="quantity-<?=$num?>" value="<?=$product['quantity']?>" min="1" <?php if ($product['meta']['quantity'] != -1): ?>max="<?=$product['meta']['quantity']?>"<?php endif; ?> placeholder="Quantity" required>
|
||||
</td>
|
||||
<?php endif; ?>
|
||||
<td class="price product-total"><?=currency_code?><?=number_format($product['options_price'] * $product['quantity'],2)?></td>
|
||||
</tr>
|
||||
<?php endforeach; ?>
|
||||
<?php endif; ?>
|
||||
</tbody>
|
||||
<td class="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="quantity">
|
||||
<input type="number" class="ajax-update" name="quantity-'.$num.'" value="1" min="1" max="1" placeholder="Quantity" readonly>
|
||||
</td>';
|
||||
} else {
|
||||
$view .= '
|
||||
<td class="quantity">
|
||||
<input type="number" class="ajax-update" name="quantity-'.$num.'" value="'.$product['quantity'].'" min="1" placeholder="Quantity" required>
|
||||
</td>';
|
||||
}
|
||||
$view .= ' <td class="price product-total">'.currency_code.''.number_format($product['options_price'] * $product['quantity'],2).'</td>
|
||||
</tr>';
|
||||
}
|
||||
}
|
||||
$view .= '</tbody>
|
||||
</table>
|
||||
</form>
|
||||
<?php if (!empty($products_in_cart) && !empty(category_id_checkout_suggestions)): ?>
|
||||
<?=getAccessoiries($pdo,category_id_checkout_suggestions)?>
|
||||
<?php endif; ?>
|
||||
<?php if (!empty($products_in_cart) && !empty(category_id_checkout_samples)): ?>
|
||||
<?=getSamples($pdo,category_id_checkout_samples)?>
|
||||
<?php endif; ?>
|
||||
</form>';
|
||||
|
||||
//SUGGESTIONS
|
||||
if (!empty($products_in_cart) && !empty(category_id_checkout_suggestions)){
|
||||
$view .= getAccessoiries($pdo,category_id_checkout_suggestions);
|
||||
}
|
||||
// SAMPLES
|
||||
if (!empty($products_in_cart) && !empty(category_id_checkout_samples)){
|
||||
$view .= getSamples($pdo,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>
|
||||
<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="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;">
|
||||
<input type="submit" form ="cart-form" value="'.$btn_emptycart.'" name="emptycart" class="btn" title="Remove cart" style="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>
|
||||
|
||||
|
||||
<h4 style="text-align: right;margin-top: -35px;">
|
||||
<a href="<?=url(link_to_collection)?>" style="text-decoration: none;color: #555555;padding: 10px 10px;font-size: 10px;">
|
||||
<?=$navigation_back_to_store?>
|
||||
<a href="'.url(link_to_collection).'" style="text-decoration: none;color: #555555;padding: 10px 10px;font-size: 10px;">
|
||||
'.$navigation_back_to_store.'
|
||||
</a>
|
||||
</h4>
|
||||
</div>
|
||||
<script>
|
||||
const buttonRight = document.getElementById("slideRight");
|
||||
const buttonLeft = document.getElementById("slideLeft");
|
||||
';
|
||||
|
||||
buttonRight.onclick = function() {
|
||||
document.getElementById('add_samples_container').scrollLeft += 50;
|
||||
};
|
||||
buttonLeft.onclick = function() {
|
||||
document.getElementById('add_samples_container').scrollLeft -= 50;
|
||||
};
|
||||
</script>
|
||||
<?=template_footer()?>
|
||||
//OUTPUT
|
||||
echo $view;
|
||||
|
||||
template_footer();
|
||||
|
||||
?>
|
||||
Reference in New Issue
Block a user