Refactor code structure for improved readability and maintainability
This commit is contained in:
194
products.php
194
products.php
@@ -9,37 +9,66 @@ $num_products_on_each_page = 25;
|
||||
//GET Details from URL
|
||||
$GET_VALUES = urlGETdetails($_GET) ?? '';
|
||||
|
||||
//Get all the categories from the database
|
||||
$categories = ioAPIv2('/v2/categories/','',$clientsecret);
|
||||
$categories = json_decode($categories,true);
|
||||
//Get all the categories from cached data
|
||||
$categories = $GLOBALS['cached_categories'];
|
||||
|
||||
//IF CATEGORY IS RECEIVED ONLY GET RELATED PRODUCTS
|
||||
$url_input = '';
|
||||
if(isset($_GET['category']) && !isset($_POST['category'])){
|
||||
$url_input = 'category='.$_GET['category'];
|
||||
// Deduplicate categories by rowID (since categories repeat for each product)
|
||||
$unique_categories = [];
|
||||
foreach ($categories as $cat) {
|
||||
$unique_categories[$cat['rowID']] = $cat;
|
||||
}
|
||||
$categories = array_values($unique_categories);
|
||||
|
||||
//Get all products from cached data
|
||||
$products = $GLOBALS['cached_catalog'];
|
||||
|
||||
// Build product-to-categories mapping for filtering
|
||||
$product_categories_map = [];
|
||||
foreach ($GLOBALS['cached_categories'] as $cat) {
|
||||
if (isset($cat['product_id'])) {
|
||||
$product_id = $cat['product_id'];
|
||||
if (!isset($product_categories_map[$product_id])) {
|
||||
$product_categories_map[$product_id] = [];
|
||||
}
|
||||
// Only add categories that have status=1 and filter=1
|
||||
if (isset($cat['status']) && $cat['status'] == 1) {
|
||||
$product_categories_map[$product_id][] = $cat['rowID'];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($_POST['category'])){
|
||||
|
||||
$filter_input = '';
|
||||
// Add categories array to each product for JavaScript filtering
|
||||
foreach ($products as &$product) {
|
||||
$product_id = $product['rowID'];
|
||||
$product['category_ids'] = isset($product_categories_map[$product_id])
|
||||
? array_unique($product_categories_map[$product_id])
|
||||
: [];
|
||||
}
|
||||
unset($product);
|
||||
|
||||
foreach (array_keys($_POST['category']) as $cat_filter){
|
||||
$filter_input .= $cat_filter.',';
|
||||
}
|
||||
|
||||
if ($url_input != ''){
|
||||
$url_input = $url_input.','.substr($filter_input,0, -1);
|
||||
|
||||
} else {
|
||||
$url_input = 'category='.substr($filter_input,0, -1);
|
||||
}
|
||||
}
|
||||
|
||||
//GET CATALOG DATA
|
||||
$products = ioAPIv2('/v2/catalog/'.$url_input,'',$clientsecret);
|
||||
$products = json_decode($products,true);
|
||||
$total_products = count($products);
|
||||
|
||||
// Get URL category filter if present
|
||||
$url_category_filter = isset($_GET['category']) ? explode(',', $_GET['category']) : [];
|
||||
|
||||
// Expand parent categories to include all their children
|
||||
$expanded_filters = [];
|
||||
foreach ($url_category_filter as $cat_id) {
|
||||
$expanded_filters[] = $cat_id;
|
||||
// Find all children of this category
|
||||
foreach ($unique_categories as $cat) {
|
||||
if ($cat['parent_id'] == $cat_id && $cat['status'] == 1 && $cat['filter'] == 1) {
|
||||
$expanded_filters[] = $cat['rowID'];
|
||||
}
|
||||
}
|
||||
}
|
||||
$url_category_filter = array_unique($expanded_filters);
|
||||
|
||||
// Debug: log what category was received
|
||||
if (debug && !empty($url_category_filter)) {
|
||||
debuglog('URL category filter (expanded): ' . implode(',', $url_category_filter));
|
||||
}
|
||||
|
||||
//SORT BY NAME
|
||||
usort($products, function($a, $b) {
|
||||
return strcmp($a['productname'], $b['productname']);
|
||||
@@ -50,7 +79,7 @@ $view = template_header($products_text,'');
|
||||
|
||||
$view .= '
|
||||
<!-- Hero Section -->
|
||||
<section class="hero" style="background-image:url('.base_url.featured_store_image.');">
|
||||
<section class="hero hero-products" style="background-image:url('.base_url.featured_store_image.');">
|
||||
<div class="hero-content">
|
||||
<h1>'.$h1_content_top.'</h1>
|
||||
</div>
|
||||
@@ -64,7 +93,7 @@ $view .= '
|
||||
<div class="filters-products-container">
|
||||
|
||||
<div class="filters" id="filters">
|
||||
<form action="" method="post">';
|
||||
<div class="filter-container">';
|
||||
|
||||
if (count($categories) > 0){
|
||||
//BUILD UP FILTERS BASED ON CATEGORY ASSIGNMENTS
|
||||
@@ -77,10 +106,11 @@ $view .= '
|
||||
//Iterate through categories for subfilters
|
||||
foreach ($categories as $subfilter){
|
||||
if ($filters['rowID'] == $subfilter['parent_id'] && $subfilter['status'] == 1 && $subfilter['filter'] == 1){
|
||||
$checked = in_array($subfilter['rowID'], $url_category_filter) ? 'checked' : '';
|
||||
$view .= '
|
||||
<div class="filter-option">
|
||||
<input type="checkbox" id="'.$subfilter['name'].'" name="category['.$subfilter['rowID'].']">
|
||||
<label for="'.$subfilter['name'].'">'.(${$subfilter['name']} ?? $subfilter['name']).'</label>
|
||||
<input type="checkbox" class="filter-checkbox" id="cat_'.$subfilter['rowID'].'" data-category="'.$subfilter['rowID'].'" '.$checked.'>
|
||||
<label for="cat_'.$subfilter['rowID'].'">'.(${$subfilter['name']} ?? $subfilter['name']).'</label>
|
||||
</div>';
|
||||
}
|
||||
}
|
||||
@@ -90,10 +120,10 @@ $view .= '
|
||||
}
|
||||
$view .= '
|
||||
<div class="filter-option">
|
||||
<input type="submit" value="'.($btn_filter ?? 'Filter').'" class="btn" >
|
||||
<button type="button" id="clearFilters" class="btn" style="display:none;">'.($btn_clear_filters ?? 'Clear Filters').'</button>
|
||||
</div>';
|
||||
}
|
||||
$view .= '</form>
|
||||
$view .= '</div>
|
||||
</div>
|
||||
<div class="products">
|
||||
<div class="product-grid">';
|
||||
@@ -105,7 +135,7 @@ $view .= '</form>
|
||||
|
||||
|
||||
$view .= '
|
||||
<div class="product-card">
|
||||
<div class="product-card" data-categories="'.implode(',', $product['category_ids']).'" data-product-id="'.$product['rowID'].'">
|
||||
<a href="'.url('index.php?page=product&rowID=' . ($product['url_slug'] ? ($product['url_slug'] ) : $product['rowID'])).(!empty($product['main_option_for_display']) ? '/'.$product['main_option_for_display']:'').'" id="'.$product['rowID'].'A" class="product">
|
||||
<img src="'.img_url.$product['full_path'].'" id="'.$product['rowID'].'" width="" height="250" alt="'.(${$product['productname']} ?? $product['productname']).'">
|
||||
</a>';
|
||||
@@ -223,7 +253,7 @@ $view .= '
|
||||
}
|
||||
</script>
|
||||
<script>
|
||||
// Simple filter toggle for mobile
|
||||
// Filter toggle for mobile
|
||||
const filterToggle = document.getElementById(\'filterToggle\');
|
||||
const filters = document.getElementById(\'filters\');
|
||||
|
||||
@@ -231,6 +261,104 @@ $view .= '
|
||||
filters.classList.toggle(\'show\');
|
||||
filterToggle.textContent = filters.classList.contains(\'show\') ? \'Hide Filters\' : \'Show Filters\';
|
||||
});
|
||||
|
||||
// Client-side product filtering
|
||||
const filterCheckboxes = document.querySelectorAll(\'.filter-checkbox\');
|
||||
const productCards = document.querySelectorAll(\'.product-card\');
|
||||
const clearFiltersBtn = document.getElementById(\'clearFilters\');
|
||||
let activeFilters = new Set();
|
||||
|
||||
console.log(\'Filter system initialized\');
|
||||
console.log(\'Total checkboxes:\', filterCheckboxes.length);
|
||||
console.log(\'Total product cards:\', productCards.length);
|
||||
|
||||
// Debug: Log all product categories
|
||||
productCards.forEach((card, idx) => {
|
||||
if (idx < 3) {
|
||||
console.log(\'Product\', idx, \'categories:\', card.getAttribute(\'data-categories\'));
|
||||
}
|
||||
});
|
||||
|
||||
// Debug: Log all filter categories
|
||||
const filterCategories = [];
|
||||
filterCheckboxes.forEach(checkbox => {
|
||||
filterCategories.push(checkbox.getAttribute(\'data-category\'));
|
||||
});
|
||||
console.log(\'Filter categories:\', filterCategories);
|
||||
|
||||
// Initialize filters from pre-checked checkboxes (from URL params)
|
||||
filterCheckboxes.forEach(checkbox => {
|
||||
if (checkbox.checked) {
|
||||
const category = checkbox.getAttribute(\'data-category\');
|
||||
activeFilters.add(category);
|
||||
}
|
||||
});
|
||||
|
||||
// Run initial filter if there are pre-selected categories
|
||||
if (activeFilters.size > 0) {
|
||||
filterProducts();
|
||||
}
|
||||
|
||||
// Filter products based on selected categories
|
||||
function filterProducts() {
|
||||
let visibleCount = 0;
|
||||
|
||||
console.log(\'Active filters:\', Array.from(activeFilters));
|
||||
|
||||
productCards.forEach(card => {
|
||||
const productCategoriesStr = card.getAttribute(\'data-categories\');
|
||||
const productCategories = productCategoriesStr ? productCategoriesStr.split(\',\') : [];
|
||||
|
||||
// If no filters active, show all products
|
||||
if (activeFilters.size === 0) {
|
||||
card.style.display = \'\';
|
||||
visibleCount++;
|
||||
} else {
|
||||
// Show product if ANY of its categories match the active filters
|
||||
const hasMatch = productCategories.some(cat => activeFilters.has(cat));
|
||||
if (hasMatch) {
|
||||
card.style.display = \'\';
|
||||
visibleCount++;
|
||||
} else {
|
||||
card.style.display = \'none\';
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Show/hide clear button
|
||||
if (clearFiltersBtn) {
|
||||
clearFiltersBtn.style.display = activeFilters.size > 0 ? \'inline-block\' : \'none\';
|
||||
}
|
||||
|
||||
console.log(\'Showing \' + visibleCount + \' of \' + productCards.length + \' products\');
|
||||
}
|
||||
|
||||
// Add event listeners to checkboxes
|
||||
filterCheckboxes.forEach(checkbox => {
|
||||
checkbox.addEventListener(\'change\', function() {
|
||||
const category = this.getAttribute(\'data-category\');
|
||||
console.log(\'Checkbox changed:\', category, \'checked:\', this.checked);
|
||||
|
||||
if (this.checked) {
|
||||
activeFilters.add(category);
|
||||
} else {
|
||||
activeFilters.delete(category);
|
||||
}
|
||||
|
||||
filterProducts();
|
||||
});
|
||||
});
|
||||
|
||||
// Clear all filters
|
||||
if (clearFiltersBtn) {
|
||||
clearFiltersBtn.addEventListener(\'click\', () => {
|
||||
filterCheckboxes.forEach(checkbox => {
|
||||
checkbox.checked = false;
|
||||
});
|
||||
activeFilters.clear();
|
||||
filterProducts();
|
||||
});
|
||||
}
|
||||
</script>
|
||||
';
|
||||
|
||||
|
||||
Reference in New Issue
Block a user