Поиск товаров в Virtuemart 2 по нескольким фильтрам
Задача: создать для Виртумарта 2 (Joomla 2.5) поисковый фильтр, учитывающий сразу несколько параметров товара.
Решение:
1) Открываем файл \administrator\components\com_virtuemart\models\product.php
Находим в методе sortSearchListQuery следующий код:
if (!empty($this->searchcustoms)) {
$joinCustom = TRUE;
foreach ($this->searchcustoms as $key => $searchcustom) {
$custom_search[] = '(pf.`virtuemart_custom_id`="' . (int)$key . '" and pf.`custom_value` like "%' . $this->_db->getEscaped ($searchcustom, TRUE) . '%")';
}
$where[] = " ( " . implode (' OR ', $custom_search) . " ) ";
}
и заменяем его на:
if (!empty($this->searchcustoms)){
$joinCustom = true;
foreach ($this->searchcustoms as $key => $searchcustom) {
if($searchcustom=trim($searchcustom)){
$key=intval($key);
$custom_search[$key] = '(pf'.$key.'.`virtuemart_custom_id`="'.$key.'" and pf'.$key.'.`custom_value` like "' .
$this->_db->getEscaped( $searchcustom, true ) . '")';
}
}
if(count($custom_search)) $where[] = " ( ".implode(' AND ', $custom_search )." ) ";
else{
$joinCustom=false;
$this->searchcustoms=false;
}
}
2) ниже находим:
if ($this->searchcustoms) {
$joinedTables[] = ' LEFT JOIN `#__virtuemart_product_customfields` as pf ON p.`virtuemart_product_id` = pf.`virtuemart_product_id` ';
}
и тоже заменяем на:
if ($this->searchcustoms) {
foreach (array_keys($this->searchcustoms) as $key) {
$joinedTables .= ' LEFT JOIN `#__virtuemart_product_customfields` as pf'.$key.
' ON p.`virtuemart_product_id` = pf'.$key.'.`virtuemart_product_id` ';
}
}
3) открываем файл \components\com_virtuemart\views\category\tmpl\default.php
Находим строку
<div class="orderby-displaynumber">
и после нее заменяем этот код:
<div class="width70 floatleft"> <?php echo $this->orderByList['orderby']; ?> <?php echo $this->orderByList['manufacturer']; ?> </div>
на этот:
<?php
/*
Virtuemart -> Товары -> Настраиваемые поля -> смотрим первую колонку,
это ID записи, в нашем случаи это «Размер» и «Цвет», тип поля «Атрибут корзины».
*/
$results=array(4=>'Выбрать размер', 10=>'Выбрать цвет');
$customfields=array(4=>0, 10=>0);
$var=JRequest::getvar('customfields', null);
foreach($customfields as $custom_id => &$value){
if($var && !empty($var[$custom_id]))
$value='&customfields['.$custom_id.']='.$var[ $custom_id];
else unset($customfields[$custom_id]);
}
$db=JFactory::getDbo();
foreach($results as $custom_id => &$result){
$ff=$customfields;
unset($ff[$custom_id]);
$ff=implode('', $ff);
$title=$result;
$result='';
$db->setQuery('
SELECT DISTINCT(custom_value) AS value
FROM #__virtuemart_product_customfields WHERE virtuemart_custom_id='.
$custom_id);
$res=$db->loadObjectList();
if(!is_array($res) || !count($res)) continue;
//Массив значений
$a=array(0=>'Любой');
//Заносим данные из таблицы в массив значений
foreach($res as $v) $a[$v->value]=$v->value;
//Сортируем
ksort($a);
//Получаем значение выбранного фильтра из запроса пользователя
$value=JRequest::getvar('customfields', null);
$value=(isset($value[$custom_id])) ? $value[$custom_id] : 0;
//Это значение помещаем в начало массива
if($value && isset($a[$value])){
$x=array( $value=>'' );
foreach($a as $k => $v) $x[$k]=$v;
$a=$x;
}else $value=0;
//Создаем фильтр
$result='<div><div>'.$title.'</div>';
$flag=false;
foreach($a as $k => $v){
if((string)$k==(string)$value) $x='<div>';
else{
if(!$flag){
$x='<div><div>';
$flag=true;
}else $x='<div>';
}
$result.=$x.'<a href="'.JRoute::_(
'index.php?option=com_virtuemart&view=category&limitstart=0&virtuemart_category_id='.
$this->category->virtuemart_category_id.$ff.
(($k) ? '&customfields['.$custom_id.']='.$k : '')).'">'.$v.'</a></div>';
}
$result.='</div></div>';
}
?>
<div>
<table border=0 cellpadding="0" cellspacing="0" style="margin:0;padding:0">
<tr>
<td>
<?php echo $this->orderByList['orderby']; ?>
<?php echo $this->orderByList['manufacturer']; ?>
</td>
<td> <?php echo $results[4]; ?> </td>
<td> <?php echo $results[10]; ?> </td>
</tr>
</table>
</div>