254 lines
13 KiB
PHP
254 lines
13 KiB
PHP
<?php
|
|
// Prevent direct access to file
|
|
defined(security_key) or exit;
|
|
// Get all the categories from the database
|
|
$stmt = $pdo->query('SELECT * FROM categories');
|
|
$stmt->execute();
|
|
$categories = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
// Get the current category from the GET request, if none exists set the default selected category to: all
|
|
$category = isset($_GET['category']) ? $_GET['category'] : $main_category;
|
|
$category_sql = '';
|
|
if ($category != $main_category) {
|
|
$category_sql = 'JOIN products_categories pc ON pc.category_id = :category_id AND pc.product_id = p.id JOIN categories c ON c.id = pc.category_id';
|
|
}
|
|
// Get the sort from GET request, will occur if the user changes an item in the select box
|
|
$default_product_sort = default_product_sort;
|
|
$sort = isset($_GET['sort']) ? $_GET['sort'] : $default_product_sort;
|
|
// The amounts of products to show on each page
|
|
$num_products_on_each_page = 25;
|
|
// The current page, in the URL this will appear as index.php?page=products&p=1, index.php?page=products&p=2, etc...
|
|
$current_page = isset($_GET['p']) && is_numeric($_GET['p']) ? (int)$_GET['p'] : 1;
|
|
// Select products ordered by the date added
|
|
if ($sort == 'sort1') {
|
|
// sort1 = Alphabetical A-Z
|
|
$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 ' . $category_sql . ' WHERE p.status = 1 ORDER BY p.name ASC LIMIT :page,:num_products');
|
|
} elseif ($sort == 'sort2') {
|
|
// sort2 = Alphabetical Z-A
|
|
$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 ' . $category_sql . ' WHERE p.status = 1 ORDER BY p.name DESC LIMIT :page,:num_products');
|
|
} elseif ($sort == 'sort3') {
|
|
// sort3 = Newest
|
|
$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 ' . $category_sql . ' WHERE p.status = 1 ORDER BY p.date_added DESC LIMIT :page,:num_products');
|
|
} elseif ($sort == 'sort4') {
|
|
// sort4 = Oldest
|
|
$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 ' . $category_sql . ' WHERE p.status = 1 ORDER BY p.date_added ASC LIMIT :page,:num_products');
|
|
} elseif ($sort == 'sort5') {
|
|
// sort5 = Highest Price
|
|
$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 ' . $category_sql . ' WHERE p.status = 1 ORDER BY p.price DESC LIMIT :page,:num_products');
|
|
} elseif ($sort == 'sort6') {
|
|
// sort6 = Lowest Price
|
|
$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 ' . $category_sql . ' WHERE p.status = 1 ORDER BY p.price ASC LIMIT :page,:num_products');
|
|
} else {
|
|
// No sort was specified, get the products with no sorting
|
|
$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 ' . $category_sql . ' WHERE p.status = 1 LIMIT :page,:num_products');
|
|
}
|
|
// bindValue will allow us to use integer in the SQL statement, we need to use for LIMIT
|
|
if ($category != $main_category) {
|
|
$stmt->bindValue(':category_id', $category, PDO::PARAM_INT);
|
|
}
|
|
$stmt->bindValue(':page', ($current_page - 1) * $num_products_on_each_page, PDO::PARAM_INT);
|
|
$stmt->bindValue(':num_products', $num_products_on_each_page, PDO::PARAM_INT);
|
|
$stmt->execute();
|
|
// Fetch the products from the database and return the result as an Array
|
|
$products = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
// Get the total number of products
|
|
$stmt = $pdo->prepare('SELECT COUNT(*) FROM products p ' . $category_sql . ' WHERE p.status = 1');
|
|
if ($category != $main_category) {
|
|
$stmt->bindValue(':category_id', $category, PDO::PARAM_INT);
|
|
}
|
|
|
|
$stmt->execute();
|
|
$total_products = $stmt->fetchColumn();
|
|
|
|
//get all media
|
|
$stmt = $pdo->query('SELECT id, full_path FROM media');
|
|
$stmt->execute();
|
|
$media = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
?>
|
|
<?=template_header_top($products_text)?>
|
|
|
|
<div class="featured" style="background-image:url(<?= base_url.featured_store_image?>);background-position: center center;">
|
|
<?=template_menu()?>
|
|
<h2><?=$h1_content_top?></h2>
|
|
<h2></h2>
|
|
<p></p>
|
|
|
|
</div>
|
|
<?php
|
|
//SHOW OFFER
|
|
if(show_offer_product_page){
|
|
|
|
echo '
|
|
<div class="" style="text-align: center;">
|
|
<p class="p.paragraph.neutral-paragraph-text-1" style="font-family:\'gerb\';font-size: 15px;">'.show_offer_product_text.'</p>
|
|
</div>
|
|
';
|
|
}
|
|
?>
|
|
<div class="products content-wrapper">
|
|
<?php
|
|
echo '
|
|
<div style="margin-top: 30px;display: flex;align-items: center;align-content: center;flex-wrap: nowrap;flex-direction: column;">
|
|
<div class="product_category_nav">
|
|
';
|
|
foreach ($categories as $categorie){
|
|
if ($categorie['parent_id'] == '0' && $categorie['status'] == 1){
|
|
$weburl = url("index.php?page=products&category=".$categorie['id']."&sort=".$sort."");
|
|
echo '<a href="'.$weburl.'">'.$categorie['name'].'</a>';
|
|
}
|
|
}
|
|
echo '
|
|
</div>';
|
|
if (isset($_GET['category'])){
|
|
echo' <div class="product_category_nav">';
|
|
|
|
foreach ($categories as $categorie){
|
|
if ($categorie['parent_id'] == $_GET['category'] && $categorie['status'] == 1){
|
|
$weburl = url("index.php?page=products&category=".$categorie['id']."&sort=".$sort."");
|
|
echo '<a href="'.$weburl.'" style="color: #fff;background-color: #555555;font-size: 10px;">'.$categorie['name'].'</a>';
|
|
}
|
|
}
|
|
echo '</div>';
|
|
}
|
|
|
|
echo'
|
|
</div>';
|
|
?>
|
|
<div class="products-header">
|
|
<?=$total_products?> <?=$product_count_1?><?=$total_products!=1? $product_count_2:''?></p>
|
|
<form action="" method="get" class="products-form">
|
|
<input type="hidden" name="page" value="products">
|
|
<label class="category">
|
|
<?=$main_filter_category?>
|
|
<select name="category">
|
|
<option value="<?=$main_category?>"<?=($category == $main_category ? ' selected' : '')?>><?=$main_category?></option>
|
|
<?=populate_categories($categories, $category)?>
|
|
</select>
|
|
</label>
|
|
<label class="sortby">
|
|
<?=$main_filter_sort?>
|
|
<select name="sort">
|
|
<option value="sort1"<?=($sort == 'sort1' ? ' selected' : '')?>><?=$sort1?></option>
|
|
<option value="sort2"<?=($sort == 'sort2' ? ' selected' : '')?>><?=$sort2?></option>
|
|
<option value="sort3"<?=($sort == 'sort3' ? ' selected' : '')?>><?=$sort3?></option>
|
|
<option value="sort4"<?=($sort == 'sort4' ? ' selected' : '')?>><?=$sort4?></option>
|
|
<option value="sort5"<?=($sort == 'sort5' ? ' selected' : '')?>><?=$sort5?></option>
|
|
<option value="sort6"<?=($sort == 'sort6' ? ' selected' : '')?>><?=$sort6?></option>
|
|
</select>
|
|
</label>
|
|
</form>
|
|
</div>
|
|
|
|
<div class="products-wrapper">
|
|
<?php
|
|
foreach ($products as $product): ?>
|
|
<div class="product">
|
|
<?php
|
|
if (empty($product['product_config'])){
|
|
echo '<a href="'.url('index.php?page=product&id=' . ($product['url_slug'] ? ($product['url_slug'] ) : $product['id'])).'" id="'.$product['id'].'A" class="product">';
|
|
}
|
|
else{//ADD related optionID when configuration is found
|
|
$option_id = getPictureID($pdo,$product['id'],$product['product_config']);
|
|
|
|
echo '<a href="'.url('index.php?page=product&id=' . ($product['url_slug'] ? ($product['url_slug'].'/'.$option_id ) : $product['id'])).'" id="'.$product['id'].'A" class="product">';
|
|
}
|
|
?>
|
|
<?php if (!empty($product['img']) && file_exists($product['img'])): ?>
|
|
<?php if (empty($product['product_config'])): ?>
|
|
<img src="<?=base_url?><?=$product['img']?>" width="" height="250" alt="<?=$product['name']?>">
|
|
</a>
|
|
<!-- Show small image below main image in case of not configured -->
|
|
<div class="" style="display:flex;justify-content: center">
|
|
<div>
|
|
<img class="img_config" src="<?=base_url?><?=$product['img']?>"/>
|
|
</div>
|
|
</div>
|
|
|
|
<?php else: ?>
|
|
<img src="<?=base_url?><?=$product['img']?>" id="<?=$product['id']?>" width="" height="250" alt="<?=$product['name']?>">
|
|
</a>
|
|
<div class="" style="display:flex;justify-content: center">
|
|
<?php
|
|
$option_profile = json_decode($product['product_config']);
|
|
|
|
foreach ($option_profile as $option){
|
|
foreach ($media as $media_item){
|
|
if ($media_item['id'] == $option->IMG_small_id){
|
|
$IMG_small_id = $media_item['full_path'];
|
|
}
|
|
if ($media_item['id'] == $option->IMG_large_id){
|
|
$IMG_large_id = $media_item['full_path'];
|
|
}
|
|
}
|
|
$option_id = ($option->option_id != '') ? $option->option_id : '';
|
|
$option_price = $option->option_price ?? 0;
|
|
$option_price = currency_code.number_format($product['price']+$option_price,2);
|
|
|
|
echo'
|
|
<div>
|
|
<img class="img_config" src="'.url($IMG_small_id).'" id="'.$option->IMG_small_id.'" onclick="update(\''.$product['id'].'\',\''.url($IMG_large_id).'\',\''.url('index.php?page=product&id=' . ($product['url_slug'] ? $product['url_slug'].'/'.$option_id : $product['id'].'/'.$option_id )).'\',\''.$option_price.'\')" />
|
|
</div>
|
|
';
|
|
|
|
}
|
|
?>
|
|
</div>
|
|
<?php endif; ?>
|
|
<?php endif; ?>
|
|
<?php
|
|
//Stock status
|
|
$stock_status = ($product['quantity'] != 0) ? $product_on_stock : $out_of_stock;
|
|
$style = ($stock_status == $product_on_stock) ? 'style="background-color: green;"' : 'style="background-color:gray;font-weight: lighter;"';
|
|
?>
|
|
<span class="stock">
|
|
<p <?=$style?>> <?=$stock_status?> </p>
|
|
</span>
|
|
<?php
|
|
if (free_shipment_indicator){
|
|
freeShipment($product['price'],'span');
|
|
}
|
|
|
|
//ADD related optionID when configuration is found
|
|
if (empty($product['product_config'])){
|
|
$option_id = '';
|
|
}else {
|
|
$option_id = '/'.getPictureID($pdo,$product['id'],$product['product_config']);
|
|
}
|
|
?>
|
|
<a href="<?=url('index.php?page=product&id=' . ($product['url_slug'] ? $product['url_slug'].$option_id : $product['id']))?>" id="<?=$product['id']?>B" class="product">
|
|
<span class="name"><?=$product['name']?></span>
|
|
<span class="price" id="<?=$product['id']?>C">
|
|
<?=currency_code?><?=number_format($product['price'],2)?>
|
|
<?php if ($product['rrp'] > 0): ?>
|
|
<span class="rrp"><?=currency_code?><?=number_format($product['rrp'],2)?></span>
|
|
<?php endif; ?>
|
|
</span>
|
|
</a>
|
|
</div>
|
|
<?php endforeach; ?>
|
|
</div>
|
|
|
|
<div class="buttons">
|
|
<?php if ($current_page > 1): ?>
|
|
<a href="<?=url('index.php?page=products&p=' . ($current_page-1) . '&category=' . $category . '&sort=' . $sort)?>" class="btn">Prev</a>
|
|
<?php endif; ?>
|
|
<?php if ($total_products > ($current_page * $num_products_on_each_page) - $num_products_on_each_page + count($products)): ?>
|
|
<a href="<?=url('index.php?page=products&p=' . ($current_page+1) . '&category=' . $category . '&sort=' . $sort)?>" class="btn">Next</a>
|
|
<?php endif; ?>
|
|
</div>
|
|
|
|
</div>
|
|
<script>
|
|
function update(id_large, IMG_large, option_id, price){
|
|
let url_id_a = id_large + 'A';
|
|
let url_id_b = id_large + 'B';
|
|
let url_id_c = id_large + 'C';
|
|
|
|
//change picture
|
|
document.getElementById(id_large).src = IMG_large;
|
|
document.getElementById(url_id_a).href = option_id;
|
|
document.getElementById(url_id_b).href = option_id;
|
|
document.getElementById(url_id_c).innerHTML = price;
|
|
}
|
|
</script>
|
|
<?=template_footer()?>
|