A
A
Anton2016-04-14 09:00:48
MODX
Anton, 2016-04-14 09:00:48

How to filter products?

There is a selection of the desired values:

<?php
//Присваивание значения в поле, показывающее максимально большую цену
    $modx->setPlaceholder('priceMaxValue', getPriceMaxValue());
    //Вовзращает самое большое значение из всех существующих в таблице (максимальная цена)
    function getPriceMaxValue(){
        global $modx;
        $filter = array("tmplvarid" => 5);
        $priceMaxValueArray = $modx->GetCollection("modTemplateVarResource", $filter);
        $maxValueResultArray = array();

        foreach($priceMaxValueArray as $k=>$v){
            $res = preg_replace('/[^0-9]/', '', $v->get("value"));
            array_push($maxValueResultArray, $res);
        }
        return max($maxValueResultArray);
    }
    //Фильтры по ценам
    function getCatPrice(){
        $catPrice = $_GET["catPrice"];
        $catPriceArray = array();
        global $modx;

        for($i=0; $i<count($catPrice); $i++){
            $filter = array("tmplvarid" => 21, "value" => $catPrice[$i]);
            $tvs = $modx->getCollection("modTemplateVarResource", $filter);

            foreach($tvs as $k=>$v){
                $id = $v->get("contentid");
                array_push($catPriceArray, $id);
            }
        }
        return $catPriceArray;
    }
    //Фильтр по диапазону цен
    function getRangePrice(){
        $priceMinValue = $_GET["priceMinValue"];
        $priceMaxValue = $_GET["priceMaxValue"];
        global $modx;

        $filter = array("tmplvarid" => 5);
        $tvs = $modx->getCollection("modTemplateVarResource", $filter);
        $rangePriceArray = array();

        foreach ($tvs as $k=>$v){
            if($v->get("value") >= $priceMinValue && $v->get("value") <= $priceMaxValue){
                $res = $v->get("contentid");
                array_push($rangePriceArray, $res);
            }
        }
        return $rangePriceArray;
    }
    //Фильтр по категории "На праздник"
    function getCatHoliday(){
        $catHoliday = $_GET["catHoliday"];
        global $modx;

        $filter = array("tmplvarid" => 19, "value" => $catHoliday);
        $tvs = $modx->getCollection("modTemplateVarResource", $filter);
        $catHolidayArray = array();

        foreach($tvs as $k=>$v){
            $res = $v->get("contentid");
            array_push($catHolidayArray, $res);
        }
        return $catHolidayArray;
    }
    //Фильтр по категории "Подарки"
    function getCatGifts(){
        $catGifts = $_GET["catGifts"];
        global $modx;
        
        $catGiftsArray = array();
        for($i=0;$i<count($catGifts);$i++){
            $filter = array("tmplvarid" => 18, "value" => $catGifts[$i]);
            $tvs = $modx->getCollection("modTemplateVarResource", $filter);
            
            foreach($tvs as $k=>$v){
                $res = $v->get("contentid");
                array_push($catGiftsArray, $res);
            }
        }
        return $catGiftsArray;
    }
    
    if($_GET["filter_submit"]){
        $globalArray = array();
        if($_GET["catPrice"]){
            $globalArray = array_merge($globalArray, getCatPrice());
        }
        if($_GET["priceMinValue"] && $_GET["priceMaxValue"]){
            $globalArray = array_merge($globalArray, getRangePrice());
        }
        if($_GET["catHoliday"]){
            $globalArray = array_merge($globalArray, getCatHoliday());
        }
        if($_GET["catGifts"]){
            $globalArray = array_merge($globalArray, getCatGifts());
        }
        $globalArray = array_unique($globalArray);
        $lastArray = implode(",", $globalArray);
        $modx->setPlaceholder('queryResult', $lastArray);
    }

At the output, I get a GET sheet like this:
catalog/?priceMinValue=1&priceMaxValue=77555&catHoliday=%238+марта%23&filter_submit=Отсортировать

Which, of course, does nothing. It doesn't understand these get values ​​for filtering.
I would like to display the selected resources through getResources\getPage snippets.
Please tell me how to implement the idea.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladislav Shkaev, 2016-04-14
@rudants

Umm, you use getCollection and at the same time want to run through getResources, there is little logic.
if according to your plan, you need to run getResources in this snippet and run through it the array that you fill in the functions

$output = $modx->runSnippet('getPage',array(
   'element' => 'getResources',
   'resources' => $array // id
));
return $output;

it is better to substitute the parameters obtained with GET with tvFilters not to stuff it once again through getCollection
$output = $modx->runSnippet('getPage',array(
   'element' => 'getResources',
   'parent' => 0,
   'tvFilters' =>  ''//  условие
));
return $output;

but in general for such filtering it is better to use getProducts, or even better pdoResources/pdoPage or even better to use the ready-made tagmanager2 snippet, it works with GET params

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question