CMXX - Added categories filter for commerce

This commit is contained in:
“VeLiTi”
2025-02-04 11:46:14 +01:00
parent 59c23bf46c
commit 0172f8405a
5 changed files with 127 additions and 5 deletions

View File

@@ -10,12 +10,42 @@ defined($security_key) or exit;
//------------------------------------------
$catalog = []; // Main catalog array
//NEW ARRAY
$criterias = [];
$filter = '';
//Check for $_GET variables and build $_GET
if(isset($get_content) && $get_content!=''){
//GET VARIABLES FROM URL
$requests = explode("&", $get_content);
//Check for keys and values
foreach ($requests as $y){
$v = explode("=", $y);
//INCLUDE VARIABLES IN ARRAY
$criterias[$v[0]] = $v[1];
if ($v[0] == 'category'){
//------------------------------------------
//IF CATEGORY IS RECEIVED ONLY GET RELATED PRODUCTS
//------------------------------------------
$url_input = '';
//Get all the related products from the database
$cat_products = ioAPIv2('/v2/products_categories/list=filter&category_id='.$v[1],'',$clientsecret);
$cat_products = json_decode($cat_products,true);
foreach($cat_products as $cat_product_id){
$url_input .= $cat_product_id['product_id'].',';
}
$filter .= '&product_id='.substr($url_input,0,-1);
}
}
}
//------------------------------------------
//GET ACTIVE AND SALES RELATED PRODUCTS
//------------------------------------------
$filter = (isset($get_content) && $get_content !='') ? '&'.$get_content : '';
//GET PRODUCTS
$api_url = '/v2/products/salesflag=1&status=1'.$filter;
$products = ioApi($api_url,'',$clientsecret);

View File

@@ -48,6 +48,32 @@ if(isset($get_content) && $get_content!=''){
//build up search
$clause .= ' AND p.productcode like :'.$v[0];
}
elseif ($v[0] == 'product_id') {
//build up product_id = rowID
//check if multiple product_ids are provided
if (str_contains($v[1], ',')){
$inputs = explode(",",$v[1]);
$new_querystring = ''; //empty querystring
$x=0;
foreach($inputs as $input){
//create key
$new_key = $v[0].'_'.$x;
//inject new key/value to array
$criterias[$new_key] = $input;
$new_querystring .= ':'.$new_key.',';
$x++;
}
//remove obsolete last character from new_querystring
$new_querystring = substr($new_querystring,0, -1);
//add new_querystring to clause
$clause .= ' AND p.rowID IN ('.$new_querystring.')';
//remove original key/value from array
unset($criterias[$v[0]]);
}
else {
$clause .= ' AND p.rowID IN (:'.$v[0].')';
}
}
else {//create clause
$clause .= ' AND p.'.$v[0].' = :'.$v[0];
}

View File

@@ -43,11 +43,67 @@ if(isset($get_content) && $get_content!=''){
if ($v[0] == 'page' || $v[0] =='p' || $v[0] =='totals' || $v[0] =='list' || $v[0] =='history'|| $v[0] =='success_msg'){
//do nothing
}
elseif ($v[0] == 'product_id') {
//check if multiple product_ids are provided
if (str_contains($v[1], ',')){
$inputs = explode(",",$v[1]);
$new_querystring = ''; //empty querystring
$x=0;
foreach($inputs as $input){
//create key
$new_key = $v[0].'_'.$x;
//inject new key/value to array
$criterias[$new_key] = $input;
$new_querystring .= ':'.$new_key.',';
$x++;
}
//remove obsolete last character from new_querystring
$new_querystring = substr($new_querystring,0, -1);
//add new_querystring to clause
$clause .= ' AND pc.product_id IN ('.$new_querystring.')';
//remove original key/value from array
unset($criterias[$v[0]]);
}
else {
$clause .= ' AND pc.product_id IN (:'.$v[0].')';
}
}
elseif ($v[0] == 'category_id') {
//check if multiple category_ids are provided
if (str_contains($v[1], ',')){
$inputs = explode(",",$v[1]);
$new_querystring = ''; //empty querystring
$x=0;
foreach($inputs as $input){
//create key
$new_key = $v[0].'_'.$x;
//inject new key/value to array
$criterias[$new_key] = $input;
$new_querystring .= ':'.$new_key.',';
$x++;
}
//remove obsolete last character from new_querystring
$new_querystring = substr($new_querystring,0, -1);
$categoryCount = count($inputs);
//add new_querystring to clause
$clause .= ' AND pc.category_id IN ('.$new_querystring.')';
//remove original key/value from array
unset($criterias[$v[0]]);
}
else {
$clause .= ' AND pc.category_id IN (:'.$v[0].')';
$categoryCount = 1;
}
}
elseif ($v[0] == 'search') {
//build up search
$clause .= ' AND c.name like :'.$v[0];
}
elseif ($v[0] == 'filter') {
//build up filter
$clause .= ' AND c.filter = :'.$v[0];
}
else {//create clause
$clause .= ' AND pc.'.$v[0].' = :'.$v[0];
}
@@ -63,9 +119,17 @@ if(isset($criterias['totals']) && $criterias['totals'] ==''){
//Request for total rows
$sql = 'SELECT count(*) as count FROM products_categories '.$whereclause.'';
}
elseif(isset($criterias['list']) && $criterias['list'] =='filter'){
$sql = 'SELECT pc.product_id
FROM products_categories pc
'.$whereclause.'
GROUP BY pc.product_id
HAVING COUNT(DISTINCT pc.category_id) = '.$categoryCount;
}
else {
//SQL for Paging
$sql = 'SELECT c.name, c.rowID FROM products_categories pc JOIN categories c ON c.rowID = pc.category_id '.$whereclause.'';
$sql = 'SELECT c.* , pc.product_id FROM products_categories pc JOIN categories c ON c.rowID = pc.category_id '.$whereclause.'';
}
$stmt = $pdo->prepare($sql);
@@ -96,7 +160,7 @@ if(isset($criterias['totals']) && $criterias['totals']==''){
$messages = $stmt->fetch();
$messages = $messages[0];
}
elseif(isset($criterias['list']) && $criterias['list']==''){
elseif(isset($criterias['list']) && $criterias['list']=='filter'){
//Excute Query
$stmt->execute();
//Get results