yilmaz70

hata dosyasını teke satırını ikiye düşürdüm


olay bu iki satırda

/catalog/controller/extension/module/adv_ajaxfilter.php

model dosyasında düzelttim ama aynı işlemi controllerde yapmıyor



$min_price = $this->currency->convert($min_price * $this->k, $this->config->get('config_currency'), $this->currency->getCode());
$max_price = $this->currency->convert($max_price * $this->k, $this->config->get('config_currency'), $this->currency->getCode());




model dosyası :



class ModelModuleAdvajaxfilter extends Model {
private $adv_ajaxfilter_setting;

public function __construct($registry) {
parent::__construct($registry);

$this->adv_ajaxfilter_setting = $this->config->get('adv_ajaxfilter_setting');
if(VERSION == '1.5.0') {
$this->adv_ajaxfilter_setting = unserialize($this->config->get('adv_ajaxfilter_setting'));
}
}


public function getAttributes($data) {

$sql = "SELECT DISTINCT pa.text, a.`attribute_id`, ad.`name`, ag.attribute_group_id, agd.name as attribute_group_name FROM `" . DB_PREFIX . "product_attribute` pa" .
" LEFT JOIN " . DB_PREFIX . "attribute a ON(pa.attribute_id=a.`attribute_id`) " .
" LEFT JOIN " . DB_PREFIX . "attribute_description ad ON(a.attribute_id=ad.`attribute_id`) " .
" LEFT JOIN " . DB_PREFIX . "attribute_group ag ON(ag.attribute_group_id=a.`attribute_group_id`) " .
" LEFT JOIN " . DB_PREFIX . "attribute_group_description agd ON(agd.attribute_group_id=ag.`attribute_group_id`) " .
" LEFT JOIN " . DB_PREFIX . "product p ON(p.product_id=pa.`product_id`) ".
" LEFT JOIN " . DB_PREFIX . "product_description pd ON (pd.product_id=p.product_id) ";
if($data['category_id']) {
$sql .= " LEFT JOIN " . DB_PREFIX . "product_to_category p2c ON(p.product_id=p2c.product_id) ";
}
$sql .= " LEFT JOIN " . DB_PREFIX . "product_to_store p2s ON(p.product_id=p2s.product_id) ";
$sql .= " WHERE p.status = '1' AND p.date_available <= NOW() AND p2s.store_id =" . (int)$this->config->get('config_store_id');
if($data['category_id']) {
$sql .= " AND p2c.category_id = '" . (int)$data['category_id'] . "'";
}
if($data['manufacturer_id']) {
$sql .= " AND p.manufacturer_id = '" . (int)$data['manufacturer_id'] . "'";
}

if (isset($this->request->post['search']) and !empty($this->request->post['search'])) {
$words = explode(' ', trim(preg_replace('/\s+/', ' ', $this->request->post['search'])));

foreach ($words as $word) {
$sql .= " AND pd.name LIKE '%" . $this->db->escape($word) . "%'";
}
}

$sql .= " AND pa.language_id = '" . (int)$this->config->get('config_language_id') . "'" .
" AND ad.language_id = '" . (int)$this->config->get('config_language_id') . "'" .
" AND agd.language_id = '" . (int)$this->config->get('config_language_id') . "'" .
" ORDER BY ag.sort_order, agd.name, a.sort_order, ad.name, pa.text";

$query = $this->db->query($sql);


$attributes = array();
foreach($query->rows as $row) {
if(!isset($attributes[$row['attribute_group_id']])) {
$attributes[$row['attribute_group_id']] = array(
'name' => $row['attribute_group_name'],
'attribute_values' => array()
);
}

if(!isset($attributes[$row['attribute_group_id']]['attribute_values'][$row['attribute_id']])) {
$attributes[$row['attribute_group_id']]['attribute_values'][$row['attribute_id']] = array('name' => $row['name'], 'values' => array());
}

$row['text'] = htmlspecialchars_decode($row['text'], ENT_COMPAT);
foreach(explode($this->adv_ajaxfilter_setting['attr_delimeter'], $row['text']) as $text) {
if(!in_array($text, $attributes[$row['attribute_group_id']]['attribute_values'][$row['attribute_id']]['values'])) {
$attributes[$row['attribute_group_id']]['attribute_values'][$row['attribute_id']]['values'][] = htmlspecialchars($text, ENT_COMPAT);

}
}
}

foreach($attributes as $attribute_group_id => $attribute_group) {
foreach($attribute_group['attribute_values'] as $attribute_id => $attribute) {
sort($attributes[$attribute_group_id]['attribute_values'][$attribute_id]['values']);
}
}
return $attributes;
}

public function getManufacturers($data) {
if($data['manufacturer_id']) {
return array();
}
$sql = "SELECT DISTINCT m.`manufacturer_id`, m.`name`, m.`image` FROM `" . DB_PREFIX . "manufacturer` m" .
" LEFT JOIN " . DB_PREFIX . "product p ON(p.manufacturer_id=m.`manufacturer_id`) ".
" LEFT JOIN " . DB_PREFIX . "product_description pd ON (pd.product_id=p.product_id) ";
if($data['category_id']) {
$sql .= " LEFT JOIN " . DB_PREFIX . "product_to_category p2c ON(p.product_id=p2c.product_id) ";
}
$sql .= " LEFT JOIN " . DB_PREFIX . "product_to_store p2s ON(p.product_id=p2s.product_id) " .
" WHERE p.status = '1' AND p.date_available <= NOW() AND p2s.store_id = " . (int)$this->config->get('config_store_id');
if($data['category_id']) {
$sql .= " AND p2c.category_id = '" . (int)$data['category_id'] . "'";
}

if (isset($this->request->post['search']) and !empty($this->request->post['search'])) {
$words = explode(' ', trim(preg_replace('/\s+/', ' ', $this->request->post['search'])));

foreach ($words as $word) {
$sql .= " AND pd.name LIKE '%" . $this->db->escape($word) . "%'";
}
}

$sql .= " ORDER BY m.sort_order, m.name";
$query = $this->db->query($sql);
return $query->rows;
}

public function getTags($data) {
$sql = "SELECT CONCAT(p2t.`tag`, ' (', count(*), ')') as 'name', p2t.tag FROM `" . DB_PREFIX . "product_tag` p2t" .
" LEFT JOIN " . DB_PREFIX . "product p ON(p.product_id=p2t.`product_id`) ".
" LEFT JOIN " . DB_PREFIX . "product_description pd ON (pd.product_id=p.product_id) ";
if($data['category_id']) {
$sql .= " LEFT JOIN " . DB_PREFIX . "product_to_category p2c ON(p.product_id=p2c.product_id) ";
}
$sql .= " LEFT JOIN " . DB_PREFIX . "product_to_store p2s ON(p.product_id=p2s.product_id) " .
" WHERE p.status = '1' AND p2t.language_id = '" . (int)$this->config->get('config_language_id') . "' AND p.date_available <= NOW() AND p2s.store_id =" . (int)$this->config->get('config_store_id');
if($data['category_id']) {
$sql .= " AND p2c.category_id = '" . (int)$data['category_id'] . "'";
}
if($data['manufacturer_id']) {
$sql .= " AND p.manufacturer_id = '" . (int)$data['manufacturer_id'] . "'";
}

if (isset($this->request->post['search']) and !empty($this->request->post['search'])) {
$words = explode(' ', trim(preg_replace('/\s+/', ' ', $this->request->post['search'])));

foreach ($words as $word) {
$sql .= " AND pd.name LIKE '%" . $this->db->escape($word) . "%'";
}
}

$sql .= " GROUP BY p2t.`tag` ORDER BY p2t.`tag`";

$query = $this->db->query($sql);
return $query->rows;
}

public function getSubCategories($data) {
$sql = "SELECT DISTINCT cd.category_id, CONCAT(cd.name, ' (', COUNT(*), ')' ) AS 'name' FROM `" . DB_PREFIX . "category` c" .
" LEFT JOIN " . DB_PREFIX . "category_description cd ON(cd.category_id=c.category_id) " .
" LEFT JOIN " . DB_PREFIX . "product_to_category p2c ON(c.category_id=p2c.category_id) " .
" LEFT JOIN " . DB_PREFIX . "product p ON(p.product_id=p2c.`product_id`) " .
" LEFT JOIN " . DB_PREFIX . "product_description pd ON (pd.product_id=p.product_id) ".
" LEFT JOIN " . DB_PREFIX . "product_to_store p2s ON(p.product_id=p2s.product_id) " .
" WHERE c.status=1 AND c.parent_id = '" . (int)$data['category_id'] . "' AND cd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND p.status = '1' AND p.date_available <= NOW() AND p2s.store_id =" . (int)$this->config->get('config_store_id');
if($data['manufacturer_id']) {
$sql .= " AND p.manufacturer_id = '" . (int)$data['manufacturer_id'] . "'";
}

$sql .= " GROUP BY cd.category_id ORDER BY `c`.`sort_order`, `cd`.`name`";

$query = $this->db->query($sql);
return $query->rows;
}

public function getOptions($data) {
$sql_image = ", ov.image";
if(version_compare("1513", VERSION)<0) {
$sql_image = "";
}
$sql = "SELECT DISTINCT ovd.option_value_id, ovd.*, od.name as 'option_name'" . $sql_image . " FROM `" . DB_PREFIX . "option_value_description` ovd
LEFT JOIN " . DB_PREFIX . "option_value ov ON(ovd.option_value_id=ov.option_value_id)
LEFT JOIN " . DB_PREFIX . "option_description od ON(ov.option_id=od.option_id)
LEFT JOIN `" . DB_PREFIX . "option` o ON(ov.option_id=o.option_id)
LEFT JOIN " . DB_PREFIX . "product_option_value pov ON(ovd.`option_value_id`=pov.`option_value_id`)
LEFT JOIN " . DB_PREFIX . "product p ON(pov.product_id = p.product_id)
LEFT JOIN " . DB_PREFIX . "product_description pd ON (pd.product_id=p.product_id) ";

if($data['category_id']) {
$sql .= "LEFT JOIN " . DB_PREFIX . "product_to_category p2c ON(p.product_id = p2c.product_id) ";
}
$sql .= "LEFT JOIN " . DB_PREFIX . "product_to_store p2s ON(p.product_id=p2s.product_id)
WHERE ovd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND od.language_id = '" . (int)$this->config->get('config_language_id') . "' AND p.status = '1' AND p.date_available <= NOW() AND p2s.store_id =" . (int)$this->config->get('config_store_id');

if($data['category_id']) {
$sql .= " AND p2c.category_id = '" . (int)$data['category_id'] . "'";
}
if($data['manufacturer_id']) {
$sql .= " AND p.manufacturer_id = '" . (int)$data['manufacturer_id'] . "'";
}

if (isset($this->request->post['search']) and !empty($this->request->post['search'])) {
$words = explode(' ', trim(preg_replace('/\s+/', ' ', $this->request->post['search'])));

foreach ($words as $word) {
$sql .= " AND pd.name LIKE '%" . $this->db->escape($word) . "%'";
}
}

$sql .= " ORDER BY o.sort_order, ov.sort_order, ovd.option_id ";
$query = $this->db->query($sql);
$options = array();
foreach($query->rows as $row) {
if(!isset($options[$row['option_id']])) {
$options[$row['option_id']] = array('option_id' => $row['option_id'],
'name' => $row['option_name'],
'option_values' => array());
}

if(version_compare("1513", VERSION)<0) {
$row['image'] = "";
}
$options[$row['option_id']]['option_values'][] = array('option_value_id' => $row['option_value_id'], 'name' => $row['name'], 'image' => $row['image']);
}
return $options;
}

public function getPriceLimits($data) {

$customer_group_id = $this->getCustomerGroup();

$sql = "SELECT max(coalesce((SELECT price FROM " . DB_PREFIX . "product_discount pd2 WHERE pd2.product_id = p.product_id AND pd2.customer_group_id = '" . (int)$customer_group_id . "' AND pd2.quantity = '1' AND ((pd2.date_start = '0000-00-00' OR pd2.date_start < NOW()) AND (pd2.date_end = '0000-00-00' OR pd2.date_end > NOW())) ORDER BY pd2.priority ASC, pd2.price ASC LIMIT 1), " .
"(SELECT price FROM " . DB_PREFIX . "product_special ps WHERE ps.product_id = p.product_id AND ps.customer_group_id = '" . (int)$customer_group_id . "' AND ((ps.date_start = '0000-00-00' OR ps.date_start < NOW()) AND (ps.date_end = '0000-00-00' OR ps.date_end > NOW())) ORDER BY ps.priority ASC, ps.price ASC LIMIT 1), " .
"p.price) ) AS max_price, min(coalesce((SELECT price FROM " . DB_PREFIX . "product_discount pd2 WHERE pd2.product_id = p.product_id AND pd2.customer_group_id = '" . (int)$customer_group_id . "' AND pd2.quantity = '1' AND ((pd2.date_start = '0000-00-00' OR pd2.date_start < NOW()) AND (pd2.date_end = '0000-00-00' OR pd2.date_end > NOW())) ORDER BY pd2.priority ASC, pd2.price ASC LIMIT 1), " .
"(SELECT price FROM " . DB_PREFIX . "product_special ps WHERE ps.product_id = p.product_id AND ps.customer_group_id = '" . (int)$customer_group_id . "' AND ((ps.date_start = '0000-00-00' OR ps.date_start < NOW()) AND (ps.date_end = '0000-00-00' OR ps.date_end > NOW())) ORDER BY ps.priority ASC, ps.price ASC LIMIT 1), " .
"p.price) ) AS min_price FROM " . DB_PREFIX . "product p" .
" LEFT JOIN " . DB_PREFIX . "product_option_value pov ON (pov.product_id=p.product_id)" .
" LEFT JOIN " . DB_PREFIX . "product_description pd ON (pd.product_id=p.product_id)" .
" LEFT JOIN " . DB_PREFIX . "product_to_store p2s ON (p2s.product_id=p.product_id)";
if($data['category_id']) {
$sql .= " LEFT JOIN " . DB_PREFIX . "product_to_category p2c ON (p2c.product_id=p.product_id)";
}

if(isset($this->request->post['old_route']) and $this->request->post['old_route'] == 'product/special') {
$sql .= " RIGHT JOIN " . DB_PREFIX . "product_special ps2 ON (ps2.product_id=p.product_id)";
}

$sql .= " WHERE p.status = '1' AND p.date_available <= NOW( ) AND p2s.store_id = " . (int)$this->config->get('config_store_id');
if($data['category_id']) {
$sql .= " AND p2c.category_id = '" . (int)$data['category_id'] . "'";
}
if($data['manufacturer_id']) {
$sql .= " AND p.manufacturer_id = '" . (int)$data['manufacturer_id'] . "'";
}

if (isset($this->request->post['search']) and !empty($this->request->post['search'])) {
$words = explode(' ', trim(preg_replace('/\s+/', ' ', $this->request->post['search'])));

foreach ($words as $word) {
$sql .= " AND pd.name LIKE '%" . $this->db->escape($word) . "%'";
}
}

if(isset($this->request->post['old_route']) and $this->request->post['old_route'] == 'product/special') {
$sql .= " AND p.product_id IN (SELECT ps9.product_id FROM " . DB_PREFIX . "product_special AS ps9 WHERE (ps9.date_start = '0000-00-00' AND ps9.date_end = '0000-00-00') OR NOW() BETWEEN DATE(ps9.date_start) AND DATE(ps9.date_end))";
}

$query = $this->db->query($sql);

return $query->row;
}

public function getTotalProducts($data) {

$customer_group_id = $this->getCustomerGroup();

$sql = "SELECT count(*) as total FROM " .
"(SELECT DISTINCT p.product_id, coalesce((SELECT price FROM " . DB_PREFIX . "product_discount pd2 WHERE pd2.product_id = p.product_id AND pd2.customer_group_id = '" . (int)$customer_group_id . "' AND pd2.quantity = '1' AND ((pd2.date_start = '0000-00-00' OR pd2.date_start < NOW()) AND (pd2.date_end = '0000-00-00' OR pd2.date_end > NOW())) ORDER BY pd2.priority ASC, pd2.price ASC LIMIT 1), " .
"(SELECT price FROM " . DB_PREFIX . "product_special ps WHERE ps.product_id = p.product_id AND ps.customer_group_id = '" . (int)$customer_group_id . "' AND ((ps.date_start = '0000-00-00' OR ps.date_start < NOW()) AND (ps.date_end = '0000-00-00' OR ps.date_end > NOW())) ORDER BY ps.priority ASC, ps.price ASC LIMIT 1), " .
" p.price) as realprice " .
" FROM " . DB_PREFIX . "product p" .
" LEFT JOIN " . DB_PREFIX . "product_description pd ON (pd.product_id=p.product_id) ".
" LEFT JOIN " . DB_PREFIX . "product_option_value pov ON (pov.product_id=p.product_id)" .
" LEFT JOIN " . DB_PREFIX . "product_to_store p2s ON (p2s.product_id=p.product_id)" .
" LEFT JOIN " . DB_PREFIX . "product_to_category p2c ON (p2c.product_id=p.product_id)";
if($data['attribute_value'] || $data['attr_slider']) {
$sql .= " LEFT JOIN " . DB_PREFIX . "product_attribute p2a ON (p2a.product_id=p.product_id)";
}

if(isset($this->request->post['old_route']) and $this->request->post['old_route'] == 'product/special') {
$sql .= " RIGHT JOIN " . DB_PREFIX . "product_special ps2 ON (ps2.product_id=p.product_id)";
}

if ($data['filter']) {
$sql .= " LEFT JOIN " . DB_PREFIX . "product_filter pf ON (p2c.product_id = pf.product_id)";
}

$sql .= " WHERE 1";

if(isset($this->request->post['old_route']) and $this->request->post['old_route'] == 'product/special') {
$sql .= " AND p.product_id IN (SELECT ps9.product_id FROM " . DB_PREFIX . "product_special AS ps9 WHERE (ps9.date_start = '0000-00-00' AND ps9.date_end = '0000-00-00') OR NOW() BETWEEN DATE(ps9.date_start) AND DATE(ps9.date_end))";
}

if($data['instock']) {
$sql .= " AND p.quantity > 0 AND (pov.quantity is null OR pov.quantity > 0)";
}

if (isset($this->request->post['search']) and !empty($this->request->post['search'])) {
$words = explode(' ', trim(preg_replace('/\s+/', ' ', $this->request->post['search'])));

foreach ($words as $word) {
$sql .= " AND pd.name LIKE '%" . $this->db->escape($word) . "%'";
}
}

if($data['categories']) {
$sql .= " AND p2c.category_id IN (" . implode(",", $data['categories']) . ")";
}

$option_filters = array();
if($data['option_value']) {
foreach($data['option_value'] as $option_value) {
$option_filters[] = "option_value_id IN(" . implode(",", $option_value) . ")";
}
}

if($option_filters) {
if($this->adv_ajaxfilter_setting['option_mode'] == 'and') {
foreach($option_filters as $i => $option_filter) {
$sql .= " AND EXISTS (select 1 FROM " . DB_PREFIX . "product_option_value pov" . $i .
" WHERE pov" . $i . ".product_id=pov.product_id AND pov" . $i . "." . $option_filter . ($data['instock'] ? "AND pov" . $i . ".quantity > 0"
: "") . ") ";

}
} else {
$sql .= " AND (" . implode(" OR ", $option_filters) . ")";
}
}

if($data['manufacturer']) {
$sql .= " AND p.manufacturer_id IN(" . implode(", ", $data['manufacturer']) . ")";
}

$d = $this->adv_ajaxfilter_setting['attr_delimeter'];

if($data['attribute_value']) {
if($this->adv_ajaxfilter_setting['attribute_mode'] == 'and') {
$i = 0;

foreach($data['attribute_value'] as $attribute_id => $values) {
if($this->adv_ajaxfilter_setting['attribute_value_mode'] == 'or') {
$sql .= " AND EXISTS (select 1 FROM " . DB_PREFIX . "product_attribute p2a" . $i . " WHERE p2a" . $i . ".product_id=p2a.product_id AND p2a" . $i . ".attribute_id = " . (int)$attribute_id .
" AND (p2a" . $i . ".text = '" . implode("' OR p2a" . $i . ".text = '", array_map(array($this->db, 'escape'), $values)) . "'" .
" OR p2a" . $i . ".text like '" . implode($d . "%' OR p2a" . $i . ".text like '", array_map(array($this->db, 'escape'), $values)) . $d . "%'" .
" OR p2a" . $i . ".text like '%" . $d . implode("' OR p2a" . $i . ".text like '%" . $d, array_map(array($this->db, 'escape'), $values)) . "'" .
" OR p2a" . $i . ".text like '%" . $d . implode($d . "%' OR p2a" . $i . ".text like '%" . $d, array_map(array($this->db, 'escape'), $values)) . $d . "%')) ";
$i++;
} else {
foreach($values as $value) {
$sql .= " AND EXISTS (select 1 FROM " . DB_PREFIX . "product_attribute p2a" . $i . " WHERE p2a" . $i . ".product_id=p2a.product_id AND p2a" . $i . ".attribute_id = " . (int)$attribute_id .
" AND (p2a" . $i . ".text = '" . $this->db->escape($value) . "'" .
" OR p2a" . $i . ".text like '" . $this->db->escape($value) . $d . "%'" .
" OR p2a" . $i . ".text like '%" . $d . $this->db->escape($value) . "'" .
" OR p2a" . $i . ".text like '%" . $d . $this->db->escape($value) . $d . "%')) ";
$i++;
}
}
}
} else {
foreach($data['attribute_value'] as $attribute_id => $values) {
$attribute_filters[] = "p2a.attribute_id = " . (int)$attribute_id .
" AND (p2a.text = '" . implode("' OR p2a.text = '", array_map(array($this->db, 'escape'), $values)) . "'" .
" OR p2a.text like '" . implode($d . "%' OR p2a.text like '", array_map(array($this->db, 'escape'), $values)) . $d . "%'" .
" OR p2a.text like '%" . $d . implode("' OR p2a.text like '%" . $d, array_map(array($this->db, 'escape'), $values)) . "'" .
" OR p2a.text like '%" . $d . implode($d . "%' OR p2a.text like '%" . $d, array_map(array($this->db, 'escape'), $values)) . $d . "%')";
}
$sql .= " AND (" . implode(" OR ", $attribute_filters) . ")";
}
}

if($data['attr_slider']) {
$i = 0;
foreach($data['attr_slider'] as $attribute_id => $values) {
if(!isset($values['min'])) {
$sql .= " AND EXISTS (select 1 FROM " . DB_PREFIX . "product_attribute p2a" . $i . " WHERE p2a" . $i . ".product_id = p2a.product_id AND p2a" . $i . ".attribute_id = " . (int)$attribute_id . " AND " .
"(p2a" . $i . ".text * 1 <= " . $values['max'] . ")) ";
} elseif(!isset($values['max'])) {
$sql .= " AND EXISTS (select 1 FROM " . DB_PREFIX . "product_attribute p2a" . $i . " WHERE p2a" . $i . ".product_id = p2a.product_id AND p2a" . $i . ".attribute_id = " . (int)$attribute_id . " AND " .
"(p2a" . $i . ".text * 1 >= " . $values['min'] . ")) ";

} else {
$sql .= " AND EXISTS (select 1 FROM " . DB_PREFIX . "product_attribute p2a" . $i . " WHERE p2a" . $i . ".product_id = p2a.product_id AND p2a" . $i . ".attribute_id = " . (int)$attribute_id . " AND " .
"(p2a" . $i . ".text * 1 BETWEEN " . $values['min'] . " AND " . $values['max'] . ")) ";
}
$i++;
}
}

if($data['min_price'] >= 0) {
$min_price = $this->currency->convert((int)$data['min_price'],$this->config->get('config_currency') , $this->config->get('config_currency'));
$sql .= " AND p.price >=" . (int)$min_price;
}

if ($data['filter']) {
//todo:: add and/or for filter and filter group
if($this->adv_ajaxfilter_setting['filter_group_mode'] == 'and') {
$array_filter = array();
$array_group = array();
foreach($data['filter'] as $filter_group){
//$sql .= " AND pf.filter_id IN (" .implode(",", $filter). ")";
foreach($filter_group as $filter) {
$array_filter[] = $filter." IN (SELECT filter_id FROM " . DB_PREFIX . "product_filter pf WHERE pf.product_id = p.product_id) ";
}

$array_group[] = '( '. implode(' OR ', $array_filter). ')';
$array_filter = array();
}

$test = " AND ". implode(' AND ', $array_group);
$sql .= " AND ". implode(' AND ', $array_group);
}
else{
//todo:: add and/or for filter and filter group
$array = array();
foreach($data['filter'] as $filter){
$array = array_merge($array, $filter);
}

$sql .= " AND pf.filter_id IN (" .implode(",", $array). ")";
}
}

$sql .= " AND p.status = '1' AND p.date_available <= NOW( ) AND p2s.store_id = " . (int)$this->config->get('config_store_id');
$sql .= ") as innertable WHERE 1 ";
if($data['min_price'] >= 0) {
$min_price = $this->currency->convert((int)$data['min_price'],$this->config->get('config_currency') , $this->config->get('config_currency'));
$sql .= " AND realprice >=" . (int)$min_price;
}
if($data['max_price'] > 0) {
$max_price = $this->currency->convert($data['max_price'],$this->config->get('config_currency') , $this->config->get('config_currency'));
$sql .= " AND realprice <=" . ($max_price);
}
$query = $this->db->query($sql);

return $query->row['total'];
}

private function getCustomerGroup() {
if($this->customer->isLogged()) {
$customer_group_id = $this->customer->getGroupId();
return $customer_group_id;
} else {
$customer_group_id = $this->config->get('config_customer_group_id');
return $customer_group_id;
}
}

public function getTotalManufacturers($data) {

$customer_group_id = $this->getCustomerGroup();

$sql = "SELECT count(*) as total, manufacturer_id FROM " .
"(SELECT DISTINCT p.product_id, m.manufacturer_id, coalesce((SELECT price FROM " . DB_PREFIX . "product_discount pd2 WHERE pd2.product_id = p.product_id AND pd2.customer_group_id = '" . (int)$customer_group_id . "' AND pd2.quantity = '1' AND ((pd2.date_start = '0000-00-00' OR pd2.date_start < NOW()) AND (pd2.date_end = '0000-00-00' OR pd2.date_end > NOW())) ORDER BY pd2.priority ASC, pd2.price ASC LIMIT 1), " .
"(SELECT price FROM " . DB_PREFIX . "product_special ps WHERE ps.product_id = p.product_id AND ps.customer_group_id = '" . (int)$customer_group_id . "' AND ((ps.date_start = '0000-00-00' OR ps.date_start < NOW()) AND (ps.date_end = '0000-00-00' OR ps.date_end > NOW())) ORDER BY ps.priority ASC, ps.price ASC LIMIT 1), " .
"p.price) as realprice " .
" FROM " . DB_PREFIX . "product p" .
" LEFT JOIN " . DB_PREFIX . "product_description pd ON (pd.product_id=p.product_id) ".
" LEFT JOIN " . DB_PREFIX . "manufacturer m ON(m.manufacturer_id=p.manufacturer_id) " .
" LEFT JOIN " . DB_PREFIX . "product_option_value pov ON (pov.product_id=p.product_id)" .
" LEFT JOIN " . DB_PREFIX . "product_to_store p2s ON (p2s.product_id=p.product_id)" .
" LEFT JOIN " . DB_PREFIX . "product_to_category p2c ON (p2c.product_id=p.product_id)";
$sql .= " LEFT JOIN " . DB_PREFIX . "product_attribute p2a ON (p2a.product_id=p.product_id)";

if(isset($this->request->post['old_route']) and $this->request->post['old_route'] == 'product/special') {
$sql .= " RIGHT JOIN " . DB_PREFIX . "product_special ps2 ON (ps2.product_id=p.product_id)";
}

if ($data['filter']) {
$sql .= " LEFT JOIN " . DB_PREFIX . "product_filter pf ON (p2c.product_id = pf.product_id)";
}

$sql .= " WHERE 1";

if(isset($this->request->post['old_route']) and $this->request->post['old_route'] == 'product/special') {
$sql .= " AND p.product_id IN (SELECT ps9.product_id FROM " . DB_PREFIX . "product_special AS ps9 WHERE (ps9.date_start = '0000-00-00' AND ps9.date_end = '0000-00-00') OR NOW() BETWEEN DATE(ps9.date_start) AND DATE(ps9.date_end))";
}

if($data['instock']) {
$sql .= " AND p.quantity > 0 AND (pov.quantity is null OR pov.quantity > 0)";
}

if($data['categories']) {
$sql .= " AND p2c.category_id IN (" . implode(",", $data['categories']) . ")";
}

if (isset($this->request->post['search']) and !empty($this->request->post['search'])) {
$words = explode(' ', trim(preg_replace('/\s+/', ' ', $this->request->post['search'])));

foreach ($words as $word) {
$sql .= " AND pd.name LIKE '%" . $this->db->escape($word) . "%'";
}
}

$option_filters = array();
if($data['option_value']) {
foreach($data['option_value'] as $option_value) {
$option_filters[] = "option_value_id IN(" . implode(",", $option_value) . ")";
}
}

if($option_filters) {
if($this->adv_ajaxfilter_setting['option_mode'] == 'and') {
foreach($option_filters as $i => $option_filter) {
$sql .= " AND EXISTS (select 1 FROM " . DB_PREFIX . "product_option_value pov" . $i .
" WHERE pov" . $i . ".product_id=pov.product_id AND pov" . $i . "." . $option_filter . ($data['instock'] ? "AND pov" . $i . ".quantity > 0"
: "") . ") ";
}
} else {
$sql .= " AND (" . implode(" OR ", $option_filters) . ")";
}
}


$d = $this->adv_ajaxfilter_setting['attr_delimeter'];

if($data['attribute_value']) {
if($this->adv_ajaxfilter_setting['attribute_mode'] == 'and') {
$i = 0;

foreach($data['attribute_value'] as $attribute_id => $values) {
if($this->adv_ajaxfilter_setting['attribute_value_mode'] == 'or') {
$sql .= " AND EXISTS (select 1 FROM " . DB_PREFIX . "product_attribute p2a" . $i . " WHERE p2a" . $i . ".product_id=p2a.product_id AND p2a" . $i . ".attribute_id = " . (int)$attribute_id .
" AND (p2a" . $i . ".text = '" . implode("' OR p2a" . $i . ".text = '", array_map(array($this->db, 'escape'), $values)) . "'" .
" OR p2a" . $i . ".text like '" . implode($d . "%' OR p2a" . $i . ".text like '", array_map(array($this->db, 'escape'), $values)) . $d . "%'" .
" OR p2a" . $i . ".text like '%" . $d . implode("' OR p2a" . $i . ".text like '%" . $d, array_map(array($this->db, 'escape'), $values)) . "'" .
" OR p2a" . $i . ".text like '%" . $d . implode($d . "%' OR p2a" . $i . ".text like '%" . $d, array_map(array($this->db, 'escape'), $values)) . $d . "%')) ";
$i++;
} else {
foreach($values as $value) {
$sql .= " AND EXISTS (select 1 FROM " . DB_PREFIX . "product_attribute p2a" . $i . " WHERE p2a" . $i . ".product_id=p2a.product_id AND p2a" . $i . ".attribute_id = " . (int)$attribute_id .
" AND (p2a" . $i . ".text = '" . $this->db->escape($value) . "'" .
" OR p2a" . $i . ".text like '" . $this->db->escape($value) . $d . "%'" .
" OR p2a" . $i . ".text like '%" . $d . $this->db->escape($value) . "'" .
" OR p2a" . $i . ".text like '%" . $d . $this->db->escape($value) . $d . "%')) ";
$i++;
}
}
}
} else {
foreach($data['attribute_value'] as $attribute_id => $values) {
$attribute_filters[] = "p2a.attribute_id = " . (int)$attribute_id .
" AND (p2a.text = '" . implode("' OR p2a.text = '", array_map(array($this->db, 'escape'), $values)) . "'" .
" OR p2a.text like '" . implode($d . "%' OR p2a.text like '", array_map(array($this->db, 'escape'), $values)) . $d . "%'" .
" OR p2a.text like '%" . $d . implode("' OR p2a.text like '%" . $d, array_map(array($this->db, 'escape'), $values)) . "'" .
" OR p2a.text like '%" . $d . implode($d . "%' OR p2a.text like '%" . $d, array_map(array($this->db, 'escape'), $values)) . $d . "%')";
}
$sql .= " AND (" . implode(" OR ", $attribute_filters) . ")";
}
}

if($data['attr_slider']) {
$i = 0;
foreach($data['attr_slider'] as $attribute_id => $values) {
if(!isset($values['min'])) {
$sql .= " AND EXISTS (select 1 FROM " . DB_PREFIX . "product_attribute p2a" . $i . " WHERE p2a" . $i . ".product_id = p2a.product_id AND p2a" . $i . ".attribute_id = " . (int)$attribute_id . " AND " .
"(p2a" . $i . ".text * 1 <= " . $values['max'] . ")) ";
} elseif(!isset($values['max'])) {
$sql .= " AND EXISTS (select 1 FROM " . DB_PREFIX . "product_attribute p2a" . $i . " WHERE p2a" . $i . ".product_id = p2a.product_id AND p2a" . $i . ".attribute_id = " . (int)$attribute_id . " AND " .
"(p2a" . $i . ".text * 1 >= " . $values['min'] . ")) ";

} else {
$sql .= " AND EXISTS (select 1 FROM " . DB_PREFIX . "product_attribute p2a" . $i . " WHERE p2a" . $i . ".product_id = p2a.product_id AND p2a" . $i . ".attribute_id = " . (int)$attribute_id . " AND " .
"(p2a" . $i . ".text * 1 BETWEEN " . $values['min'] . " AND " . $values['max'] . ")) ";
}
$i++;
}
}


if($data['min_price'] >= 0) {
$min_price = $this->currency->convert((int)$data['min_price'], $this->config->get('config_currency') , $this->config->get('config_currency'));
$sql .= " AND p.price >=" . (int)$min_price;
}

if ($data['filter']) {
//todo:: add and/or for filter and filter group

$array = array();
foreach($data['filter'] as $filter){
$array = array_merge($array, $filter);
}

$sql .= " AND pf.filter_id IN (" .implode(",", $array). ")";
}

$sql .= " AND p.status = '1' AND p.date_available <= NOW( ) AND p2s.store_id = " . (int)$this->config->get('config_store_id');
$sql .= ") as innertable WHERE 1 ";
if($data['min_price'] >= 0) {
$min_price = $this->currency->convert((int)$data['min_price'],$this->config->get('config_currency') , $this->config->get('config_currency'));
$sql .= " AND realprice >=" . (int)$min_price;
}
if($data['max_price'] > 0) {
$max_price = $this->currency->convert($data['max_price'],$this->config->get('config_currency') , $this->config->get('config_currency'));
$sql .= " AND realprice <=" . ($max_price);
}

$sql .= " GROUP BY " . "manufacturer_id";
$query = $this->db->query($sql);

$result = array();
foreach($query->rows as $row) {
$result[] = array('id' => $row['manufacturer_id'], 't' => $row['total']);
}
return $result;
}

public function getTotalTags($data) {

$customer_group_id = $this->getCustomerGroup();

$sql = "SELECT CONCAT(`tag`, ' (', count(*), ')') as 'name', tag FROM " .
"(SELECT DISTINCT p.product_id, tag, coalesce((SELECT price FROM " . DB_PREFIX . "product_discount pd2 WHERE pd2.product_id = p.product_id AND pd2.customer_group_id = '" . (int)$customer_group_id . "' AND pd2.quantity = '1' AND ((pd2.date_start = '0000-00-00' OR pd2.date_start < NOW()) AND (pd2.date_end = '0000-00-00' OR pd2.date_end > NOW())) ORDER BY pd2.priority ASC, pd2.price ASC LIMIT 1), " .
"(SELECT price FROM " . DB_PREFIX . "product_special ps WHERE ps.product_id = p.product_id AND ps.customer_group_id = '" . (int)$customer_group_id . "' AND ((ps.date_start = '0000-00-00' OR ps.date_start < NOW()) AND (ps.date_end = '0000-00-00' OR ps.date_end > NOW())) ORDER BY ps.priority ASC, ps.price ASC LIMIT 1), " .
"p.price) as realprice " .
" FROM " . DB_PREFIX . "product p" .
" LEFT JOIN " . DB_PREFIX . "product_description pd ON (pd.product_id=p.product_id) ".
" LEFT JOIN " . DB_PREFIX . "manufacturer m ON(m.manufacturer_id=p.manufacturer_id) " .
" LEFT JOIN " . DB_PREFIX . "product_option_value pov ON (pov.product_id=p.product_id)" .
" LEFT JOIN " . DB_PREFIX . "product_to_store p2s ON (p2s.product_id=p.product_id)" .
" LEFT JOIN " . DB_PREFIX . "product_to_category p2c ON (p2c.product_id=p.product_id)";
$sql .= " LEFT JOIN " . DB_PREFIX . "product_attribute p2a ON (p2a.product_id=p.product_id)";
$sql .= " LEFT JOIN " . DB_PREFIX . "product_tag p2t ON (p2t.product_id=p.product_id)";

if(isset($this->request->post['old_route']) and $this->request->post['old_route'] == 'product/special') {
$sql .= " RIGHT JOIN " . DB_PREFIX . "product_special ps2 ON (ps2.product_id=p.product_id)";
}

if ($data['filter']) {
$sql .= " LEFT JOIN " . DB_PREFIX . "product_filter pf ON (p2c.product_id = pf.product_id)";
}

$sql .= " WHERE 1";

if(isset($this->request->post['old_route']) and $this->request->post['old_route'] == 'product/special') {
$sql .= " AND p.product_id IN (SELECT ps9.product_id FROM " . DB_PREFIX . "product_special AS ps9 WHERE (ps9.date_start = '0000-00-00' AND ps9.date_end = '0000-00-00') OR NOW() BETWEEN DATE(ps9.date_start) AND DATE(ps9.date_end))";
}

if($data['instock']) {
$sql .= " AND p.quantity > 0 AND (pov.quantity is null OR pov.quantity > 0)";
}

if($data['categories']) {
$sql .= " AND p2c.category_id IN (" . implode(",", $data['categories']) . ")";
}
if($data['manufacturer']) {
$sql .= " AND p.manufacturer_id IN(" . implode(", ", $data['manufacturer']) . ")";
}

if (isset($this->request->post['search']) and !empty($this->request->post['search'])) {
$words = explode(' ', trim(preg_replace('/\s+/', ' ', $this->request->post['search'])));

foreach ($words as $word) {
$sql .= " AND pd.name LIKE '%" . $this->db->escape($word) . "%'";
}
}

$option_filters = array();
if($data['option_value']) {
foreach($data['option_value'] as $option_value) {
$option_filters[] = "option_value_id IN(" . implode(",", $option_value) . ")";
}
}

if($option_filters) {
if($this->adv_ajaxfilter_setting['option_mode'] == 'and') {
foreach($option_filters as $i => $option_filter) {
$sql .= " AND EXISTS (select 1 FROM " . DB_PREFIX . "product_option_value pov" . $i .
" WHERE pov" . $i . ".product_id=pov.product_id AND pov" . $i . "." . $option_filter . ($data['instock'] ? "AND pov" . $i . ".quantity > 0"
: "") . ") ";
}
} else {
$sql .= " AND (" . implode(" OR ", $option_filters) . ")";
}
}

$d = $this->adv_ajaxfilter_setting['attr_delimeter'];

if($data['attribute_value']) {
if($this->adv_ajaxfilter_setting['attribute_mode'] == 'and') {
$i = 0;

foreach($data['attribute_value'] as $attribute_id => $values) {
if($this->adv_ajaxfilter_setting['attribute_value_mode'] == 'or') {
$sql .= " AND EXISTS (select 1 FROM " . DB_PREFIX . "product_attribute p2a" . $i . " WHERE p2a" . $i . ".product_id=p2a.product_id AND p2a" . $i . ".attribute_id = " . (int)$attribute_id .
" AND (p2a" . $i . ".text = '" . implode("' OR p2a" . $i . ".text = '", array_map(array($this->db, 'escape'), $values)) . "'" .
" OR p2a" . $i . ".text like '" . implode($d . "%' OR p2a" . $i . ".text like '", array_map(array($this->db, 'escape'), $values)) . $d . "%'" .
" OR p2a" . $i . ".text like '%" . $d . implode("' OR p2a" . $i . ".text like '%" . $d, array_map(array($this->db, 'escape'), $values)) . "'" .
" OR p2a" . $i . ".text like '%" . $d . implode($d . "%' OR p2a" . $i . ".text like '%" . $d, array_map(array($this->db, 'escape'), $values)) . $d . "%')) ";
$i++;
} else {
foreach($values as $value) {
$sql .= " AND EXISTS (select 1 FROM " . DB_PREFIX . "product_attribute p2a" . $i . " WHERE p2a" . $i . ".product_id=p2a.product_id AND p2a" . $i . ".attribute_id = " . (int)$attribute_id .
" AND (p2a" . $i . ".text = '" . $this->db->escape($value) . "'" .
" OR p2a" . $i . ".text like '" . $this->db->escape($value) . $d . "%'" .
" OR p2a" . $i . ".text like '%" . $d . $this->db->escape($value) . "'" .
" OR p2a" . $i . ".text like '%" . $d . $this->db->escape($value) . $d . "%')) ";
$i++;
}
}
}
} else {
foreach($data['attribute_value'] as $attribute_id => $values) {
$attribute_filters[] = "p2a.attribute_id = " . (int)$attribute_id .
" AND (p2a.text = '" . implode("' OR p2a.text = '", array_map(array($this->db, 'escape'), $values)) . "'" .
" OR p2a.text like '" . implode($d . "%' OR p2a.text like '", array_map(array($this->db, 'escape'), $values)) . $d . "%'" .
" OR p2a.text like '%" . $d . implode("' OR p2a.text like '%" . $d, array_map(array($this->db, 'escape'), $values)) . "'" .
" OR p2a.text like '%" . $d . implode($d . "%' OR p2a.text like '%" . $d, array_map(array($this->db, 'escape'), $values)) . $d . "%')";
}
$sql .= " AND (" . implode(" OR ", $attribute_filters) . ")";
}
}

if($data['attr_slider']) {
$i = 0;
foreach($data['attr_slider'] as $attribute_id => $values) {
if(!isset($values['min'])) {
$sql .= " AND EXISTS (select 1 FROM " . DB_PREFIX . "product_attribute p2a" . $i . " WHERE p2a" . $i . ".product_id = p2a.product_id AND p2a" . $i . ".attribute_id = " . (int)$attribute_id . " AND " .
"(p2a" . $i . ".text * 1 <= " . $values['max'] . ")) ";
} elseif(!isset($values['max'])) {
$sql .= " AND EXISTS (select 1 FROM " . DB_PREFIX . "product_attribute p2a" . $i . " WHERE p2a" . $i . ".product_id = p2a.product_id AND p2a" . $i . ".attribute_id = " . (int)$attribute_id . " AND " .
"(p2a" . $i . ".text * 1 >= " . $values['min'] . ")) ";

} else {
$sql .= " AND EXISTS (select 1 FROM " . DB_PREFIX . "product_attribute p2a" . $i . " WHERE p2a" . $i . ".product_id = p2a.product_id AND p2a" . $i . ".attribute_id = " . (int)$attribute_id . " AND " .
"(p2a" . $i . ".text * 1 BETWEEN " . $values['min'] . " AND " . $values['max'] . ")) ";
}
$i++;
}
}

if($data['min_price'] >= 0) {
$min_price = $this->currency->convert((int)$data['min_price'],$this->config->get('config_currency') , $this->config->get('config_currency'));
$sql .= " AND p.price >=" . (int)$min_price;
}

if ($data['filter']) {
//todo:: add and/or for filter and filter group

$array = array();
foreach($data['filter'] as $filter){
$array = array_merge($array, $filter);
}

$sql .= " AND pf.filter_id IN (" .implode(",", $array). ")";
}

$sql .= " AND p2t.language_id = '" . (int)$this->config->get('config_language_id') . "' AND p.status = '1' AND p.date_available <= NOW( ) AND p2s.store_id = " . (int)$this->config->get('config_store_id');
$sql .= ") as innertable WHERE 1 ";
if($data['min_price'] >= 0) {
$min_price = $this->currency->convert((int)$data['min_price'],$this->config->get('config_currency') , $this->config->get('config_currency'));
$sql .= " AND realprice >=" . (int)$min_price;
}
if($data['max_price'] > 0) {
$max_price = $this->currency->convert($data['max_price'],$this->config->get('config_currency') , $this->config->get('config_currency'));
$sql .= " AND realprice <=" . ($max_price);
}

$sql .= " GROUP BY `tag` ORDER BY `tag`";
$query = $this->db->query($sql);
foreach($query->rows as $i => $row) {
if($data['tags'] && in_array($row['tag'], $data['tags'])) {
$query->rows[$i]['checked'] = "1";
}
}
return $query->rows;
}

public function getTotalCategories($data, $category_id) {

$customer_group_id = $this->getCustomerGroup();

$sql = "SELECT CONCAT(`name`, ' (', count(*), ')') as 'name', `category_id` FROM " .
"(SELECT DISTINCT p.product_id, cd.name, cd.category_id, c.sort_order, coalesce((SELECT price FROM " . DB_PREFIX . "product_discount pd2 WHERE pd2.product_id = p.product_id AND pd2.customer_group_id = '" . (int)$customer_group_id . "' AND pd2.quantity = '1' AND ((pd2.date_start = '0000-00-00' OR pd2.date_start < NOW()) AND (pd2.date_end = '0000-00-00' OR pd2.date_end > NOW())) ORDER BY pd2.priority ASC, pd2.price ASC LIMIT 1), " .
"(SELECT price FROM " . DB_PREFIX . "product_special ps WHERE ps.product_id = p.product_id AND ps.customer_group_id = '" . (int)$customer_group_id . "' AND ((ps.date_start = '0000-00-00' OR ps.date_start < NOW()) AND (ps.date_end = '0000-00-00' OR ps.date_end > NOW())) ORDER BY ps.priority ASC, ps.price ASC LIMIT 1), " .
"p.price) as realprice " .
" FROM " . DB_PREFIX . "product p" .
" LEFT JOIN " . DB_PREFIX . "product_description pd ON (pd.product_id=p.product_id) ".
" LEFT JOIN " . DB_PREFIX . "manufacturer m ON(m.manufacturer_id=p.manufacturer_id) " .
" LEFT JOIN " . DB_PREFIX . "product_option_value pov ON (pov.product_id=p.product_id)" .
" LEFT JOIN " . DB_PREFIX . "product_to_store p2s ON (p2s.product_id=p.product_id)" .
" LEFT JOIN " . DB_PREFIX . "product_to_category p2c ON (p2c.product_id=p.product_id)" .
" LEFT JOIN `" . DB_PREFIX . "category` c ON(p2c.category_id=c.category_id) " .
" LEFT JOIN " . DB_PREFIX . "category_description cd ON(cd.category_id=c.category_id) ";
$sql .= " LEFT JOIN " . DB_PREFIX . "product_attribute p2a ON (p2a.product_id=p.product_id)";

if(isset($this->request->post['old_route']) and $this->request->post['old_route'] == 'product/special') {
$sql .= " RIGHT JOIN " . DB_PREFIX . "product_special ps2 ON (ps2.product_id=p.product_id)";
}


if ($data['filter']) {
$sql .= " LEFT JOIN " . DB_PREFIX . "product_filter pf ON (p2c.product_id = pf.product_id)";
}

$sql .= " WHERE 1";

if(isset($this->request->post['old_route']) and $this->request->post['old_route'] == 'product/special') {
$sql .= " AND p.product_id IN (SELECT ps9.product_id FROM " . DB_PREFIX . "product_special AS ps9 WHERE (ps9.date_start = '0000-00-00' AND ps9.date_end = '0000-00-00') OR NOW() BETWEEN DATE(ps9.date_start) AND DATE(ps9.date_end))";
}

if($data['instock']) {
$sql .= " AND p.quantity > 0 AND (pov.quantity is null OR pov.quantity > 0)";
}

if($data['manufacturer']) {
$sql .= " AND p.manufacturer_id IN(" . implode(", ", $data['manufacturer']) . ")";
}

if (isset($this->request->post['search']) and !empty($this->request->post['search'])) {
$words = explode(' ', trim(preg_replace('/\s+/', ' ', $this->request->post['search'])));

foreach ($words as $word) {
$sql .= " AND pd.name LIKE '%" . $this->db->escape($word) . "%'";
}
}

$option_filters = array();
if($data['option_value']) {
foreach($data['option_value'] as $option_value) {
$option_filters[] = "option_value_id IN(" . implode(",", $option_value) . ")";
}
}

if($option_filters) {
if($this->adv_ajaxfilter_setting['option_mode'] == 'and') {
foreach($option_filters as $i => $option_filter) {
$sql .= " AND EXISTS (select 1 FROM " . DB_PREFIX . "product_option_value pov" . $i .
" WHERE pov" . $i . ".product_id=pov.product_id AND pov" . $i . "." . $option_filter . ($data['instock'] ? "AND pov" . $i . ".quantity > 0"
: "") . ") ";
}
} else {
$sql .= " AND (" . implode(" OR ", $option_filters) . ")";
}
}

$d = $this->adv_ajaxfilter_setting['attr_delimeter'];

if($data['attribute_value']) {
if($this->adv_ajaxfilter_setting['attribute_mode'] == 'and') {
$i = 0;

foreach($data['attribute_value'] as $attribute_id => $values) {
if($this->adv_ajaxfilter_setting['attribute_value_mode'] == 'or') {
$sql .= " AND EXISTS (select 1 FROM " . DB_PREFIX . "product_attribute p2a" . $i . " WHERE p2a" . $i . ".product_id=p2a.product_id AND p2a" . $i . ".attribute_id = " . (int)$attribute_id .
" AND (p2a" . $i . ".text = '" . implode("' OR p2a" . $i . ".text = '", array_map(array($this->db, 'escape'), $values)) . "'" .
" OR p2a" . $i . ".text like '" . implode($d . "%' OR p2a" . $i . ".text like '", array_map(array($this->db, 'escape'), $values)) . $d . "%'" .
" OR p2a" . $i . ".text like '%" . $d . implode("' OR p2a" . $i . ".text like '%" . $d, array_map(array($this->db, 'escape'), $values)) . "'" .
" OR p2a" . $i . ".text like '%" . $d . implode($d . "%' OR p2a" . $i . ".text like '%" . $d, array_map(array($this->db, 'escape'), $values)) . $d . "%')) ";
$i++;
} else {
foreach($values as $value) {
$sql .= " AND EXISTS (select 1 FROM " . DB_PREFIX . "product_attribute p2a" . $i . " WHERE p2a" . $i . ".product_id=p2a.product_id AND p2a" . $i . ".attribute_id = " . (int)$attribute_id .
" AND (p2a" . $i . ".text = '" . $this->db->escape($value) . "'" .
" OR p2a" . $i . ".text like '" . $this->db->escape($value) . $d . "%'" .
" OR p2a" . $i . ".text like '%" . $d . $this->db->escape($value) . "'" .
" OR p2a" . $i . ".text like '%" . $d . $this->db->escape($value) . $d . "%')) ";
$i++;
}
}
}
} else {
foreach($data['attribute_value'] as $attribute_id => $values) {
$attribute_filters[] = "p2a.attribute_id = " . (int)$attribute_id .
" AND (p2a.text = '" . implode("' OR p2a.text = '", array_map(array($this->db, 'escape'), $values)) . "'" .
" OR p2a.text like '" . implode($d . "%' OR p2a.text like '", array_map(array($this->db, 'escape'), $values)) . $d . "%'" .
" OR p2a.text like '%" . $d . implode("' OR p2a.text like '%" . $d, array_map(array($this->db, 'escape'), $values)) . "'" .
" OR p2a.text like '%" . $d . implode($d . "%' OR p2a.text like '%" . $d, array_map(array($this->db, 'escape'), $values)) . $d . "%')";
}
$sql .= " AND (" . implode(" OR ", $attribute_filters) . ")";
}
}

if($data['attr_slider']) {
$i = 0;
foreach($data['attr_slider'] as $attribute_id => $values) {
if(!isset($values['min'])) {
$sql .= " AND EXISTS (select 1 FROM " . DB_PREFIX . "product_attribute p2a" . $i . " WHERE p2a" . $i . ".product_id = p2a.product_id AND p2a" . $i . ".attribute_id = " . (int)$attribute_id . " AND " .
"(p2a" . $i . ".text * 1 <= " . $values['max'] . ")) ";
} elseif(!isset($values['max'])) {
$sql .= " AND EXISTS (select 1 FROM " . DB_PREFIX . "product_attribute p2a" . $i . " WHERE p2a" . $i . ".product_id = p2a.product_id AND p2a" . $i . ".attribute_id = " . (int)$attribute_id . " AND " .
"(p2a" . $i . ".text * 1 >= " . $values['min'] . ")) ";

} else {
$sql .= " AND EXISTS (select 1 FROM " . DB_PREFIX . "product_attribute p2a" . $i . " WHERE p2a" . $i . ".product_id = p2a.product_id AND p2a" . $i . ".attribute_id = " . (int)$attribute_id . " AND " .
"(p2a" . $i . ".text * 1 BETWEEN " . $values['min'] . " AND " . $values['max'] . ")) ";
}
$i++;
}
}

if($data['min_price'] >= 0) {
$min_price = $this->currency->convert((int)$data['min_price'],$this->config->get('config_currency') , $this->config->get('config_currency'));
$sql .= " AND p.price >=" . (int)$min_price;
}

if ($data['filter']) {
//todo:: add and/or for filter and filter group

$array = array();
foreach($data['filter'] as $filter){
$array = array_merge($array, $filter);
}

$sql .= " AND pf.filter_id IN (" .implode(",", $array). ")";
}

$sql .= " AND c.status=1 AND c.parent_id = '" . $category_id . "' AND cd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND p.status = '1' AND p.date_available <= NOW( ) AND p2s.store_id = " . (int)$this->config->get('config_store_id');
$sql .= ") as innertable WHERE 1 ";
if($data['min_price'] >= 0) {
$min_price = $this->currency->convert((int)$data['min_price'],$this->config->get('config_currency') , $this->config->get('config_currency'));
$sql .= " AND realprice >=" . (int)$min_price;
}
if($data['max_price'] > 0) {
$max_price = $this->currency->convert($data['max_price'],$this->config->get('config_currency') , $this->config->get('config_currency'));
$sql .= " AND realprice <=" . ($max_price);
}

$sql .= " GROUP BY category_id ORDER BY sort_order, name";

$query = $this->db->query($sql);
foreach($query->rows as $i => $row) {
if($data['categories'] && in_array($row['category_id'], $data['categories'])) {
$query->rows[$i]['checked'] = "1";
}
}
return $query->rows;
}

public function getTotalAttributes($data) {

$customer_group_id = $this->getCustomerGroup();

$sql = "SELECT count(*) as total, attribute_id, text FROM " .
"(SELECT DISTINCT p.product_id, p2a.attribute_id, p2a.text, coalesce((SELECT price FROM " . DB_PREFIX . "product_discount pd2 WHERE pd2.product_id = p.product_id AND pd2.customer_group_id = '" . (int)$customer_group_id . "' AND pd2.quantity = '1' AND ((pd2.date_start = '0000-00-00' OR pd2.date_start < NOW()) AND (pd2.date_end = '0000-00-00' OR pd2.date_end > NOW())) ORDER BY pd2.priority ASC, pd2.price ASC LIMIT 1), " .
"(SELECT price FROM " . DB_PREFIX . "product_special ps WHERE ps.product_id = p.product_id AND ps.customer_group_id = '" . (int)$customer_group_id . "' AND ((ps.date_start = '0000-00-00' OR ps.date_start < NOW()) AND (ps.date_end = '0000-00-00' OR ps.date_end > NOW())) ORDER BY ps.priority ASC, ps.price ASC LIMIT 1), " .
"p.price) as realprice " .
" FROM " . DB_PREFIX . "product p" .
" LEFT JOIN " . DB_PREFIX . "product_description pd ON (pd.product_id=p.product_id) ".
" LEFT JOIN " . DB_PREFIX . "manufacturer m ON(m.manufacturer_id=p.manufacturer_id) " .
" LEFT JOIN " . DB_PREFIX . "product_option_value pov ON (pov.product_id=p.product_id)" .
" LEFT JOIN " . DB_PREFIX . "product_to_store p2s ON (p2s.product_id=p.product_id)" .
" LEFT JOIN " . DB_PREFIX . "product_to_category p2c ON (p2c.product_id=p.product_id)";
$sql .= " LEFT JOIN " . DB_PREFIX . "product_attribute p2a ON (p2a.product_id=p.product_id)";

if(isset($this->request->post['old_route']) and $this->request->post['old_route'] == 'product/special') {
$sql .= " RIGHT JOIN " . DB_PREFIX . "product_special ps2 ON (ps2.product_id=p.product_id)";
}

if ($data['filter']) {
$sql .= " LEFT JOIN " . DB_PREFIX . "product_filter pf ON (p2c.product_id = pf.product_id)";
}

$sql .= " WHERE 1";

if(isset($this->request->post['old_route']) and $this->request->post['old_route'] == 'product/special') {
$sql .= " AND p.product_id IN (SELECT ps9.product_id FROM " . DB_PREFIX . "product_special AS ps9 WHERE (ps9.date_start = '0000-00-00' AND ps9.date_end = '0000-00-00') OR NOW() BETWEEN DATE(ps9.date_start) AND DATE(ps9.date_end))";
}

if($data['instock']) {
$sql .= " AND p.quantity > 0 AND (pov.quantity is null OR pov.quantity > 0)";
}

if($data['categories']) {
$sql .= " AND p2c.category_id IN (" . implode(",", $data['categories']) . ")";
}

if($data['manufacturer']) {
$sql .= " AND p.manufacturer_id IN(" . implode(", ", $data['manufacturer']) . ")";
}

if (isset($this->request->post['search']) and !empty($this->request->post['search'])) {
$words = explode(' ', trim(preg_replace('/\s+/', ' ', $this->request->post['search'])));

foreach ($words as $word) {
$sql .= " AND pd.name LIKE '%" . $this->db->escape($word) . "%'";
}
}

$d = $this->adv_ajaxfilter_setting['attr_delimeter'];

if($data['attribute_value']) {
if($this->adv_ajaxfilter_setting['attribute_mode'] == 'and') {
$i = 0;
foreach($data['attribute_value'] as $attribute_id => $values) {
if($this->adv_ajaxfilter_setting['attribute_value_mode'] == 'or') {
$sql .= " AND EXISTS (select 1 FROM " . DB_PREFIX . "product_attribute p2a" . $i . " WHERE p2a" . $i . ".product_id=p2a.product_id AND p2a" . $i . ".attribute_id = " . (int)$attribute_id .
" AND (p2a" . $i . ".text = '" . implode("' OR p2a" . $i . ".text = '", array_map(array($this->db, 'escape'), $values)) . "'" .
" OR p2a" . $i . ".text like '" . implode($d . "%' OR p2a" . $i . ".text like '", array_map(array($this->db, 'escape'), $values)) . $d . "%'" .
" OR p2a" . $i . ".text like '%" . $d . implode("' OR p2a" . $i . ".text like '%" . $d, array_map(array($this->db, 'escape'), $values)) . "'" .
" OR p2a" . $i . ".text like '%" . $d . implode($d . "%' OR p2a" . $i . ".text like '%" . $d, array_map(array($this->db, 'escape'), $values)) . $d . "%')) ";
$i++;
} else {
foreach($values as $value) {
$sql .= " AND EXISTS (select 1 FROM " . DB_PREFIX . "product_attribute p2a" . $i . " WHERE p2a" . $i . ".product_id=p2a.product_id AND p2a" . $i . ".attribute_id = " . (int)$attribute_id .
" AND (p2a" . $i . ".text = '" . $this->db->escape($value) . "'" .
" OR p2a" . $i . ".text like '" . $this->db->escape($value) . $d . "%'" .
" OR p2a" . $i . ".text like '%" . $d . $this->db->escape($value) . "'" .
" OR p2a" . $i . ".text like '%" . $d . $this->db->escape($value) . $d . "%')) ";
$i++;
}
}
}
}
}

if($data['attr_slider']) {
$i = 0;
foreach($data['attr_slider'] as $attribute_id => $values) {
if(!isset($values['min'])) {
$sql .= " AND EXISTS (select 1 FROM " . DB_PREFIX . "product_attribute p2a" . $i . " WHERE p2a" . $i . ".product_id = p2a.product_id AND p2a" . $i . ".attribute_id = " . (int)$attribute_id . " AND " .
"(p2a" . $i . ".text * 1 <= " . $values['max'] . ")) ";
} elseif(!isset($values['max'])) {
$sql .= " AND EXISTS (select 1 FROM " . DB_PREFIX . "product_attribute p2a" . $i . " WHERE p2a" . $i . ".product_id = p2a.product_id AND p2a" . $i . ".attribute_id = " . (int)$attribute_id . " AND " .
"(p2a" . $i . ".text * 1 >= " . $values['min'] . ")) ";

} else {
$sql .= " AND EXISTS (select 1 FROM " . DB_PREFIX . "product_attribute p2a" . $i . " WHERE p2a" . $i . ".product_id = p2a.product_id AND p2a" . $i . ".attribute_id = " . (int)$attribute_id . " AND " .
"(p2a" . $i . ".text * 1 BETWEEN " . $values['min'] . " AND " . $values['max'] . ")) ";
}
$i++;
}
}

$option_filters = array();
if($data['option_value']) {
foreach($data['option_value'] as $option_value) {
$option_filters[] = "option_value_id IN(" . implode(",", $option_value) . ")";
}
}

if($option_filters) {
if($this->adv_ajaxfilter_setting['option_mode'] == 'and') {
foreach($option_filters as $i => $option_filter) {
$sql .= " AND EXISTS (select 1 FROM " . DB_PREFIX . "product_option_value pov" . $i .
" WHERE pov" . $i . ".product_id=pov.product_id AND pov" . $i . "." . $option_filter . ($data['instock'] ? "AND pov" . $i . ".quantity > 0"
: "") . ") ";
}
} else {
$sql .= " AND (" . implode(" OR ", $option_filters) . ")";
}
}


if($data['min_price'] >= 0) {
$min_price = $this->currency->convert((int)$data['min_price'],$this->config->get('config_currency') , $this->config->get('config_currency'));
$sql .= " AND p.price >=" . (int)$min_price;
}

if ($data['filter']) {
//todo:: add and/or for filter and filter group

$array = array();
foreach($data['filter'] as $filter){
$array = array_merge($array, $filter);
}

$sql .= " AND pf.filter_id IN (" .implode(",", $array). ")";
}

$sql .= " AND p.status = '1' AND p.date_available <= NOW( ) AND p2a.language_id='" . (int)$this->config->get('config_language_id') . "' AND p2s.store_id = " . (int)$this->config->get('config_store_id');
$sql .= ") as innertable WHERE 1 ";
if($data['min_price'] >= 0) {
$min_price = $this->currency->convert((int)$data['min_price'],$this->config->get('config_currency') , $this->config->get('config_currency'));
$sql .= " AND realprice >=" . (int)$min_price;
}
if($data['max_price'] > 0) {
$max_price = $this->currency->convert($data['max_price'],$this->config->get('config_currency') , $this->config->get('config_currency'));
$sql .= " AND realprice <=" . ($max_price);
}

$sql .= " GROUP BY " . "attribute_id, text";
$query = $this->db->query($sql);

$result = array();
$attributes = array();
foreach($query->rows as $row) {
$row['text'] = htmlspecialchars_decode($row['text'], ENT_COMPAT);
foreach(explode($this->adv_ajaxfilter_setting['attr_delimeter'], $row['text']) as $text) {
if(!isset($attributes[$row['attribute_id'] . "." . $text])) {
$attributes[$row['attribute_id'] . "." . $text] = array('id' => $row['attribute_id'], 'text' => $text, 't' => $row['total']);
} else {
$attributes[$row['attribute_id'] . "." . $text]['t'] += $row['total'];
}
}
}

foreach($attributes as $attribute) {
$result[] = array('id' => $attribute['id'], 'text' => $attribute['text'], 't' => $attribute['t']);
}
return $result;
}

public function getTotalOptions($data) {

$customer_group_id = $this->getCustomerGroup();

$sql = "SELECT count(*) as total, option_value_id FROM " .
"(SELECT DISTINCT p.product_id, pov.option_value_id, coalesce((SELECT price FROM " . DB_PREFIX . "product_discount pd2 WHERE pd2.product_id = p.product_id AND pd2.customer_group_id = '" . (int)$customer_group_id . "' AND pd2.quantity = '1' AND ((pd2.date_start = '0000-00-00' OR pd2.date_start < NOW()) AND (pd2.date_end = '0000-00-00' OR pd2.date_end > NOW())) ORDER BY pd2.priority ASC, pd2.price ASC LIMIT 1), " .
"(SELECT price FROM " . DB_PREFIX . "product_special ps WHERE ps.product_id = p.product_id AND ps.customer_group_id = '" . (int)$customer_group_id . "' AND ((ps.date_start = '0000-00-00' OR ps.date_start < NOW()) AND (ps.date_end = '0000-00-00' OR ps.date_end > NOW())) ORDER BY ps.priority ASC, ps.price ASC LIMIT 1), " .
"p.price) as realprice " .
" FROM " . DB_PREFIX . "product p" .
" LEFT JOIN " . DB_PREFIX . "product_description pd ON (pd.product_id=p.product_id) ".
" LEFT JOIN " . DB_PREFIX . "manufacturer m ON(m.manufacturer_id=p.manufacturer_id) " .
" LEFT JOIN " . DB_PREFIX . "product_option_value pov ON (pov.product_id=p.product_id)" .
" LEFT JOIN " . DB_PREFIX . "product_to_store p2s ON (p2s.product_id=p.product_id)" .
" LEFT JOIN " . DB_PREFIX . "product_to_category p2c ON (p2c.product_id=p.product_id)";
$sql .= " LEFT JOIN " . DB_PREFIX . "product_attribute p2a ON (p2a.product_id=p.product_id)";

if(isset($this->request->post['old_route']) and $this->request->post['old_route'] == 'product/special') {
$sql .= " RIGHT JOIN " . DB_PREFIX . "product_special ps2 ON (ps2.product_id=p.product_id)";
}

if ($data['filter']) {
$sql .= " LEFT JOIN " . DB_PREFIX . "product_filter pf ON (p2c.product_id = pf.product_id)";
}

$sql .= " WHERE 1";

if(isset($this->request->post['old_route']) and $this->request->post['old_route'] == 'product/special') {
$sql .= " AND p.product_id IN (SELECT ps9.product_id FROM " . DB_PREFIX . "product_special AS ps9 WHERE (ps9.date_start = '0000-00-00' AND ps9.date_end = '0000-00-00') OR NOW() BETWEEN DATE(ps9.date_start) AND DATE(ps9.date_end))";
}

if($data['instock']) {
$sql .= " AND p.quantity > 0 AND (pov.quantity is null OR pov.quantity > 0)";
}

if($data['categories']) {
$sql .= " AND p2c.category_id IN (" . implode(",", $data['categories']) . ")";
}

if($data['manufacturer']) {
$sql .= " AND p.manufacturer_id IN(" . implode(", ", $data['manufacturer']) . ")";
}

if (isset($this->request->post['search']) and !empty($this->request->post['search'])) {
$words = explode(' ', trim(preg_replace('/\s+/', ' ', $this->request->post['search'])));

foreach ($words as $word) {
$sql .= " AND pd.name LIKE '%" . $this->db->escape($word) . "%'";
}
}

$d = $this->adv_ajaxfilter_setting['attr_delimeter'];

if($data['attribute_value']) {
if($this->adv_ajaxfilter_setting['attribute_mode'] == 'and') {
$i = 0;

foreach($data['attribute_value'] as $attribute_id => $values) {
$sql .= " AND EXISTS (select 1 FROM " . DB_PREFIX . "product_attribute p2a" . $i . " WHERE p2a" . $i . ".product_id=p2a.product_id AND p2a" . $i . ".attribute_id = " . (int)$attribute_id .
" AND (p2a" . $i . ".text = '" . implode("' OR p2a" . $i . ".text = '", array_map(array($this->db, 'escape'), $values)) . "'" .
" OR p2a" . $i . ".text like '" . implode($d . "%' OR p2a" . $i . ".text like '", array_map(array($this->db, 'escape'), $values)) . $d . "%'" .
" OR p2a" . $i . ".text like '%" . $d . implode("' OR p2a" . $i . ".text like '%" . $d, array_map(array($this->db, 'escape'), $values)) . "'" .
" OR p2a" . $i . ".text like '%" . $d . implode($d . "%' OR p2a" . $i . ".text like '%" . $d, array_map(array($this->db, 'escape'), $values)) . $d . "%')) ";
$i++;
}
} else {
foreach($data['attribute_value'] as $attribute_id => $values) {
$attribute_filters[] = "p2a.attribute_id = " . (int)$attribute_id .
" AND (p2a.text = '" . implode("' OR p2a.text = '", array_map(array($this->db, 'escape'), $values)) . "'" .
" OR p2a.text like '" . implode($d . "%' OR p2a.text like '", array_map(array($this->db, 'escape'), $values)) . $d . "%'" .
" OR p2a.text like '%" . $d . implode("' OR p2a.text like '%" . $d, array_map(array($this->db, 'escape'), $values)) . "'" .
" OR p2a.text like '%" . $d . implode($d . "%' OR p2a.text like '%" . $d, array_map(array($this->db, 'escape'), $values)) . $d . "%')";
}
$sql .= " AND (" . implode(" OR ", $attribute_filters) . ")";
}
}

if($data['attr_slider']) {
$i = 0;
foreach($data['attr_slider'] as $attribute_id => $values) {
if(!isset($values['min'])) {
$sql .= " AND EXISTS (select 1 FROM " . DB_PREFIX . "product_attribute p2a" . $i . " WHERE p2a" . $i . ".product_id = p2a.product_id AND p2a" . $i . ".attribute_id = " . (int)$attribute_id . " AND " .
"(p2a" . $i . ".text * 1 <= " . $values['max'] . ")) ";
} elseif(!isset($values['max'])) {
$sql .= " AND EXISTS (select 1 FROM " . DB_PREFIX . "product_attribute p2a" . $i . " WHERE p2a" . $i . ".product_id = p2a.product_id AND p2a" . $i . ".attribute_id = " . (int)$attribute_id . " AND " .
"(p2a" . $i . ".text * 1 >= " . $values['min'] . ")) ";

} else {
$sql .= " AND EXISTS (select 1 FROM " . DB_PREFIX . "product_attribute p2a" . $i . " WHERE p2a" . $i . ".product_id = p2a.product_id AND p2a" . $i . ".attribute_id = " . (int)$attribute_id . " AND " .
"(p2a" . $i . ".text * 1 BETWEEN " . $values['min'] . " AND " . $values['max'] . ")) ";
}
$i++;
}
}

if($data['min_price'] >= 0) {
$min_price = $this->currency->convert((int)$data['min_price'],$this->config->get('config_currency') , $this->config->get('config_currency'));
$sql .= " AND p.price >=" . (int)$min_price;
}

if ($data['filter']) {
//todo:: add and/or for filter and filter group

$array = array();
foreach($data['filter'] as $filter){
$array = array_merge($array, $filter);
}

$sql .= " AND pf.filter_id IN (" .implode(",", $array). ")";
}

$sql .= " AND p.status = '1' AND p.date_available <= NOW( ) AND p2s.store_id = " . (int)$this->config->get('config_store_id');
$sql .= ") as innertable WHERE 1 ";
if($data['min_price'] >= 0) {
$min_price = $this->currency->convert((int)$data['min_price'],$this->config->get('config_currency') , $this->config->get('config_currency'));
$sql .= " AND realprice >=" . (int)$min_price;
}
if($data['max_price'] > 0) {
$max_price = $this->currency->convert($data['max_price'],$this->config->get('config_currency') , $this->config->get('config_currency'));
$sql .= " AND realprice <=" . ($max_price);
}

$sql .= " GROUP BY " . "option_value_id";
$query = $this->db->query($sql);

$result = array();
foreach($query->rows as $row) {
$result[] = array('id' => $row['option_value_id'], 't' => $row['total']);
}

return $result;
}

public function getProducts($data) {
$customer_group_id = $this->getCustomerGroup();

$sql = "SELECT product_id FROM(";
$sql .= "SELECT DISTINCT p.product_id, pd.name, p.model, p.quantity, p.price, p.sort_order, p.date_added ";
if(isset($data["sort"]) && $data["sort"] == "rating") {
$sql .= ", (SELECT AVG(rating) AS total FROM " . DB_PREFIX . "review r1 WHERE r1.product_id = p.product_id AND r1.status = '1' GROUP BY r1.product_id) AS rating ";
}
$sql .= ", coalesce((SELECT price FROM " . DB_PREFIX . "product_discount pd2 WHERE pd2.product_id = p.product_id AND pd2.customer_group_id = '" . (int)$customer_group_id . "' AND pd2.quantity = '1' AND ((pd2.date_start = '0000-00-00' OR pd2.date_start < NOW()) AND (pd2.date_end = '0000-00-00' OR pd2.date_end > NOW())) ORDER BY pd2.priority ASC, pd2.price ASC LIMIT 1), " .
"(SELECT price FROM " . DB_PREFIX . "product_special ps WHERE ps.product_id = p.product_id AND ps.customer_group_id = '" . (int)$customer_group_id . "' AND ((ps.date_start = '0000-00-00' OR ps.date_start < NOW()) AND (ps.date_end = '0000-00-00' OR ps.date_end > NOW())) ORDER BY ps.priority ASC, ps.price ASC LIMIT 1), " .
"p.price) as realprice ";
$sql .= "FROM " . DB_PREFIX . "product p" .
" LEFT JOIN " . DB_PREFIX . "product_option_value pov ON (pov.product_id=p.product_id)" .
" LEFT JOIN " . DB_PREFIX . "product_description pd ON (pd.product_id=p.product_id)" .
" LEFT JOIN " . DB_PREFIX . "product_to_store p2s ON (p2s.product_id=p.product_id)" .
" LEFT JOIN " . DB_PREFIX . "product_to_category p2c ON (p2c.product_id=p.product_id)";
if($data['attribute_value'] || $data['attr_slider']) {
$sql .= " LEFT JOIN " . DB_PREFIX . "product_attribute p2a ON (p2a.product_id=p.product_id)";
}

if(isset($this->request->post['old_route']) and $this->request->post['old_route'] == 'product/special') {
$sql .= " RIGHT JOIN " . DB_PREFIX . "product_special ps2 ON (ps2.product_id=p.product_id)";
}

if ($data['filter']) {
$sql .= " LEFT JOIN " . DB_PREFIX . "product_filter pf ON (p2c.product_id = pf.product_id)";
}

$sql .= " WHERE 1";

if(isset($this->request->post['old_route']) and $this->request->post['old_route'] == 'product/special') {
$sql .= " AND p.product_id IN (SELECT ps9.product_id FROM " . DB_PREFIX . "product_special AS ps9 WHERE (ps9.date_start = '0000-00-00' AND ps9.date_end = '0000-00-00') OR NOW() BETWEEN DATE(ps9.date_start) AND DATE(ps9.date_end))";
}

if($data['instock']) {
$sql .= " AND p.quantity > 0 AND (pov.quantity is null OR pov.quantity > 0)";
}

if($data['categories']) {
$sql .= " AND p2c.category_id IN (" . implode(",", $data['categories']) . ")";
}

$option_filters = array();
if($data['option_value']) {
foreach($data['option_value'] as $option_value) {
$option_filters[] = "option_value_id IN(" . implode(",", $option_value) . ")";
}
}

if($option_filters) {
if($this->adv_ajaxfilter_setting['option_mode'] == 'and') {
foreach($option_filters as $i => $option_filter) {
$sql .= " AND EXISTS (select 1 FROM " . DB_PREFIX . "product_option_value pov" . $i .
" WHERE pov" . $i . ".product_id=pov.product_id AND pov" . $i . "." . $option_filter . ($data['instock'] ? "AND pov" . $i . ".quantity > 0"
: "") . ") ";
}
} else {
$sql .= " AND (" . implode(" OR ", $option_filters) . ")";
}
}

if($data['manufacturer']) {
$sql .= " AND p.manufacturer_id IN(" . implode(", ", $data['manufacturer']) . ")";
}

$d = $this->adv_ajaxfilter_setting['attr_delimeter'];

if($data['attribute_value']) {
if($this->adv_ajaxfilter_setting['attribute_mode'] == 'and') {
$i = 0;

foreach($data['attribute_value'] as $attribute_id => $values) {
if($this->adv_ajaxfilter_setting['attribute_value_mode'] == 'or') {
$sql .= " AND EXISTS (select 1 FROM " . DB_PREFIX . "product_attribute p2a" . $i . " WHERE p2a" . $i . ".product_id=p2a.product_id AND p2a" . $i . ".attribute_id = " . (int)$attribute_id .
" AND (p2a" . $i . ".text = '" . implode("' OR p2a" . $i . ".text = '", array_map(array($this->db, 'escape'), $values)) . "'" .
" OR p2a" . $i . ".text like '" . implode($d . "%' OR p2a" . $i . ".text like '", array_map(array($this->db, 'escape'), $values)) . $d . "%'" .
" OR p2a" . $i . ".text like '%" . $d . implode("' OR p2a" . $i . ".text like '%" . $d, array_map(array($this->db, 'escape'), $values)) . "'" .
" OR p2a" . $i . ".text like '%" . $d . implode($d . "%' OR p2a" . $i . ".text like '%" . $d, array_map(array($this->db, 'escape'), $values)) . $d . "%')) ";
$i++;
} else {
foreach($values as $value) {
$sql .= " AND EXISTS (select 1 FROM " . DB_PREFIX . "product_attribute p2a" . $i . " WHERE p2a" . $i . ".product_id=p2a.product_id AND p2a" . $i . ".attribute_id = " . (int)$attribute_id .
" AND (p2a" . $i . ".text = '" . $this->db->escape($value) . "'" .
" OR p2a" . $i . ".text like '" . $this->db->escape($value) . $d . "%'" .
" OR p2a" . $i . ".text like '%" . $d . $this->db->escape($value) . "'" .
" OR p2a" . $i . ".text like '%" . $d . $this->db->escape($value) . $d . "%')) ";
$i++;
}
}
}
} else {
foreach($data['attribute_value'] as $attribute_id => $values) {
$attribute_filters[] = "p2a.attribute_id = " . (int)$attribute_id .
" AND (p2a.text = '" . implode("' OR p2a.text = '", array_map(array($this->db, 'escape'), $values)) . "'" .
" OR p2a.text like '" . implode($d . "%' OR p2a.text like '", array_map(array($this->db, 'escape'), $values)) . $d . "%'" .
" OR p2a.text like '%" . $d . implode("' OR p2a.text like '%" . $d, array_map(array($this->db, 'escape'), $values)) . "'" .
" OR p2a.text like '%" . $d . implode($d . "%' OR p2a.text like '%" . $d, array_map(array($this->db, 'escape'), $values)) . $d . "%')";
}
$sql .= " AND (" . implode(" OR ", $attribute_filters) . ")";
}
}

if($data['attr_slider']) {
$i = 0;
foreach($data['attr_slider'] as $attribute_id => $values) {
if(!isset($values['min'])) {
$sql .= " AND EXISTS (select 1 FROM " . DB_PREFIX . "product_attribute p2a" . $i . " WHERE p2a" . $i . ".product_id = p2a.product_id AND p2a" . $i . ".attribute_id = " . (int)$attribute_id . " AND " .
"(p2a" . $i . ".text * 1 <= " . $values['max'] . ")) ";
} elseif(!isset($values['max'])) {
$sql .= " AND EXISTS (select 1 FROM " . DB_PREFIX . "product_attribute p2a" . $i . " WHERE p2a" . $i . ".product_id = p2a.product_id AND p2a" . $i . ".attribute_id = " . (int)$attribute_id . " AND " .
"(p2a" . $i . ".text * 1 >= " . $values['min'] . ")) ";

} else {
$sql .= " AND EXISTS (select 1 FROM " . DB_PREFIX . "product_attribute p2a" . $i . " WHERE p2a" . $i . ".product_id = p2a.product_id AND p2a" . $i . ".attribute_id = " . (int)$attribute_id . " AND " .
"(p2a" . $i . ".text * 1 BETWEEN " . $values['min'] . " AND " . $values['max'] . ")) ";
}
$i++;
}
}

if($data['min_price'] >= 0) {
$min_price = $this->currency->convert((int)$data['min_price'],$this->config->get('config_currency') , $this->config->get('config_currency'));
$sql .= " AND p.price >=" . (int)($min_price);
}

if ($data['filter']) {
//todo:: add and/or for filter and filter group
if($this->adv_ajaxfilter_setting['filter_group_mode'] == 'and') {
$array_filter = array();
$array_group = array();
foreach($data['filter'] as $filter_group){
//$sql .= " AND pf.filter_id IN (" .implode(",", $filter). ")";
foreach($filter_group as $filter) {
$array_filter[] = $filter." IN (SELECT filter_id FROM " . DB_PREFIX . "product_filter pf WHERE pf.product_id = p.product_id) ";
}

$array_group[] = '( '. implode(' OR ', $array_filter). ')';
$array_filter = array();
}

$test = " AND ". implode(' AND ', $array_group);
$sql .= " AND ". implode(' AND ', $array_group);
}
else{
//todo:: add and/or for filter and filter group
$array = array();
foreach($data['filter'] as $filter){
$array = array_merge($array, $filter);
}

$sql .= " AND pf.filter_id IN (" .implode(",", $array). ")";
}
}

$sql .= " AND pd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND p.status = '1' AND p.date_available <= NOW( ) AND p2s.store_id = " . (int)$this->config->get('config_store_id');

if (isset($this->request->post['search']) and !empty($this->request->post['search'])) {
$words = explode(' ', trim(preg_replace('/\s+/', ' ', $this->request->post['search'])));

foreach ($words as $word) {
$sql .= " AND pd.name LIKE '%" . $this->db->escape($word) . "%'";
}
}

$sort_data = array(
'pd.name' => 'name',
'p.model' => 'model',
'p.quantity' => 'quantity',
'p.price' => 'realprice',
'p.sort_order' => 'sort_order',
'p.date_added' => 'date_added',
'rating' => 'rating'
);

$sql .= ") as innertable WHERE 1 ";
if($data['min_price'] >= 0) {
$min_price = $this->currency->convert((int)$data['min_price'], $this->config->get('config_currency') , $this->config->get('config_currency'));
$sql .= "AND realprice >=" . (int)($min_price);
}
if($data['max_price'] > 0) {
$max_price = $this->currency->convert($data['max_price'], $this->config->get('config_currency') , $this->config->get('config_currency'));
$sql .= " AND realprice <=" . ($max_price);
}

if(isset($this->request->post['old_route']) and $this->request->post['old_route'] == 'product/latest') {
$sql .= " ORDER BY DATE(date_added) DESC ";
}
else{
if(isset($data['sort']) && array_key_exists($data['sort'], $sort_data)) {
$data['sort'] = $sort_data[$data['sort']];
if($data['sort'] == 'name' || $data['sort'] == 'model') {
$sql .= " ORDER BY LCASE(" . $data['sort'] . ")";
} else {
$sql .= " ORDER BY " . $data['sort'];
}
} else {
$sql .= " ORDER BY sort_order";
}

if(isset($data['order']) && ($data['order'] == 'DESC')) {
$sql .= " DESC, LCASE(name) DESC";
} else {
$sql .= " ASC, LCASE(name) ASC";
}
}

if(isset($data['start']) || isset($data['limit'])) {
if($data['start'] < 0) {
$data['start'] = 0;
}

if($data['limit'] < 1) {
$data['limit'] = 20;
}

$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
}

$query = $this->db->query($sql);
// var_dump($sql);
$product_data = array();
if($query->rows) {
$this->load->model('catalog/product');
foreach($query->rows as $result) {
$product_data[$result['product_id']] = $this->model_catalog_product->getProduct($result['product_id']);
}
}
return $product_data;
}

public function getTaxRates($tax_class_id) {
$query = $this->db->query("SELECT tr1.rate, tr1.type FROM " . DB_PREFIX . "tax_rate tr1 LEFT JOIN " . DB_PREFIX . "tax_rule tr2 ON (tr1.tax_rate_id=tr2.tax_rate_id) " .
"WHERE tr2.tax_class_id=" . (int)$tax_class_id . " ORDER BY tr2.priority");
return $query->rows;
}

}



controller dosyası :

class ControllerExtensionModuleAdvajaxfilter extends Controller {
private $k = 1;

public function index($setting)
{
$this->load->language('extension/module/adv_ajaxfilter');

$this->document->addStyle('catalog/view/theme/default/stylesheet/filter.css');


$data['text_tax'] = $this->language->get('text_tax');
$data['button_cart'] = $this->language->get('button_cart');
$data['button_wishlist'] = $this->language->get('button_wishlist');
$data['button_compare'] = $this->language->get('button_compare');
$data['text_price_range'] = $this->language->get('text_price_range');
$data['text_manufacturers'] = $this->language->get('text_manufacturers');
$data['text_categories'] = $this->language->get('text_categories');
$data['text_attributes'] = $this->language->get('text_attributes');
$data['text_all'] = $this->language->get('text_all');
$data['clear_filter'] = $this->language->get('clear_filter');
$data['text_instock'] = $this->language->get('text_instock');

$data['heading_title'] = $this->language->get('heading_title');

$data['pds_sku'] = $this->language->get('pds_sku');
$data['pds_upc'] = $this->language->get('pds_upc');
$data['pds_location'] = $this->language->get('pds_location');
$data['pds_model'] = $this->language->get('pds_model');
$data['pds_brand'] = $this->language->get('pds_brand');
$data['pds_stock'] = $this->language->get('pds_stock');


$data['symbol_right']=$this->currency->getSymbolRight( $this->session->data['currency']);
$data['symbol_left']=$this->currency->getSymbolLeft($this->session->data['currency']);

// $data['symbol_right'] = $this->currency->getSymbolRight();
// $data['symbol_left'] = $this->currency->getSymbolLeft();

$this->load->model('setting/setting');
$advsettings = $this->model_setting_setting->getSetting('adv_ajaxfilter');
if (isset($advsettings['adv_ajaxfilter_module'])) {
$data['setting'] = $advsettings['adv_ajaxfilter_module'][0];
}
else{
$data['setting'] = array();
}

$adv_ajaxfilter_setting = $this->config->get('adv_ajaxfilter_setting');


$cat_id = false;
$data['path'] = "";
if(isset($this->request->get['path'])) {
$data['path'] = $this->request->get['path'];
$parts = explode('_', (string)$this->request->get['path']);
$cat_id = array_pop($parts);
}

$man_id = false;
if(isset($this->request->get['manufacturer_id'])) {
$man_id = $this->request->get['manufacturer_id'];
$_data = array(
'filter_manufacturer_id' => $this->request->get['manufacturer_id']
);
} else {
$_data = array(
'filter_category_id' => $cat_id,
'filter_sub_category' => false
);
}

$this->load->model('catalog/product');
$product_total = $this->model_catalog_product->getTotalProducts($_data);
if($product_total < 2) {
return;
}

$data['category_id'] = $cat_id;

$_data = array('category_id' => $cat_id, 'manufacturer_id' => $man_id);

$this->load->model('module/adv_ajaxfilter');
$this->load->model('tool/image');

$data['manufacturers'] = false;
if(isset($this->request->get['manufacturer_id'])) {
$data['manufacturer_id'] = $this->request->get['manufacturer_id'];
} else {
if($adv_ajaxfilter_setting['display_manufacturer'] != 'none') {
$data['manufacturers'] = $this->model_module_adv_ajaxfilter->getManufacturers($_data);
$data['display_manufacturer'] = $adv_ajaxfilter_setting['display_manufacturer'];
$data['expanded_manufacturer'] = isset($adv_ajaxfilter_setting['expanded_manufacturer']) ? 1 : 0;
foreach($data['manufacturers'] as $key => $manufacturer) {
$data['manufacturers'][$key]['image'] = $this->model_tool_image->resize($manufacturer['image'], 50, 50);
}
}
}
$data['options'] = $this->model_module_adv_ajaxfilter->getOptions($_data);
foreach($data['options'] as $i => $option) {
if(!isset($adv_ajaxfilter_setting['display_option_' . $option['option_id']])) {
$adv_ajaxfilter_setting['display_option_' . $option['option_id']] = 'none';
}
$display_option = $adv_ajaxfilter_setting['display_option_' . $option['option_id']];
if($display_option != 'none') {
$data['options'][$i]['display'] = $display_option;
$data['options'][$i]['expanded'] = isset($adv_ajaxfilter_setting['expanded_option_' . $option['option_id']]) ? 1 : 0;
foreach($data['options'][$i]['option_values'] as $j => $option_value) {
$data['options'][$i]['option_values'][$j]['thumb'] = $this->model_tool_image->resize($data['options'][$i]['option_values'][$j]['image'], 50, 50);
}
} else {
unset($data['options'][$i]);
}
}

$data['categories'] = false;
if($adv_ajaxfilter_setting['display_categories'] != 'none') {
$data['display_categories'] = $adv_ajaxfilter_setting['display_categories'];
$data['categories'] = $this->model_module_adv_ajaxfilter->getSubCategories($_data);
$data['expanded_categories'] = isset($adv_ajaxfilter_setting['expanded_categories']) ? 1 : 0;
}

$data['attributes'] = $this->model_module_adv_ajaxfilter->getAttributes($_data);

foreach($data['attributes'] as $j => $attr_group) {
foreach($attr_group['attribute_values'] as $attr_id => $attr) {
if(!isset($adv_ajaxfilter_setting['display_attribute_' . $attr_id])) {
$adv_ajaxfilter_setting['display_attribute_' . $attr_id] = 'none';
}
$display_attribute = $adv_ajaxfilter_setting['display_attribute_' . $attr_id];
if($display_attribute != 'none') {
if($display_attribute == 'slider') {
$values = $data['attributes'][$j]['attribute_values'][$attr_id]['values'];
$first = $values[0];
$data['attributes'][$j]['attribute_values'][$attr_id]['suffix'] = preg_replace("/^-?[0-9 .,]*/", '', $first);

$values = array_map('floatval', $values);
$values = array_unique($values);
sort($values);
$data['attributes'][$j]['attribute_values'][$attr_id]['values'] = $values;
}
$data['attributes'][$j]['attribute_values'][$attr_id]['display'] = $display_attribute;
$data['attributes'][$j]['attribute_values'][$attr_id]['expanded'] = isset($adv_ajaxfilter_setting['expanded_attribute_' . $attr_id]) ? 1 : 0;
} else {
unset($data['attributes'][$j]['attribute_values'][$attr_id]);
if(!$data['attributes'][$j]['attribute_values']) {
unset($data['attributes'][$j]);
}
}
}
}

$data['filter_groups'] = array();
$this->load->model('catalog/category');

$fltr_grps = $this->model_catalog_category->getCategoryFilters($cat_id);
$data['display_filters'] = $adv_ajaxfilter_setting['display_filters'];
$data['expanded_filters'] = isset($adv_ajaxfilter_setting['expanded_filters']) ? 1 : 0;

if ($fltr_grps) {
foreach ($fltr_grps as $fltr_grp) {
$filter_data = array();

foreach ($fltr_grp['filter'] as $filter) {
$_data = array(
'filter_category_id' => $cat_id,
'filter_filter' => $filter['filter_id']
);

$filter_data[] = array(
'filter_id' => $filter['filter_id'],
'name' => $filter['name'] . ($this->config->get('config_product_count') ? ' (' . $this->model_catalog_product->getTotalProducts($_data) . ')' : '')
);
}

$data['filter_groups'][] = array(
'filter_group_id' => $fltr_grp['filter_group_id'],
'name' => $fltr_grp['name'],
'filter' => $filter_data
);
}
}


$data['price_slider'] = $adv_ajaxfilter_setting['price_slider'];
$data['attr_group'] = $adv_ajaxfilter_setting['attr_group'];

$data['instock_checked'] = isset($adv_ajaxfilter_setting['instock_checked']) ? 1 : 0;
$data['instock_visible'] = isset($adv_ajaxfilter_setting['instock_visible']) ? 1 : 0;

if($data['options'] || $data['manufacturers'] || $data['attributes'] || $data['price_slider']) {
$this->document->addScript('catalog/view/javascript/jquery-ui/jquery-ui.js');
$this->document->addScript('catalog/view/javascript/jquery.plugins.adv.js');
$this->document->addScript('catalog/view/javascript/adv_ajaxfilter.min.js');

$this->document->addStyle('catalog/view/javascript/jquery-ui/css/jquery-ui.css');
}

$adv_ajaxfilter_setting['adv_ajaxfilter_afterload'] ="";
$data['adv_ajaxfilter_container'] = $adv_ajaxfilter_setting['adv_ajaxfilter_container'];
$data['adv_ajaxfilter_afterload'] = html_entity_decode($adv_ajaxfilter_setting['adv_ajaxfilter_afterload'], ENT_QUOTES, 'UTF-8');

if(isset($this->request->get['route'])) {
$data['old_route'] = $this->request->get['route'];
}
else {
$data['old_route'] ="";
}

if(isset($this->request->get['search'])) {
$data['search'] = $this->request->get['search'];
}
else {
$data['search'] ="";
}

$data['route'] = '';
if(isset($this->request->get['route'])){
$data['route'] = $this->request->get['route'];
}

$data['url'] = $this->url->link('product/adv_ajaxfilter', '');





return $this->load->view('extension/module/adv_ajaxfilter', $data);
}

public function getProducts() {
$this->load->language('extension/module/adv_ajaxfilter');


$adv_ajaxfilter_setting = $this->config->get('adv_ajaxfilter_setting');

if((float)$adv_ajaxfilter_setting['tax'] > 0) {
$this->k = 1 + (float)$adv_ajaxfilter_setting['tax'] / 100;
}

if (isset($this->request->get['page'])) {
$page = $this->request->get['page'];
} else if (isset($this->request->post['page'])) {
$page = $this->request->post['page'];
} else {
$page = 1;
}

if (isset($this->request->get['limit'])) {
$limit = $this->request->get['limit'];
} else {
$limit = $this->config->get('config_product_limit');
}


if (isset($_SERVER['HTTP_REFERER'])) {

$THE_REFER=$_SERVER['HTTP_REFERER'];

$query = parse_url($_SERVER['HTTP_REFERER'], PHP_URL_QUERY);
$par = explode('&',$query);
}










if(isset($this->request->post['sort'])) {
$sort = $this->request->post['sort'];
} else if(isset($par[2])){
$new_sort = explode('=',$par[2]);
$sort = $new_sort[1];
} else {
$sort = 'p.sort_order';
}

if(isset($this->request->post['order'])) {
$order = $this->request->post['order'];
} else if(isset($par[3])){
$new_order = explode('=',$par[3]);
$order = $new_order[1];
} else {
$order = 'ASC';
}

$this->load->model('module/adv_ajaxfilter');
$this->load->model('catalog/product');
$this->load->model('tool/image');

$man = false;
if(isset($this->request->post['manufacturer'])) {
$man = $this->trim_array_values($this->request->post['manufacturer']);
if(!count($man)) {
$man = false;
}
}
$man_id = false;
if(isset($this->request->post['manufacturer_id'])) {
$man_id = $this->request->post['manufacturer_id'];
$man = array($man_id);
}

$option_value = false;
if(isset($this->request->post['option_value'])) {
$option_value = $this->trim_array_values($this->request->post['option_value']);
if(!count($option_value)) {
$option_value = false;
}
}

$attr_value = array();
if(isset($this->request->post['attribute_value'])) {
$attr_value = $this->trim_array_values($this->request->post['attribute_value']);
if(!count($attr_value)) {
$attr_value = array();
}
}

$instock = false;
if(isset($this->request->post['instock'])) {
$instock = true;
}


$cats = false;
if(isset($this->request->post['categories'])) {
$cats = $this->trim_array_values($this->request->post['categories']);
if(!count($cats)) {
$cats = false;
}
}

$cat_id = 0;
if(isset($this->request->post['category_id'])) {
$cat_id = $this->request->post['category_id'];
}
if(!$cats && $cat_id) {
$cats = array($cat_id);
}

$attr_slider = isset($this->request->post['attr_slider']) ? $this->request->post['attr_slider'] : false;

$filter = false;
if(isset($this->request->post['filter'])) {
$filter = $this->trim_array_values($this->request->post['filter']);
if(!count($filter)) {
$filter = false;
}
}

$data = array(
'instock' => $instock,
'option_value' => $option_value,
'manufacturer' => $man,
'attribute_value' => $attr_value,
'categories' => $cats,
'attr_slider' => $attr_slider,
'filter' => $filter,
'min_price' => $this->request->post['min_price'] / $this->k,
'max_price' => $this->request->post['max_price'] / $this->k,
'start' => ($page - 1) * $limit,
'limit' => $limit,
'sort' => $sort,
'order' => $order
);

if(isset($this->request->post['manufacturer_id']) || ($adv_ajaxfilter_setting['display_manufacturer'] == 'none')) {
$totals_mans = false;
} else {
$totals_mans = $this->model_module_adv_ajaxfilter->getTotalManufacturers($data);
}

$totals_options = $this->model_module_adv_ajaxfilter->getTotalOptions($data);

$totals_atrs = $this->model_module_adv_ajaxfilter->getTotalAttributes($data);
foreach($attr_value as $attr_id => $values) {
foreach($totals_atrs as $i => $attr) {
if($attr['id'] == $attr_id) {
unset($totals_atrs[$i]);
}
}

$temp_data = $data;
unset($temp_data['attribute_value'][$attr_id]);
foreach($this->model_module_adv_ajaxfilter->getTotalAttributes($temp_data) as $attr){
if($attr['id'] == $attr_id) {
$totals_atrs[] = $attr;
}
}
}


$totals_cats = $this->model_module_adv_ajaxfilter->getTotalCategories($data, $cat_id);

$results = $this->model_module_adv_ajaxfilter->getProducts($data);

$min_price = false;
$max_price = false;

if(isset($this->request->post['getPriceLimits']) && $this->request->post['getPriceLimits']) {
$priceLimits = $this->model_module_adv_ajaxfilter->getPriceLimits(array('category_id' => $cat_id, 'manufacturer_id' => $man_id));
$min_price = $priceLimits['min_price'];
$max_price = $priceLimits['max_price'];
}

$this->request->get['path'] = isset($this->request->post['path']) ? $this->request->post['path'] : '';

$product_total = $this->model_module_adv_ajaxfilter->getTotalProducts($data);

$pagination = new Pagination();
$pagination->total = $product_total;
$pagination->page = $page;
$pagination->limit = $limit;
$pagination->url = $this->url->link('product/category', 'path=' . $this->request->get['path'] . '&page={page}');

$pagination_result = sprintf($this->language->get('text_pagination'), ($product_total) ? (($page - 1) * $limit) + 1 : 0, ((($page - 1) * $limit) > ($product_total - $limit)) ? $product_total : ((($page - 1) * $limit) + $limit), $product_total, ceil($product_total / $limit));


//$data['upperlimit']=$this->currency->format(100, $this->session->data['currency'],($data['upperlimit']));

$min_price = $this->currency->convert($min_price * $this->k, $this->session->data['currency'],$this->config->get('config_currency'), $this->currency->getCode());
$max_price = $this->currency->convert($max_price * $this->k, $this->session->data['currency'],$this->config->get('config_currency'), $this->currency->getCode());


$result_html = $this->getProductsTmpl($results, $product_total);

$json = json_encode(array( 'result_html' => $result_html,
'min_price' => $min_price,
'max_price' => $max_price,
'pagination' => $pagination->render(),
'pagination_result' => $pagination_result,
'totals_data' => array('manufacturers' => $totals_mans,
'options' => $totals_options,
'attributes' => $totals_atrs,
'categories' => $totals_cats)));
$this->response->setOutput($json);
}

private function getProductsTmpl($results, $product_total) {
$this->load->language('product/category');
$data['text_refine'] = $this->language->get('text_refine');
$data['text_empty'] = $this->language->get('text_empty');
$data['text_quantity'] = $this->language->get('text_quantity');
$data['text_manufacturer'] = $this->language->get('text_manufacturer');
$data['text_model'] = $this->language->get('text_model');
$data['text_price'] = $this->language->get('text_price');
$data['text_tax'] = $this->language->get('text_tax');
$data['text_points'] = $this->language->get('text_points');
$data['text_compare'] = sprintf($this->language->get('text_compare'), (isset($this->session->data['compare']) ? count($this->session->data['compare']) : 0));
$data['text_display'] = $this->language->get('text_display');
$data['text_list'] = $this->language->get('text_list');
$data['text_grid'] = $this->language->get('text_grid');
$data['text_sort'] = $this->language->get('text_sort');
$data['text_limit'] = $this->language->get('text_limit');

$data['button_cart'] = $this->language->get('button_cart');
$data['button_wishlist'] = $this->language->get('button_wishlist');
$data['button_compare'] = $this->language->get('button_compare');
$data['button_continue'] = $this->language->get('button_continue');

$data['products'] = array();

foreach ($results as $result) {
if ($result['image']) {
$image = $this->model_tool_image->resize($result['image'], $this->config->get('config_image_product_width'), $this->config->get('config_image_product_height'));
} else {
$image = false;
}

if (($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) {
$price = $this->currency->format($this->tax->calculate($result['price'], $result['tax_class_id'], $this->config->get('config_tax')));
} else {
$price = false;
}

if ((float)$result['special']) {
$special = $this->currency->format($this->tax->calculate($result['special'], $result['tax_class_id'], $this->config->get('config_tax')));
} else {
$special = false;
}

if ($this->config->get('config_tax')) {
$tax = $this->currency->format((float)$result['special'] ? $result['special'] : $result['price']);
} else {
$tax = false;
}

if ($this->config->get('config_review_status')) {
$rating = (int)$result['rating'];
} else {
$rating = false;
}

$data['products'][] = array(
'product_id' => $result['product_id'],
'thumb' => $image,
'name' => $result['name'],
'description' => utf8_substr(strip_tags(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8')), 0, 100) . '..',
'price' => $price,
'special' => $special,
'tax' => $tax,
'rating' => $result['rating'],
'reviews' => sprintf($this->language->get('text_reviews'), (int)$result['reviews']),
'href' => $this->url->link('product/product', 'path=' . $this->request->get['path'] . '&product_id=' . $result['product_id']),
'saving' => $result['price'] == 0 ? 100 : round((($result['price'] - $result['special'])/$result['price'])*100, 0)
);
}



return $this->load->view('extension/module/adv_ajaxfilter_products', $data);
}

private function trim_array_values(array $array) {
foreach($array as $key => $value) {
if(is_array($value)) {
$array[$key] = $this->trim_array_values($value);
if(!count($array[$key])) {
unset($array[$key]);
}
} elseif(is_string($value)) {
$value = trim($value);
if(!$value) {
unset($array[$key]);
}
}
}
return $array;
}
}

?>