From 0172f8405afdb6130c53f96931c6039b6b586b45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CVeLiTi=E2=80=9D?= <“info@veliti.nl”> Date: Tue, 4 Feb 2025 11:46:14 +0100 Subject: [PATCH] CMXX - Added categories filter for commerce --- api/v2/get/catalog.php | 34 ++++++++++++++- api/v2/get/products.php | 26 ++++++++++++ api/v2/get/products_categories.php | 68 +++++++++++++++++++++++++++++- categories.php | 2 + settings/settingsprofiles.php | 2 +- 5 files changed, 127 insertions(+), 5 deletions(-) diff --git a/api/v2/get/catalog.php b/api/v2/get/catalog.php index c9d6649..bf77ad0 100644 --- a/api/v2/get/catalog.php +++ b/api/v2/get/catalog.php @@ -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); diff --git a/api/v2/get/products.php b/api/v2/get/products.php index 8813172..30974b6 100644 --- a/api/v2/get/products.php +++ b/api/v2/get/products.php @@ -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]; } diff --git a/api/v2/get/products_categories.php b/api/v2/get/products_categories.php index ddea494..882b388 100644 --- a/api/v2/get/products_categories.php +++ b/api/v2/get/products_categories.php @@ -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 diff --git a/categories.php b/categories.php index d4e560f..3229530 100644 --- a/categories.php +++ b/categories.php @@ -61,6 +61,7 @@ function admin_populate_categories($categories, $parent_id = 0, $n = 0) { $html .= '